好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

java使用influxDB数据库的详细代码

本文实例为大家分享了 java 使用influxdb 数据库 的具体代码,供大家参考,具体内容如下

1.pom.xml中导入jar包依赖

?

1

2

3

4

5

6

<!-- 引入influxdb依赖  -->

  <dependency>

   <groupid>org.influxdb</groupid>

   <artifactid>influxdb-java</artifactid>

   <version> 2.5 </version>

  </dependency>

2.编写influxdb工具类:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

package com.hontye.parameter.util;

 

import org.influxdb.influxdb;

import org.influxdb.influxdbfactory;

import org.influxdb.dto.point;

import org.influxdb.dto.point.builder;

import org.influxdb.dto.query;

import org.influxdb.dto.queryresult;

import java.util.map;

 

/**

  * 时序数据库 influxdb 连接

  * @author dai_lw

  *

  */

public class influxdbutil {

 

   private static string openurl = "http://127.0.0.1:8086" ;//连接地址

   private static string username = "root" ; //用户名

   private static string password = "root" ; //密码

   private static string database = "paramter_db" ; //数据库

   private static string measurement = "tw_parameter_tb" ; //表名

 

   private influxdb influxdb;

 

 

   public influxdbutil(string username, string password, string openurl, string database){

     this .username = username;

     this .password = password;

     this .openurl = openurl;

     this .database = database;

   }

 

   public static influxdbutil setup(){

     //创建 连接

     influxdbutil influxdbutil = new influxdbutil(username, password, openurl, database);

 

     influxdbutil.influxdbbuild();

 

     influxdbutil.createretentionpolicy();

 

//   influxdb.deletedb(database);

//   influxdb.createdb(database);

     return influxdbutil;

   }

 

   /**连接时序数据库;获得influxdb**/

   public influxdb influxdbbuild(){

     if (influxdb == null ){

       influxdb = influxdbfactory.connect(openurl, username, password);

       influxdb.createdatabase(database);

     }

     return influxdb;

   }

 

   /**

    * 设置数据保存策略

    * defalut 策略名 /database 数据库名/ 30d 数据保存时限30天/ 1 副本个数为1/ 结尾default 表示 设为默认的策略

    */

   public void createretentionpolicy(){

     string command = string.format( "create retention policy \"%s\" on \"%s\" duration %s replication %s default" ,

         "defalut" , database, "30d" , 1 );

     this .query(command);

   }

 

   /**

    * 查询

    * @param command 查询语句

    * @return

    */

   public queryresult query(string command){

     return influxdb.query( new query(command, database));

   }

 

   /**

    * 插入

    * @param tags 标签

    * @param fields 字段

    */

   public void insert(map<string, string> tags, map<string, object> fields){

     builder builder = point.measurement(measurement);

     builder.tag(tags);

     builder.fields(fields);

 

     influxdb.write(database, "" , builder.build());

   }

 

   /**

    * 删除

    * @param command 删除语句

    * @return 返回错误信息

    */

   public string deletemeasurementdata(string command){

     queryresult result = influxdb.query( new query(command, database));

     return result.geterror();

   }

 

   /**

    * 创建数据库

    * @param dbname

    */

   public void createdb(string dbname){

     influxdb.createdatabase(dbname);

   }

 

   /**

    * 删除数据库

    * @param dbname

    */

   public void deletedb(string dbname){

     influxdb.deletedatabase(dbname);

   }

 

   public string getusername() {

     return username;

   }

 

   public void setusername(string username) {

     this .username = username;

   }

 

   public string getpassword() {

     return password;

   }

 

   public void setpassword(string password) {

     this .password = password;

   }

 

   public string getopenurl() {

     return openurl;

   }

 

   public void setopenurl(string openurl) {

     this .openurl = openurl;

   }

 

   public void setdatabase(string database) {

     this .database = database;

   }

}

3.存值

?

1

2

3

4

5

6

7

8

9

10

11

12

13

public class quatyserviceimpl{

private influxdbutil influxdb;

 

public void intodb() {

   influxdb = influxdbutil.setup();

   map<string, string> tags = new hashmap<>();

   map<string, object> fields = new hashmap<>();

   tags.put( "tag_name" ,info.getkey());

   fields.put( "tag_value" ,code);

   fields.put( "timampest" , df.format( new date()));

   influxdb.insert(tags, fields);

   }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

原文链接:https://blog.csdn.net/qq_36004521/article/details/80101608

查看更多关于java使用influxDB数据库的详细代码的详细内容...

  阅读:54次