更新时间:2023-11-21 gmt 08:00
通过java语言连接实例-凯发k8国际娱乐官网入口
本章节介绍了通过java语言连接geminidb influx实例的方法。
pom文件依赖
org.influxdb influxdb-java 2.21
使用ssl方式连接实例的java代码示例
package influxdb; import java.security.securerandom; import java.security.cert.x509certificate; import java.util.concurrent.timeunit; import javax.net.ssl.sslcontext; import okhttp3.okhttpclient; import org.influxdb.influxdb; import org.influxdb.influxdbfactory; import org.influxdb.dto.point; import org.influxdb.dto.query; import org.influxdb.dto.queryresult; import org.apache.http.ssl.sslcontexts; import javax.net.ssl.*; public class demo { public static void main(string[] args) { okhttpclient.builder client = new okhttpclient.builder() .connecttimeout(10, timeunit.seconds) .writetimeout(10, timeunit.seconds) .readtimeout(10, timeunit.seconds) .retryonconnectionfailure(true); client.sslsocketfactory(defaultsslsocketfactory(), defaulttrustmanager()); client.hostnameverifier(noophostnameverifier()); final string serverurl = "https://127.0.0.1:8086", username = "root", password = "root"; influxdb influxdb = influxdbfactory.connect(serverurl, username, password, client); // create a database... string databasename = "foo"; influxdb.query(new query("create database " databasename, databasename)); influxdb.setdatabase(databasename); // write points to influxdb. influxdb.write(point.measurement("bar") .time(system.currenttimemillis(), timeunit.milliseconds) .tag("location", "chengdu") .addfield("temperature", 22) .build()); // query your data using influxql. queryresult queryresult = influxdb.query(new query("select * from bar", databasename)); // close it if your application is terminating or you are not using it anymore. influxdb.close(); } private static x509trustmanager defaulttrustmanager() { return new x509trustmanager() { public x509certificate[] getacceptedissuers() { return new x509certificate[0]; } public void checkclienttrusted(x509certificate[] certs, string authtype) { } public void checkservertrusted(x509certificate[] certs, string authtype) { } }; } private static sslsocketfactory defaultsslsocketfactory() { try { sslcontext sslcontext = sslcontexts.createdefault(); sslcontext.init(null, new trustmanager[] { defaulttrustmanager() }, new securerandom()); return sslcontext.getsocketfactory(); } catch (exception e) { throw new runtimeexception(e); } } private static hostnameverifier noophostnameverifier() { return new hostnameverifier() { @override public boolean verify(final string s, final sslsession sslsession) { return true;//true 表示使用ssl方式,但是不校验ssl证书,建议使用这种方式 } }; } }
使用非ssl方式连接实例的java代码示例
package influxdb; import okhttp3.okhttpclient; import org.influxdb.influxdb; import org.influxdb.influxdbfactory; import org.influxdb.dto.point; import org.influxdb.dto.query; import org.influxdb.dto.queryresult; import java.util.concurrent.timeunit; public class demonossl { public static void main(string[] args) { okhttpclient.builder client = new okhttpclient.builder() .connecttimeout(10, timeunit.seconds) .writetimeout(10, timeunit.seconds) .readtimeout(10, timeunit.seconds) .retryonconnectionfailure(true); final string serverurl = "http://127.0.0.1:8086", username = "root", password = "root"; influxdb influxdb = influxdbfactory.connect(serverurl, username, password, client); // create a database... string databasename = "foo"; influxdb.query(new query("create database " databasename, databasename)); influxdb.setdatabase(databasename); // write points to influxdb. influxdb.write(point.measurement("bar") .time(system.currenttimemillis(), timeunit.milliseconds) .tag("location", "chengdu") .addfield("temperature", 22) .build()); // query your data using influxql. queryresult queryresult = influxdb.query(new query("select * from bar", databasename)); // close it if your application is terminating or you are not using it anymore. influxdb.close(); } }
使用连接池方式连接实例的java代码示例
package influxdb; import okhttp3.connectionpool; import okhttp3.okhttpclient; import org.influxdb.influxdb; import org.influxdb.influxdbfactory; import org.influxdb.dto.point; import org.influxdb.dto.query; import org.influxdb.dto.queryresult; import java.util.concurrent.timeunit; public class democonnectionpool { public static void main(string[] args) { // 客户端连接池功能基于okhttpclient实现 okhttpclient.builder client = new okhttpclient().newbuilder(); client.connecttimeout(10, timeunit.seconds); client.readtimeout(10, timeunit.seconds); client.writetimeout(10, timeunit.seconds); // 建议设为true,屏蔽部分连接错误,并自动进行重试 client.retryonconnectionfailure(true); // maxidleconnections指连接池最多维护的未使用的idle连接数量,默认值是5 // 超过阈值的idle连接会由连接池关闭,关闭后sockets进入time_wait状态等待系统回收,该参数需根据实际连接数适当调整 client.connectionpool(new connectionpool(5, 30, timeunit.seconds)); final string serverurl = "http://127.0.0.1:8086", username = "root", password = "root"; influxdb influxdb = influxdbfactory.connect(serverurl, username, password, client); // create a database... string databasename = "foo"; influxdb.query(new query("create database " databasename, databasename)); influxdb.setdatabase(databasename); // write points to influxdb. influxdb.write(point.measurement("bar") .time(system.currenttimemillis(), timeunit.milliseconds) .tag("location", "chengdu") .addfield("temperature", 22) .build()); // query your data using influxql. queryresult queryresult = influxdb.query(new query("select * from bar", databasename)); // close it if your application is terminating or you are not using it anymore. influxdb.close(); } }
父主题:
意见反馈
文档内容是否对您有帮助?
提交成功!非常感谢您的反馈,我们会继续努力做到更好!
您可在查看反馈及问题处理状态。
系统繁忙,请稍后重试
如您有其它疑问,您也可以通过华为云社区问答频道来与我们联系探讨