Skip to content

Latest commit

 

History

History
143 lines (102 loc) · 3.87 KB

spanner_gsql.md

File metadata and controls

143 lines (102 loc) · 3.87 KB

Setup and configure Spanner

Before you begin

  1. Make sure you have a Google Cloud project and billing is enabled.

  2. Set your PROJECT_ID environment variable:

    export PROJECT_ID=<YOUR_PROJECT_ID>
  3. Install the gcloud CLI.

  4. Set gcloud project:

    gcloud config set project $PROJECT_ID
  5. Enable APIs:

    gcloud services enable spanner.googleapis.com
  6. Install python and set up a python virtual environment.

  7. Make sure you have python version 3.11+ installed.

    python -V

Create a Cloud Spanner instance

  1. Set environment variables.

    export INSTANCE=my-spanner-gsql-instance
    export DATABASE=assistantdemo
    export REGION=regional-us-central1
    export INSTANCE_DESCRIPTION="My Spanner GSQL Instance"
  2. Create a Cloud Spanner instance:

    gcloud spanner instances create $INSTANCE \
        --config=$REGION \
        --nodes=1 \
        --description=$INSTANCE_DESCRIPTION
  3. Create a database within the Cloud Spanner instance:

    gcloud spanner databases create $DATABASE --instance=$INSTANCE
  4. Verify the database created with the gcloud tool:

    gcloud spanner databases execute-sql $DATABASE \
        --instance=$INSTANCE \
        --sql="SELECT 1"

Create a Service Account

  1. Set environment variables.

    export SA_NAME=spanner-service
    export SA_EMAIL=$SA_NAME@$PROJECT_ID.iam.gserviceaccount.com
  2. Create a Service Account: Use the gcloud iam service-accounts create command to create a new service account.

    gcloud iam service-accounts create $SA_NAME --description="Service account for Cloud Spanner" --display-name="Cloud Spanner Service Account"
  3. Grant Required Permissions: Assign the necessary roles to the service account. For Cloud Spanner read and write access, you can grant the roles/spanner.databaseUser and roles/spanner.databaseAdmin roles. Use the gcloud projects add-iam-policy-binding command to grant these roles.

    gcloud projects add-iam-policy-binding $PROJECT_ID --member="serviceAccount:$SA_EMAIL" --role="roles/spanner.databaseUser" --condition=None
    gcloud projects add-iam-policy-binding $PROJECT_ID --member="serviceAccount:$SA_EMAIL" --role="roles/spanner.databaseAdmin" --condition=None
  4. Generate a key file for the service account. This key file will be used for authentication when accessing GCP resources programmatically.

    gcloud iam service-accounts keys create key.json --iam-account $SA_EMAIL
  5. Use the generated key file (key.json) to authenticate your application when accessing Cloud Spanner.

Initialize data

  1. Change into the retrieval service directory:

    cd genai-databases-retrieval-app/retrieval_service
  2. Install requirements:

    pip install -r requirements.txt
  3. Make a copy of example-config.yml and name it config.yml.

    cp example-config.yml config.yml
  4. Update config.yml with your database information.

      host: 0.0.0.0
      datastore:
        # Example for Spanner
        kind: "spanner-gsql"
        project: <YOUR_PROJECT_ID>
        instance: my-spanner-gsql-instance
        database: assistantdemo
        service_account_key_file: <PATH_TO_SERVICE_ACCOUNT_KEY_FILE>
  5. Populate data into database:

    python run_database_init.py

Clean up resources

Clean up after completing the demo.

  1. Delete the Cloud Spanner instance:

    gcloud spanner instances delete $INSTANCE