Search code examples
pythongoogle-cloud-platformgoogle-cloud-spanner

Has anyone queried Google Spanner using StreamedResultSet in Python


I want to extract a huge table from spanner and instead of the regular execute_sql function, I wish to stream data. But I don't see a lot of documentation around it.

I am using google cloud python libraries and I couldn't find a solution either

Has anyone tried it?

import os,sys
import subprocess
import glob
from os import path
from google.cloud.spanner_v1.streamed import StreamedResultSet
def run_quickstart():
    # [START spanner_quickstart]
    # Imports the Google Cloud Client Library.
    from google.cloud import spanner

    # Instantiate a client.
    spanner_client = spanner.Client.from_service_account_json('<stripped Auth Key>')

    # Your Cloud Spanner instance ID.
    instance_id = 'spannereval'

    # Get a Cloud Spanner instance by ID.
    instance = spanner_client.instance(instance_id)

    # Your Cloud Spanner database ID.
    database_id = 'ias-ei-test'

    # Get a Cloud Spanner database by ID.
    database = instance.database(database_id)

    # Execute a simple SQL statement.
    with database.snapshot() as snapshot:
       results = snapshot.execute_sql('SELECT * from product_inventory_status')
       for row in results:
            print(row)
    # [END spanner_quickstart]

Solution

  • The code you have is streaming under the hood. See StreamedResultSet returned from execute_sql here: https://github.com/GoogleCloudPlatform/google-cloud-python/blob/master/spanner/google/cloud/spanner_v1/snapshot.py#L144