使用Java操作时序数据库influxdb,结果报错,求大神看看
org.influxdb.InfluxDBIOException: java.net.ConnectException: Failed to connect to /127.0.0.1:8086
at org.influxdb.impl.InfluxDBImpl.execute(InfluxDBImpl.java:612)
at org.influxdb.impl.InfluxDBImpl.write(InfluxDBImpl.java:356)
at org.influxdb.impl.InfluxDBImpl.write(InfluxDBImpl.java:331)
at com.learn.influxdb.test.InfluxDBConnect.insert(InfluxDBConnect.java:80)
at com.learn.influxdb.test.InfluxDBTest.testInsert(InfluxDBTest.java:76)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.net.ConnectException: Failed to connect to /127.0.0.1:8086
at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:242)
at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:160)
at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:257)
at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:135)
at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:114)
at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:126)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at org.influxdb.impl.GzipRequestInterceptor.intercept(GzipRequestInterceptor.java:42)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:144)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
at okhttp3.RealCall.execute(RealCall.java:77)
at retrofit2.OkHttpCall.execute(OkHttpCall.java:180)
at org.influxdb.impl.InfluxDBImpl.execute(InfluxDBImpl.java:604)
... 27 more
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at okhttp3.internal.platform.Platform.connectSocket(Platform.java:129)
at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:240)
... 52 more
代码如下:
package com.learn.influxdb.test;
import java.util.Map;
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;
/**
* 时序数据库 InfluxDB 连接
*/
public class InfluxDBConnect {
private String username;// 用户名
private String password;// 密码
private String openurl;// 连接地址
private String database;// 数据库
private InfluxDB influxDB;
public InfluxDBConnect(String username, String password, String openurl,
String database) {
this.username = username;
this.password = password;
this.openurl = openurl;
this.database = database;
}
/** 连接时序数据库;获得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 measurement 表
* @param tags 标签
* @param fields 字段
*/
public void insert(String measurement, Map tags,
Map 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;
}
}
package com.learn.influxdb.test;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.learn.influxdb.domain.CodeInfo;
import junit.framework.Assert;
import org.influxdb.dto.QueryResult;
import org.influxdb.dto.QueryResult.Result;
import org.influxdb.dto.QueryResult.Series;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.BeanWrapperImpl;
public class InfluxDBTest {
private InfluxDBConnect influxDBConnect;
private String username = "zg";// 用户名
private String password = "123";// 密码
private String openurl = "http://127.0.0.1:8086";// 连接地址
private String database = "test_db";// 数据库
private String measurement = "sys_code";
//http://192.168.80.130:8086/query?q=CREATE+DATABASE+%22testDB%22&db=testDB
@Before
public void setUp() {
// 创建 连接
influxDBConnect = new InfluxDBConnect(username, password, openurl, database);
influxDBConnect.influxDbBuild();
//influxDBConnect.createRetentionPolicy();
// influxDBConnect.deleteDB("sms");
// influxDB.createDB(database);
}
@Test
public void testInsert() {// 测试数据插入
Map tags = new HashMap();
Map fields = new HashMap();
List list = new ArrayList();
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");
for (int i = 40; i <= 60; i++) {
CodeInfo info1 = new CodeInfo();
info1.setId(i);
info1.setFlag(1);
info1.setName("BANKS");
info1.setCode("ABC");
info1.setDescr("中国农业银行");
info1.setDescrE("ABC");
info1.setTime(new Date().getTime());
info1.setCreatedBy("system");
info1.setCreatedAt(formatter.format(new Date()));
list.add(info1);
}
for (CodeInfo info : list) {
tags.put("TAG_CODE", info.getCode());
tags.put("TAG_NAME", info.getName());
fields.put("ID", info.getId());
fields.put("FLAG", info.getFlag());
fields.put("NAME", info.getName());
fields.put("CODE", info.getCode());
fields.put("DESCR", info.getDescr());
fields.put("DESCR_E", info.getDescrE());
fields.put("CREATED_BY", info.getCreatedBy());
fields.put("CREATED_AT", info.getCreatedAt());
fields.put("TIME", info.getTime());
influxDBConnect.insert(measurement, tags, fields);
}
}
这里我的数据库安装在本地的centos7上,连接地址应该是127.0.0.1:8086没错吧?求大神看看错误出在哪
解决方法:
好了这里是我没想通,Linux上的这个localhost让我误解了
这个实在centos上的localhost,而我的Java代码是在windows上写的,所以两个localhost有区别,将这个localhost换成服务器地址就ok了。