我在C#中使用BIGQUERY
,并尝试通过insert命令将查询保存到临时表。
当我运行命令j.Insert
时,插入是异步完成的(如下面的代码所示),我不能保证数据实际上是完全插入的。
有没有办法检查当前正在运行的作业的状态,以便知道作业何时完成(等待该作业完成)。
以下是我的代码(基于以下示例:Create a table from Select Bigquery
我添加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 ****
答案 0 :(得分:1)
您可以使用PollUntilCompleted()
检查完成情况,而不是Thread.Sleep()
。在你的情况下,它将是:
var resJob = j.Insert(theJob, _settings.ProjetId).Execute();
resJob.PollUntilCompleted();
您可以看到complete sample on Github,但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);
}
在C#的新Google Cloud库中,它可能被称为PollQueryUntilCompleted
based on the documentation。