2013-02-11 61 views
6

合計結果が何千行もあるBigQueryクエリを実行したいのですが、一度に100個の結果のページ(maxResultspageTokenのパラメータを使用)を検索したいだけです。pageTokensをJava用のGoogleクライアントライブラリで使用してページ分割されたBigQueryクエリ結果をリクエストするにはどうすればよいですか?

BigQuery APIは、collection.listメソッドでpageTokenパラメータの使用をサポートしています。しかし、私は非同期クエリを実行しており、getQueryResultメソッドを使用して結果を取得しており、pageTokenパラメータをサポートしていないようです。 getQueryResultspageTokenを使用できますか?

+0

:仕事、クエリの完了をポーリングして、クエリ結果のページの後のページを取得するには、次のようになりますどんな助けも高く評価されます – Innovation

答えて

11

更新:page through list results hereの新しいドキュメントがあります。

開発者が私に個人的にこの質問をしたので、私はこの質問に自己回答しています。私はStack Overflowで回答を共有したいと思います。

pageTokenパラメータは、Tabledata.listメソッドからページ番号の付けられた結果を要求するときに使用できます。たとえば、結果データが100,000行を超える場合や10 MBを超える場合など、結果セットは自動的に改ページされます。また、maxResultsパラメータを明示的に設定することによって、結果のページ付けを要求することもできます。結果の各ページにはpageTokenパラメータが返され、次のページの結果を取得するために使用できます。

すべてのクエリでは、新しいBigQueryテーブルが作成されます。テーブルに明示的に名前を付けると、24時間しか持続しません。ただし、無名の「匿名」テーブルでも識別子があります。どちらの場合も、クエリジョブを挿入した後、新しく作成されたテーブルの名前を取得します。次に、tabledata.listメソッド(およびmaxResults/pageTokenパラメータの組み合わせ)を使用して、結果をページ区切り形式で要求します。ループして、以前に取得したpageTokenを使用してtabledata.listを呼び出して、pageTokensが返されなくなるまで(最後のページに達したことを意味します)

Java用Google APIクライアントライブラリを使用すると、我々はJavaScriptでそれをやろうとしていますし、中に任意のものを得ることはありませんjavascript.Iでそれを処理する方法

// Create a new BigQuery client authorized via OAuth 2.0 protocol 
// See: https://developers.google.com/bigquery/docs/authorization#installed-applications 
Bigquery bigquery = createAuthorizedClient(); 

// Start a Query Job 
String querySql = "SELECT TOP(word, 500), COUNT(*) FROM publicdata:samples.shakespeare"; 
JobReference jobId = startQuery(bigquery, PROJECT_ID, querySql); 

// Poll for Query Results, return result output 
TableReference completedJob = checkQueryResults(bigquery, PROJECT_ID, jobId); 

// Return and display the results of the Query Job 
displayQueryResults(bigquery, completedJob); 

/** 
* Inserts a Query Job for a particular query 
*/ 
public static JobReference startQuery(Bigquery bigquery, String projectId, 
             String querySql) throws IOException { 
    System.out.format("\nInserting Query Job: %s\n", querySql); 

    Job job = new Job(); 
    JobConfiguration config = new JobConfiguration(); 
    JobConfigurationQuery queryConfig = new JobConfigurationQuery(); 
    config.setQuery(queryConfig); 

    job.setConfiguration(config); 
    queryConfig.setQuery(querySql); 

    Insert insert = bigquery.jobs().insert(projectId, job); 
    insert.setProjectId(projectId); 
    JobReference jobId = insert.execute().getJobReference(); 

    System.out.format("\nJob ID of Query Job is: %s\n", jobId.getJobId()); 

    return jobId; 
} 

/** 
* Polls the status of a BigQuery job, returns TableReference to results if "DONE" 
*/ 
private static TableReference checkQueryResults(Bigquery bigquery, String projectId, JobReference jobId) 
    throws IOException, InterruptedException { 
    // Variables to keep track of total query time 
    long startTime = System.currentTimeMillis(); 
    long elapsedTime; 

    while (true) { 
    Job pollJob = bigquery.jobs().get(projectId, jobId.getJobId()).execute(); 
    elapsedTime = System.currentTimeMillis() - startTime; 
    System.out.format("Job status (%dms) %s: %s\n", elapsedTime, 
     jobId.getJobId(), pollJob.getStatus().getState()); 
    if (pollJob.getStatus().getState().equals("DONE")) { 
     return pollJob.getConfiguration().getQuery().getDestinationTable(); 
    } 
    // Pause execution for one second before polling job status again, to 
    // reduce unnecessary calls to the BigQUery API and lower overall 
    // application bandwidth. 
    Thread.sleep(1000); 
    } 
} 

/** 
* Page through the result set 
*/ 
private static void displayQueryResults(Bigquery bigquery, 
             TableReference completedJob) throws IOException { 

    long maxResults = 20; 
    String pageToken = null; 
    int page = 1; 

    // Default to not looping 
    boolean moreResults = false; 

    do { 
    TableDataList queryResult = bigquery.tabledata().list(
      completedJob.getProjectId(), 
      completedJob.getDatasetId(), 
      completedJob.getTableId()) 
       .setMaxResults(maxResults) 
       .setPageToken(pageToken) 
     .execute(); 
    List<TableRow> rows = queryResult.getRows(); 
    System.out.print("\nQuery Results, Page #" + page + ":\n------------\n"); 
    for (TableRow row : rows) { 
     for (TableCell field : row.getF()) { 
     System.out.printf("%-50s", field.getV()); 
     } 
     System.out.println(); 
    } 
    if (queryResult.getPageToken() != null) { 
     pageToken = queryResult.getPageToken(); 
     moreResults = true; 
     page++; 
    } else { 
     moreResults = false; 
    } 
    } while (moreResults); 
} 
+1

は魅力のように働きます! – koma

+1

恐ろしい男:) – Innovation

+1

私の仕事は私に目的地のテーブルを与えませんか? – Tjorriemorrie

関連する問題