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

BigQuery - Check state of job after calling Insert method

我在 C# 中使用 BIGQUERY,并尝试通过插入命令将查询保存到临时 table。

当我运行命令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 ****

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

var resJob = j.Insert(theJob, _settings.ProjetId).Execute();
resJob.PollUntilCompleted();

可以看到complete sample on Github, but the relevant part linked from Querying Data是:

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);
}

看起来在新的 Google C# 云库中,它可能被称为 PollQueryUntilCompleted based on the documentation