【问题标题】:How to get "Long" values without rounding error using influxdb-java.jar?如何使用 influxdb-java.jar 获得“长”值而不会出现舍入错误?
【发布时间】:2016-09-29 15:56:00
【问题描述】:

我使用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.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

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

为什么插入了“i”后缀的数据却在“Double”中返回了“value”

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

如何使用 influxdb-java.jar 获得“长”值而不会出现舍入错误?

【问题讨论】:

标签: java influxdb


【解决方案1】:

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

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]

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 我向项目团队提出了一个建议。仅这个项目就有 393 个未解决的问题......我想我找到了我的下一个 GitHub 项目
    • 不能转换为 Double to long (java.lang.ClassCastException)
    • ((Number)series.getValues().get(0).get(1)).longValue() =1234567890123456768(不是 1234567890123456789)
    猜你喜欢
    • 2020-12-27
    • 2020-10-18
    • 2014-06-15
    • 1970-01-01
    • 1970-01-01
    • 2016-10-27
    • 1970-01-01
    • 1970-01-01
    • 2020-03-03
    相关资源
    最近更新 更多