Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get integer data from InfluxDB #153

Closed
aurrelhebert opened this issue Mar 3, 2016 · 9 comments
Closed

Get integer data from InfluxDB #153

aurrelhebert opened this issue Mar 3, 2016 · 9 comments

Comments

@aurrelhebert
Copy link

Hello,
I have a field column that I pushed on influxDB using integer type ( value = 1i ). When I query this specific column I always get a double ( 1.0 ) as a result.
I am using this specific result :

QueryResult queryResult = myInfluxDB.query(myQuery);

@jiafu1115
Copy link
Contributor

jiafu1115 commented Nov 10, 2016

@aurrelhebert @majst01 What's 1i?

@aurrelhebert
Copy link
Author

Hello @jiafu1115,
Check the data type documentation of InfluxDB.
When you pushed numbers on InfluxDB, they are systematically converted to "Float", if you prefer to store integer, you need to push your point by adding "i" at the end of your number.
When I query my column containing integer number with this java client, I loose the information that my number is an Integer and always get a float.

@jiafu1115
Copy link
Contributor

@aurrelhebert Many thanks to you! Can you give the detailed code for how you write and how you query?

@aurrelhebert
Copy link
Author

aurrelhebert commented Nov 10, 2016

As it's indicated in my first comment, I pushed Integer value using the write protocol of Influx. When requesting Influx directly I could get Integer values but when requesting them with the java client it was only float.
The request was simply done like this with my client:

Query query = new Query(queryText, dbName);
QueryResult result = influxDB.query(query);

As this issue is quite old, I am not using an integer column anymore in my data base. I can re-setup a local influx, if you want more details about it.

@jiafu1115
Copy link
Contributor

@aurrelhebert I will test it to avoid your effort to re-setup it. waiting for my feedback.

@aurrelhebert
Copy link
Author

Thanks

@jiafu1115
Copy link
Contributor

jiafu1115 commented Nov 10, 2016

@aurrelhebert

I figure out the root cause:

in short, it is not bug for influxdb-java. the root cause is in dependency jar: moshi.
no matter we use gson or moshi to parse json, it both parse 1 to 1.0. So you see 1.0.
square/moshi#192 told us it may be standard or common issue for json parse due to we doesn't know 1 is integer or double. So we can't fix it in influxdb-java due to gson and moshi both doesn't acted as you expected. What's more, I am also surprised for this.

my test code:

    public static class JsonDemo{
        private Object integer;
        private String string;
        public Object getInteger() {
            return integer;
        }
        public void setInteger(Object integer) {
            this.integer = integer;
        }
        public String getString() {
            return string;
        }
        public void setString(String string) {
            this.string = string;
        }
        @Override
        public String toString() {
            return "JsonDemo [integer=" + integer + ", string=" + string + "]";
        }


    }
         Gson gson = new Gson();
         JsonDemo element = gson.fromJson ("{\"integer\":1,\"string\":\"my\"}", JsonDemo.class);
         System.out.println(element);

JsonDemo [integer=1.0, string=my]

            JsonDemo jsonDemo = new JsonDemo();
            jsonDemo.setInteger(1);
            jsonDemo.setString("my");

            Moshi moshi = new Moshi.Builder().build();
            JsonAdapter<JsonDemo> jsonAdapter = moshi.adapter(JsonDemo.class);

            String json = jsonAdapter.toJson(jsonDemo);
            System.out.println(json);

            //{"integer":1,"string":"my"}

            JsonDemo jsonDemo2 = jsonAdapter.fromJson(json);
            System.out.println(jsonDemo2);

            //JsonDemo [integer=1.0, string=my]] 

@majst01 So you can close this issue just now!

@aurrelhebert
Copy link
Author

Thanks for the investigation.

@IronsDu
Copy link

IronsDu commented May 21, 2021

Hi, How to fix this problem by myself?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants