Asynchronous BigQuery-调用Insert方法后检查作业的状态

Asynchronous BigQuery-调用Insert方法后检查作业的状态,asynchronous,google-bigquery,temp-tables,Asynchronous,Google Bigquery,Temp Tables,我正在C#中使用BIGQUERY,并尝试通过insert命令将查询保存到临时表 当我运行命令j.Insert时,插入是异步完成的(如下代码所示),我不能保证数据实际上是完全插入的 有没有办法检查当前正在运行的作业的状态,以便知道作业何时完成或未完成(等待作业完成) 以下是我的代码(基于以下示例): 我添加sleep(5000)是为了保证任务已经完成,但这是一个确定性的解决方案 // _bigQueryService is of type: BigQueryServ

我正在C#中使用
BIGQUERY
,并尝试通过insert命令将查询保存到临时表

当我运行命令
j.Insert
时,插入是异步完成的(如下代码所示),我不能保证数据实际上是完全插入的

有没有办法检查当前正在运行的作业的状态,以便知道作业何时完成或未完成(等待作业完成)

以下是我的代码(基于以下示例):

我添加sleep(5000)是为了保证任务已经完成,但这是一个确定性的解决方案

                // _bigQueryService is of type: BigQueryService.
                // query is the main query (select ...)
                JobsResource j = _bigQueryService.Jobs;

            Job theJob = new Job();

            DateTime n = DateTime.UtcNow;

            TableReference _destTempTable = new TableReference
            {
                ProjectId = "myprojectid",
                DatasetId = "myDataSetId",
                TableId = "myTempTable")
            };
            theJob.Configuration = new JobConfiguration()
            {
                Query = new JobConfigurationQuery()
                {
                    AllowLargeResults = true,
                    CreateDisposition =  "CREATE_IF_NEEDED", /* CREATE_IF_NEEDED, CREATE_NEVER */
                    DefaultDataset = "myDefaultDataSet",
                    MaximumBillingTier = 100,
                    DestinationTable = _destTempTable,
                    Query = query,
                }
            };
            var resJob = j.Insert(theJob, _settings.ProjetId).Execute();
            Thread.Sleep(5000); // *** I need better solution instead this line ****

您可以使用
polluntillcompleted()
来检查是否完成,而不是
Thread.Sleep()
。在您的情况下,它应该是:

var resJob = j.Insert(theJob, _settings.ProjetId).Execute();
resJob.PollUntilCompleted();
您可以看到,但链接的相关部分是:


它看起来像是在新的谷歌云库C#中,它可能被称为
pollQueryUnitCompleted

新库?我正在调查一个月内的重大查询问题。我会检查一下。我使用的是v2,但没有pollQueryUnitCompleted函数。你的答案很好-我需要挖掘你提供的链接。谢谢。请注意,在运行了
作业.PollUntilCompleted()
之后,job.Status.State仍在“运行”,但作业确实已完成。您需要添加以下内容:var result=job.PollUntilCompleted();
public BigQueryResults AsyncQuery(string projectId, string datasetId, string tableId,
    string query, BigQueryClient client)
{
    var table = client.GetTable(projectId, datasetId, tableId);
    BigQueryJob job = client.CreateQueryJob(query,
        new CreateQueryJobOptions { UseQueryCache = false });

    // Wait for the job to complete.
    job.PollUntilCompleted();

    // Then we can fetch the results, either via the job or by accessing
    // the destination table.
    return client.GetQueryResults(job.Reference.JobId);
}