我正在使用.NET API将数据流到BQ中。我在Process中注意到,新的TCP/IP连接是一次又一次地创建和结束的。我想知道是否有可能重用连接,避免创建和结束连接的大开销?
public async Task InsertAsync(BaseBigQueryTable table, IList<IDictionary<string, object>> rowList, GetBqInsertIdFunction getInsert,CancellationToken ct)
{
if (rowList.Count == 0)
{
return;
}
string tableId = table.TableId;
IList<TableDataInsertAllRequest.RowsData> requestRows = rowList.Select(row => new TableDataInsertAllRequest.RowsData {Json = row,InsertId = getInsert(row)}).ToList();
TableDataInsertAllRequest request = new TableDataInsertAllRequest { Rows = requestRows };
bool needCreateTable = false;
BigqueryService bqService = null;
try
{
bqService = GetBigQueryService();
TableDataInsertAllResponse response =
await
bqService.Tabledata.InsertAll(request, _account.ProjectId, table.DataSetId, tableId)
.ExecuteAsync(ct);
IList<TableDataInsertAllResponse.InsertErrorsData> insertErrors = response.InsertErrors;
if (insertErrors != null && insertErrors.Count > 0)
{
//handling errors, removed for easier reading..
}
}catch{
//... removed for easier reading
}
finally
{
if (bqService != null)
bqService.Dispose();
}
}
private BigqueryService GetBigQueryService()
{
return new BigqueryService(new BaseClientService.Initializer
{
HttpClientInitializer = _credential,
ApplicationName = _applicationName,
});
}** 跟踪 **
下面给出的答案似乎是减少http连接的唯一解决方案。然而,我发现在大量的实时数据流上使用批处理请求可能有一些限制。参见我对此的另一个问题:Google API BatchRequest: An established connection was aborted by the software in your host machine
发布于 2015-09-30 19:28:20
下面的链接文档说明了如何对API调用进行批处理,以减少客户端必须进行的HTTP连接的数量
https://cloud.google.com/bigquery/batch
批处理请求发出后,您可以得到响应并解析出所有涉及到的工作。作为另一种选择,您可以为每个内部请求在批处理请求中预置作业。注意:你需要确保那些工作是独一无二的。
在此之后,您可以通过jobs.get https://cloud.google.com/bigquery/docs/reference/v2/jobs/get检查每个作业的运行情况。
https://stackoverflow.com/questions/32868134
复制相似问题