创建 BigQuery 读取会话

创建新的读取会话,以指定要从中读取数据的 BigQuery 表、要投影的列以及可选的行过滤条件。

代码示例

Node.js

试用此示例之前,请按照 BigQuery 快速入门:使用客户端库中的 Node.js 设置说明进行操作。 如需了解详情,请参阅 BigQuery Node.js API 参考文档

如需向 BigQuery 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为客户端库设置身份验证

const {BigQueryReadClient} = require('@google-cloud/bigquery-storage');
const {status} = require('@grpc/grpc-js');

const client = new BigQueryReadClient();

/**
 * Creates a read session for a BigQuery table.
 *
 * @param {string} projectId The project ID to use for billing and quota (e.g., 'my-project-id').
 * @param {string} datasetId The ID of the dataset to read from (e.g., 'my_dataset').
 * @param {string} tableId The ID of the table to read from (e.g., 'my_table').
 */
async function createReadSession(
  projectId, datasetId, tableId
) {
  const parent = `projects/${projectId}`;
  const table = `projects/${projectId}/datasets/${datasetId}/tables/${tableId}`;

  const readOptions = {
    selectedFields: ['field_01', 'field_02'],
    rowRestriction: 'field_03 > 100',
  };

  const request = {
    parent,
    readSession: {
      table,
      dataFormat: 'AVRO',
      readOptions,
    },
    maxStreamCount: 1,
  };

  try {
    const [session] = await client.createReadSession(request);

    console.log(`Read session created: ${session.name}`);
    console.log('Session details:');
    console.log(`  Table: ${session.table}`);
    console.log(`  Data format: ${session.dataFormat}`);
    console.log(`  Estimated row count: ${session.estimatedRowCount}`);

    if (session.streams.length > 0) {
      const streamName = session.streams[0].name;
      console.log(`  First stream: ${streamName}`);
    } else {
      console.log('  No streams found in the session.');
    }

    if (session.avroSchema && session.avroSchema.schema) {
      console.log('  AVRO schema:');
      console.log(`  ${session.avroSchema.schema}`);
    }
  } catch (err) {
    if (err.code === status.NOT_FOUND) {
      console.log(
        `Could not find table ${table}. Please ensure the table exists and you have permission to read it.`,
      );
    } else {
      console.error('Error creating read session:', err);
    }
  }
}

Python

试用此示例之前,请按照 BigQuery 快速入门:使用客户端库中的 Python 设置说明进行操作。 如需了解详情,请参阅 BigQuery Python API 参考文档

如需向 BigQuery 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为客户端库设置身份验证

from google.api_core.exceptions import NotFound
from google.cloud.bigquery_storage_v1 import BigQueryReadClient
from google.cloud.bigquery_storage_v1.types import DataFormat, ReadSession

client = BigQueryReadClient()


def create_read_session(
    project_id: str,
    dataset_id: str,
    table_id: str,
) -> None:
    """Creates a read session for a BigQuery table.

    Args:
        project_id: The project ID that will be billed for the read session.
        dataset_id: The dataset ID of the table to read from.
        table_id: The table ID of the table to read from.
    """

    parent = f"projects/{project_id}"
    table = client.table_path(project_id, dataset_id, table_id)

    # If no fields are specified, all fields will be returned.
    read_options = ReadSession.TableReadOptions(
        selected_fields=["field_01", "field_02"], row_restriction="field_03 > 100"
    )

    read_session = ReadSession(
        table=table,
        data_format=DataFormat.ARROW,
        read_options=read_options,
    )

    try:
        session = client.create_read_session(
            parent=parent,
            read_session=read_session,
            max_stream_count=1,
        )

        print(f"Successfully created read session: {session.name}")
        if session.streams:
            print(f"Stream found: {session.streams[0].name}")
        else:
            print("No streams found in the session.")

    except NotFound:
        print(
            f"Could not find the table '{table}'. Please check that the table exists."
        )

后续步骤

如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅Google Cloud 示例浏览器