Search code examples
google-cloud-platformgoogle-cloud-datastore

Is there a way to update several entities using the google cloud console?


In google cloud console, I can use "Datastore > Entities" to update one entity at a time, or make SELECT queries using GQL.

I need to update several entities and it would be impractical to do it one by one. Is there a way to do an update query or something similar to the "Interactive console" that exists in the "admin server" when I running my project in the local GAE server?

EDIT: Just for clarification, I want to do this without needing to deploy new code in my production server.


Solution

  • As noted by sergio franco, it's possible to use Cloud Functions to access the datastore and update the entities. It's still needed to deploy the function, but since it can all be done from the console, it's considerably simpler and faster than adding new code to the project and deploying the whole project.

    It's important to remember that you'll be priced for each time the function in executed, no matter the size fo the function. More details here

    • Go to the google cloud console and select "Cloud Functions" from the menu on the left, it's on the Compute section:

    google cloud console menu showing Cloud Functions

    • Choose "Create Function"
    • Give the function a descriptive name (it will be easier to know what it does later)
    • Choose 128Mb (you'll be charged depending on memory allocation)
    • Choose HTTP as the Trigger
    • Since the purpose of this function is to update the datastore, I think it's best to leave unchecked the "Allow unauthenticated invocations"
    • Choose "inline editor"
    • Choose Python 3.7 as the runtime (since the examples below will be in Python)
    • Add the code below in the "requirements.txt" tab:
    # Function dependencies, for example:
    # package>=version
    google-cloud-datastore==1.8.0
    
    • main.py tab:
    from io import StringIO
    from google.cloud import datastore
    datastore_client = datastore.Client()
    
    def update_books(request):
        buffer = StringIO()
        query = datastore_client.query(kind='Book')
        query.add_filter('author', '=', 'Terry Pratchett')
    
        data = query.fetch()
    
        for book in data:
            book['rating'] = '5 stars'
            client.put(book)
            buffer.write('Updated book {}'.format(book['title']))
    
        return buffer.getvalue()
    
    • Click on "Environment Variables, Network and More"
    • In Networking, choose "Allow internal traffic only"
    • Press "Create"
    • Wait for the function to be deployed
    • Go to the "Testing" tab
    • Press "Test Function"

    Anything returned by the function, will be printed in the "Output" section of the page.

    Anything called with print will be sent to the logs

    You can access all the entities properties as if it was a dict.

    Sources:

    cloud functions python to access Datastore

    https://googleapis.dev/python/datastore/latest/entities.html

    https://cloud.google.com/functions/docs/quickstart-python