本文實(shí)例為大家分享了java使用influxdb數(shù)據(jù)庫(kù)的具體代碼,供大家參考,具體內(nèi)容如下
1.pom.xml中導(dǎo)入jar包依賴(lài)
1
2
3
4
5
6
|
<!-- 引入influxdb依賴(lài) --> <dependency> <groupid>org.influxdb</groupid> <artifactid>influxdb-java</artifactid> <version> 2.5 </version> </dependency> |
2.編寫(xiě)influxdb工具類(lèi):
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; /** * 時(shí)序數(shù)據(jù)庫(kù) 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" ; //數(shù)據(jù)庫(kù) 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(){ //創(chuàng)建 連接 influxdbutil influxdbutil = new influxdbutil(username, password, openurl, database); influxdbutil.influxdbbuild(); influxdbutil.createretentionpolicy(); // influxdb.deletedb(database); // influxdb.createdb(database); return influxdbutil; } /**連接時(shí)序數(shù)據(jù)庫(kù);獲得influxdb**/ public influxdb influxdbbuild(){ if (influxdb == null ){ influxdb = influxdbfactory.connect(openurl, username, password); influxdb.createdatabase(database); } return influxdb; } /** * 設(shè)置數(shù)據(jù)保存策略 * defalut 策略名 /database 數(shù)據(jù)庫(kù)名/ 30d 數(shù)據(jù)保存時(shí)限30天/ 1 副本個(gè)數(shù)為1/ 結(jié)尾default 表示 設(shè)為默認(rèn)的策略 */ 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); } /** * 查詢(xún) * @param command 查詢(xún)語(yǔ)句 * @return */ public queryresult query(string command){ return influxdb.query( new query(command, database)); } /** * 插入 * @param tags 標(biāo)簽 * @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 刪除語(yǔ)句 * @return 返回錯(cuò)誤信息 */ public string deletemeasurementdata(string command){ queryresult result = influxdb.query( new query(command, database)); return result.geterror(); } /** * 創(chuàng)建數(shù)據(jù)庫(kù) * @param dbname */ public void createdb(string dbname){ influxdb.createdatabase(dbname); } /** * 刪除數(shù)據(jù)庫(kù) * @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); } } |
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/qq_36004521/article/details/80101608