2017-01-10 3 views
0

C#でBIGQUERYを使用していて、insertコマンドでテンポラリテーブルにクエリを保存しようとしています。BigQuery - Insertメソッドを呼び出すとジョブの状態を確認します

私がコマンドj.Insertを実行しているとき、挿入は非同期的に(次のコードのように)行われます。データが実際に完全に挿入されることは保証できません。

現在実行中のジョブの状態をチェックする方法はありますか?ジョブが終了したかどうかを知るために(ジョブが完了するまで待ちます)。ここで

は、上の例に基づいて(私のコードです:Create a table from Select Bigquery

私は、ジョブが終了したことをguartanteeするために、スリープ(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 **** 

答えて

1

。あなたは完成ではなくThread.Sleep()をチェックするためにPollUntilCompleted()を使用することができますあなたのケースでは、それは次のようになります。

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

ますC 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クラウドライブラリに、それはPollQueryUntilCompletedbased on the documentationと呼ばれるかもしれないように見えます。

+0

新しいライブラリですか?私は1か月も経たないうちに大きなクエリの問題を調査しています。 私はそれをチェックします。私はv2を使用していますが、PollQueryUntilCompleted関数はありません。 あなたの答えはかなり良いです - 私はあなたが与えたリンクを掘る必要があります。 ありがとうございます。 – Eitan

関連する問題