Missing point about querying schema

Hello there !

I’m new to influxdb and trying to make a small and very classic REST to influxDB prototype. I’m using the python client to connect a local influxdb instance since I have to use FastAPI. I think I understand the basics but when it comes to composed object I feel like I am missing something when querying.

Let’s assume I create a GPS record with two fields which are latitude and longitude, and an id.
I am currently adding a pair of gps coordinates using something like

    point = Point("gps") \
            .tag("id", gps.id) \
            .field("latitude", gps.latitude_value) \
            .field("longitude", gps.longitude_value)
    database.write_api.write(database.bucket, database.org, point)

So far so good ! But when it comes to querying the datas, it gets weird to me. I’ll use the following query of code to get the coordinates back :

from(bucket: "example-bucket")
    |> range(start: -15m)
    |> filter(f'(r) => r._measurement == "gps"')

and then printing the result using this code (I voluntary shorten it a bit for readability, you’ll get what result is)

for table in result:
        for record in table.records:
            results_array.append((record.get_time(), record.get_field(), record.get_value()))

return results_array

But when printing this result back I get one object for each latitude and each longitude. So each pair of coordinates comes back as two separated objects, and i would have liked to get them both in the same one.

I’ve find a solution to do so by querying two times the coordinates, filtering each time on the field name and then making a join, but I can’t believe I am not missing something, because this would very badly apply to larger objects.

Any help please ? Is my way of writing / reading wrong ?

Many thanks !
Kevin.

Hello @Kevin_Heirich,
Are you familiar with Pandas?
That’s my prefered way to work with

https://pandas.pydata.org/

I think it’s the easiest way to use the results from the client. I don’t like to use the client any other way :stuck_out_tongue: but I’m also just a huge pandas fan.
See this for an example:

(PRO TIP: always add |> pivot(rowKey:[“_time”], columnKey: [“cpu”], valueColumn: “_value”) to the end of your query when converting to a Pandas DataFrame. It will pivot your data from the separate _field _value columns to the traditional relational table shape which I think is easier to work with)

Otherwise can you please try executing two queries where you filter for each field independently?

from(bucket: "example-bucket")
    |> range(start: -15m)
    |> filter(f(r) => r._measurement == "gps")
    |> filter(f(r) => r._field == "latitude")

etc…

Thank you

Hey there !

Sorry for the late answer.
This has been very helping together with the influxdb university.

My main problem was my misunderstanding of the table flux that result after the queries and how many instruction as pivot or group could help get the result I want on the query-side instead of having to work on it on the result in the API or the client getting the answer.

Panda on the other hand seem very appropriate to work the results afterwards.

Thanks you very much ! :slight_smile:

Kevin.

1 Like

@Kevin_Heirich,
Of course! I’m glad it’s easy to work with now.
I hope you have a great week.