用于调用 Google Cloud BigQuery Storage Read gRPC API 的类型化接口,以高吞吐量读取表数据。
BigQuery Storage Read API 提供对 BigQuery 表数据的快速、高吞吐量访问。该 gRPC API 通过 google.cloud.bigquery.storage.v1.BigQueryRead 服务公开,在 bigquerystorage.googleapis.com:443 上通过 TLS 访问,主要方法包括 CreateReadSession 和 ReadRows(服务器流式)。
请求通过 TsgcGRPCBigQueryCreateReadSessionRequest 构建,它会设置 Parent (projects/<id>)、ReadSession.Table (projects/<id>/datasets/<ds>/tables/<tbl>)、ReadSession.DataFormat (1 = AVRO, 2 = ARROW) 和 MaxStreamCount。会话响应携带流名称,随后通过服务器流式方法 ReadRows 读取这些流。
下面的示例使用服务账户 JWT 进行身份验证,通过 TsgcHTTP2Client 将 TsgcGRPCClient 连接到 BigQuery Storage 主机,设置授权 Bearer 元数据,并使用 AVRO 数据格式对某个表调用 CreateReadSession:
oHTTP2 := TsgcHTTP2Client.Create(nil);
oHTTP2.Host := 'bigquerystorage.googleapis.com';
oHTTP2.Port := 443;
oHTTP2.TLS := True;
oGRPC := TsgcGRPCClient.Create(nil);
oGRPC.Client := oHTTP2;
// service-account JWT authentication
oGRPC.GoogleCloudOptions.JWT.KeyFile := 'service-account.json';
oGRPC.GoogleCloudOptions.JWT.API_Endpoint := 'https://bigquerystorage.googleapis.com/';
oGRPC.DefaultMetadata.AddValue('authorization', 'Bearer ' + oGRPC.GoogleCloudOptions.JWT.Token);
// build the typed request and call the method
oRequest := TsgcGRPCBigQueryCreateReadSessionRequest.Create;
try
oRequest.Parent := 'projects/my-project-id';
oRequest.ReadSession.Table := 'projects/my-project-id/datasets/my_dataset/tables/my_table';
oRequest.ReadSession.DataFormat := 1; // 1 = AVRO, 2 = ARROW
oRequest.MaxStreamCount := 1;
oResponse := oGRPC.Call('google.cloud.bigquery.storage.v1.BigQueryRead', 'CreateReadSession', oRequest.ToBytes);
ShowMessage(oResponse.DataString);
finally
oRequest.Free;
end;
| 名称 | 描述 |
|---|---|
| CreateReadSession | 在某个表上创建一个读取会话,并返回可以并行读取的流。 |
| ReadRows | 服务器流式方法,用于从读取会话读取某个流的行。 |
示例文件夹 Demos/21.GRPC/16.BigQuery 中提供了一个可用的示例,演示如何进行身份验证、创建读取会话以及流式读取某个表的行。