Airflow Summit 2025 is coming October 07-09. Register now to secure your spot!

airflow.providers.google.cloud.hooks.spanner

This module contains a Google Cloud Spanner Hook.

Classes

SpannerConnectionParams

Information about Google Spanner connection parameters.

SpannerHook

Hook for Google Cloud Spanner APIs.

Module Contents

class airflow.providers.google.cloud.hooks.spanner.SpannerConnectionParams[source]

Bases: NamedTuple

Information about Google Spanner connection parameters.

project_id: str | None[source]
instance_id: str | None[source]
database_id: str | None[source]
class airflow.providers.google.cloud.hooks.spanner.SpannerHook(gcp_conn_id='google_cloud_default', impersonation_chain=None, **kwargs)[source]

Bases: airflow.providers.google.common.hooks.base_google.GoogleBaseHook, airflow.providers.common.sql.hooks.sql.DbApiHook

Hook for Google Cloud Spanner APIs.

All the methods in the hook where project_id is used must be called with keyword arguments rather than positional.

conn_name_attr = 'gcp_conn_id'[source]
default_conn_name = 'google_cloud_spanner_default'[source]
conn_type = 'gcpspanner'[source]
hook_name = 'Google Cloud Spanner'[source]
get_uri()[source]

Override DbApiHook get_uri method for get_sqlalchemy_engine().

get_sqlalchemy_engine(engine_kwargs=None)[source]

Get an sqlalchemy_engine object.

Parameters:

engine_kwargs – Kwargs used in create_engine().

Returns:

the created engine.

get_instance(instance_id, project_id)[source]

Get information about a particular instance.

Parameters:
  • project_id (str) – Optional, The ID of the Google Cloud project that owns the Cloud Spanner database. If set to None or missing, the default project_id from the Google Cloud connection is used.

  • instance_id (str) – The ID of the Cloud Spanner instance.

Returns:

Spanner instance

Return type:

google.cloud.spanner_v1.instance.Instance | None

create_instance(instance_id, configuration_name, node_count, display_name, project_id)[source]

Create a new Cloud Spanner instance.

Parameters:
  • instance_id (str) – The ID of the Cloud Spanner instance.

  • configuration_name (str) – The name of the instance configuration defining how the instance will be created. Possible configuration values can be retrieved via https://cloud.google.com/spanner/docs/reference/rest/v1/projects.instanceConfigs/list

  • node_count (int) – (Optional) The number of nodes allocated to the Cloud Spanner instance.

  • display_name (str) – (Optional) The display name for the instance in the Google Cloud Console. Must be between 4 and 30 characters. If this value is not passed, the name falls back to the instance ID.

  • project_id (str) – Optional, the ID of the Google Cloud project that owns the Cloud Spanner database. If set to None or missing, the default project_id from the Google Cloud connection is used.

Returns:

None

Return type:

None

update_instance(instance_id, configuration_name, node_count, display_name, project_id)[source]

Update an existing Cloud Spanner instance.

Parameters:
  • instance_id (str) – The ID of the Cloud Spanner instance.

  • configuration_name (str) – The name of the instance configuration defining how the instance will be created. Possible configuration values can be retrieved via https://cloud.google.com/spanner/docs/reference/rest/v1/projects.instanceConfigs/list

  • node_count (int) – (Optional) The number of nodes allocated to the Cloud Spanner instance.

  • display_name (str) – (Optional) The display name for the instance in the Google Cloud Console. Must be between 4 and 30 characters. If this value is not set in the constructor, the name falls back to the instance ID.

  • project_id (str) – Optional, the ID of the Google Cloud project that owns the Cloud Spanner database. If set to None or missing, the default project_id from the Google Cloud connection is used.

Returns:

None

Return type:

None

delete_instance(instance_id, project_id)[source]

Delete an existing Cloud Spanner instance.

Parameters:
  • instance_id (str) – The ID of the Cloud Spanner instance.

  • project_id (str) – Optional, the ID of the Google Cloud project that owns the Cloud Spanner database. If set to None or missing, the default project_id from the Google Cloud connection is used.

Returns:

None

Return type:

None

get_database(instance_id, database_id, project_id)[source]

Retrieve a database in Cloud Spanner; return None if the database does not exist in the instance.

Parameters:
  • instance_id (str) – The ID of the Cloud Spanner instance.

  • database_id (str) – The ID of the database in Cloud Spanner.

  • project_id (str) – Optional, the ID of the Google Cloud project that owns the Cloud Spanner database. If set to None or missing, the default project_id from the Google Cloud connection is used.

Returns:

Database object or None if database does not exist

Return type:

google.cloud.spanner_v1.database.Database | None

create_database(instance_id, database_id, ddl_statements, project_id)[source]

Create a new database in Cloud Spanner.

Parameters:
  • instance_id (str) – The ID of the Cloud Spanner instance.

  • database_id (str) – The ID of the database to create in Cloud Spanner.

  • ddl_statements (list[str]) – The string list containing DDL for the new database.

  • project_id (str) – Optional, the ID of the Google Cloud project that owns the Cloud Spanner database. If set to None or missing, the default project_id from the Google Cloud connection is used.

Returns:

None

Return type:

None

update_database(instance_id, database_id, ddl_statements, project_id, operation_id=None)[source]

Update DDL of a database in Cloud Spanner.

Parameters:
  • instance_id (str) – The ID of the Cloud Spanner instance.

  • database_id (str) – The ID of the database in Cloud Spanner.

  • ddl_statements (list[str]) – The string list containing DDL for the new database.

  • project_id (str) – Optional, the ID of the Google Cloud project that owns the Cloud Spanner database. If set to None or missing, the default project_id from the Google Cloud connection is used.

  • operation_id (str | None) – (Optional) The unique per database operation ID that can be specified to implement idempotency check.

Returns:

None

Return type:

None

delete_database(instance_id, database_id, project_id)[source]

Drop a database in Cloud Spanner.

Parameters:
  • instance_id (str) – The ID of the Cloud Spanner instance.

  • database_id – The ID of the database in Cloud Spanner.

  • project_id (str) – Optional, the ID of the Google Cloud project that owns the Cloud Spanner database. If set to None or missing, the default project_id from the Google Cloud connection is used.

Returns:

True if everything succeeded

Return type:

bool

execute_dml(instance_id, database_id, queries, project_id)[source]

Execute an arbitrary DML query (INSERT, UPDATE, DELETE).

Parameters:
  • instance_id (str) – The ID of the Cloud Spanner instance.

  • database_id (str) – The ID of the database in Cloud Spanner.

  • queries (list[str]) – The queries to execute.

  • project_id (str) – Optional, the ID of the Google Cloud project that owns the Cloud Spanner database. If set to None or missing, the default project_id from the Google Cloud connection is used.

Returns:

list of numbers of affected rows by DML query

Return type:

list[int]

get_openlineage_database_dialect(connection)[source]

Return database dialect for OpenLineage.

get_openlineage_database_info(connection)[source]

Return Spanner specific information for OpenLineage.

get_openlineage_default_schema()[source]

Spanner expose ‘public’ or ‘’ schema depending on dialect(Postgres vs GoogleSQL).

SQLAlchemy dialect for Spanner does not expose default schema, so we return None to follow the same approach.

Was this entry helpful?