2

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.

2 Answers 2

3

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'])
Sign up to request clarification or add additional context in comments.

Comments

0

What you are looking for is the Python client library for Cloud Datastore.

You can use it to query data from a Python script, in your example it would be something similar to this:

from google.cloud import datastore
client = datastore.Client()
query = client.query(kind='Person')
query = query.add_filter('Salary', '>', 280000)

To learn more, I recommend you to take a look at the documentation for queries and on how to set up authentication.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.