本文實例為大家分享了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