Search code examples
pythongoogle-cloud-platformgoogle-cloud-datastore

Writing a python script to fetch content of a datastore entity in GCP


I am trying to fetch a column of a datastore entity in Google Cloud Platform. I can do it by writing a GQL query from the console, but I want to do the same using a python script and use the same GQL query and run it through the cloudshell of GCP. Here is a table similar to the one used in datastore :

The table named Person similar to datastore entity

I have to find out the names whose Salary is more than 280000. I wrote a GQL query in the console :

SELECT Name from Person where Salary>280000 ;

I got the result, but I want to do the same using Python script which could run in the cloudshell.


Solution

  • Its pretty simple.

    from google.cloud import datastore
    client = datastore.Client()
    query = client.query(kind='Kindname')
    query = query.add_filter('Salary', '>', 280000)
    l = query.fetch()
    l = list(l)
    if not l:
        print("No result is returned")
    else:  
        d = dict(l[0])
        print(d['name'])