如何使用 influxdb-java.jar 获得没有舍入误差的 "Long" 值?

How to get "Long" values without rounding error using influxdb-java.jar?

我使用influx命令(CLI)写了一个数据如下:

CREATE DATABASE mydb
USE mydb
insert test value=1234567890123456789i

然后,我使用 influx 命令 (CLI) 读取数据

> select * from test
name: test
time                    value
1475129299322514085     1234567890123456789

好的。没问题。

但是当我使用带有 influxdb-java-2 的 JAVA 程序读取数据时。3.jar:

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" 后缀?

我期待的 "value" 是 1234567890123456789,而不是 1234567890123456770 (=1.23456789012345677E18).

如何使用 influxdb-java.jar 获得 "Long" 值而不出现舍入误差?

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

解决方法是转换为 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());

在 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]