【问题标题】:How to reuse TCP connections with .NET BigQuery API?如何使用 .NET BigQuery API 重用 TCP 连接?
【发布时间】: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


    【解决方案1】:

    以下链接记录了如何将 API 调用批处理以减少客户端必须建立的 HTTP 连接数

    https://cloud.google.com/bigquery/batch

    批处理请求发出后,您可以得到响应并解析出所有涉及的jobid。作为替代方案,您可以为每个内部请求在批处理请求中预设作业 ID。注意:您需要确保这些 jobid 是唯一的

    之后,您可以通过 jobs.get https://cloud.google.com/bigquery/docs/reference/v2/jobs/get 查看每个作业的情况

    【讨论】:

    • 谢谢!在这里我找到了一个示例代码developers.google.com/api-client-library/dotnet/guide/batch 但是,如何从 await batchRequest.ExecuteAsync() 获得每个请求的详细响应甚至错误?
    • 理想情况下,您应该发布新问题 :) 并在这篇文章中接受答案——当然如果它被接受了
    • 嗯,谢谢你的回答,我会投赞成票,因为它肯定有助于我的部分问题。但是人们从问答中得到了什么,基本上就是解决他们的问题。就我而言,我当然想找到一种行之有效的方法。但是你的建议让我觉得你可能知道答案,但只是想把它放在另一个问题上,并在另一个新接受的答案上赢得声誉。如果是这样,我认为社区不应该这样工作。
    • 我会在我之前的回答中添加我的想法。我问你关于发布新问题的问题,因为这是我看到其他(看起来像成熟的交流用户)建议的方式。没有违反规则的意思
    • 感谢您的想法。老实说,我不知道那里有什么规则。从我的角度来看,一个好的社区专注于我们正在处理的问题。规则服务于我们的目标并使事情变得合理和方便。我在 stackoverflow 中看到了很多帖子,人们通过在 cmets 和新答案中来回讨论来不断构建一个主题。它有助于加深人们的洞察力。这就是我支持的方式:)
    猜你喜欢
    • 2023-03-21
    • 2010-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多