如何使用Influxdb-java.jar获取“Long”值而不会出现舍入错误?

时间:2016-09-29 15:56:00

标签: java influxdb

我使用涌入命令(CLI)编写了一个数据,如下所示:

CREATE DATABASE mydb
USE mydb
insert test value=1234567890123456789i

然后,我使用涌入命令(CLI)

读取数据
> select * from test
name: test
time                    value
1475129299322514085     1234567890123456789

行。没问题。

但是当我使用带有Influxdb-java-2.3.jar的JAVA程序读取数据时:

public static void main(String[] args) {

    InfluxDB conn = InfluxDBFactory.connect(DB_URL, DB_USER, DB_PASSWD);
    String sql =  "SELECT time,value from test";
    Query query = new Query(sql, DB_NAME);
    QueryResult result = conn.query(query, TimeUnit.NANOSECONDS);
    Series series = result.getResults().get(0).getSeries().get(0);

    System.out.println(series.getColumns().get(0) + "=" + series.getValues().get(0).get(0) + " " + series.getValues().get(0).get(0).getClass().getName());
    System.out.println(series.getColumns().get(1) + "=" + series.getValues().get(0).get(1) + " " + series.getValues().get(0).get(1).getClass().getName());
}

结果是:

time=1.47512929932251418E18 java.lang.Double
value=1.23456789012345677E18 java.lang.Double

为什么“time”“Double”中返回,即使查询选项是NANOSECONDS?

为什么“value”“Double”中返回,即使数据是以“i”后缀插入的?

我期待的“价值”是 1234567890123456789 ,而不是 1234567890123456770 (= 1.23456789012345677E18)

使用Influxdb-java.jar,如何在不使用舍入错误的情况下获取“长”值?

2 个答案:

答案 0 :(得分:0)

不幸的是,没有适合您的解决方案。这个库就是这样设计的。

解决方法是转换为long

System.out.println(series.getColumns().get(0) + "=" + (long)(double)series.getValues().get(0).get(0) + " " + series.getValues().get(0).get(0).getClass().getName());

System.out.println(series.getColumns().get(1) + "=" + series.getValues().get(0).get(1).longValue() + " " + series.getValues().get(0).get(1).getClass().getName());

答案 1 :(得分:0)

在influxdb-java-2.15.jar上使用MSGPACK格式

OkHttpClient.Builder client = new OkHttpClient.Builder();
InfluxDB jsonInfluxDB = InfluxDBFactory.connect(URL, USER, PASSWORD, client, ResponseFormat.JSON);
InfluxDB msgpackInfluxDB = InfluxDBFactory.connect(URL, USER, PASSWORD, client, ResponseFormat.MSGPACK);
//influxDB.setLogLevel(InfluxDB.LogLevel.FULL);
Query query = new Query("SELECT * FROM xxx LIMIT 1", "XXX");

QueryResult jsonResult = jsonInfluxDB.query(query, TimeUnit.NANOSECONDS);
logger.info("{}", jsonResult);

QueryResult msgpackResult = msgpackInfluxDB.query(query, TimeUnit.NANOSECONDS);
logger.info("{}", msgpackResult);

结果

INFO test - QueryResult [results=[Result [series=[Series [name=mib, tags=null, columns=[time, commodity, country, expiration, high, indicator, instrument, last, low, market, modifier, open, strike, turnover, volume], values=[[1.56937620100001485E18, 300, 96, 20191213, 1.621E7, =, 11, 1.6145E7, 1.592E7, 60, 0, 1.6E7, 0, 55640.0, 2.0]]]], error=null]], error=null]
INFO test - QueryResult [results=[Result [series=[Series [name=mib, tags=null, columns=[time, commodity, country, expiration, high, indicator, instrument, last, low, market, modifier, open, strike, turnover, volume], values=[[1569376201000014846, 300, 96, 20191213, 16210000, =, 11, 16145000, 15920000, 60, 0, 16000000, 0, 55640, 2]]]], error=null]], error=null]