【发布时间】:2015-09-30 14:02:14
【问题描述】:
我正在使用 .NET API 将数据流式传输到 BQ。我注意到在 Process Explorer 中,新的 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
【问题讨论】:
标签: c# google-bigquery google-api-dotnet-client