名前付きパラメータ タイプと指定されたパラメータ タイプのクエリを実行します。
コードサンプル
Java
このサンプルを試す前に、BigQuery クイックスタート: クライアント ライブラリの使用の Java の設定手順を実施してください。詳細については、BigQuery Java API のリファレンス ドキュメントをご覧ください。
import com.google.cloud.bigquery.BigQuery;
import com.google.cloud.bigquery.BigQueryException;
import com.google.cloud.bigquery.BigQueryOptions;
import com.google.cloud.bigquery.QueryJobConfiguration;
import com.google.cloud.bigquery.QueryParameterValue;
import com.google.cloud.bigquery.StandardSQLTypeName;
import com.google.cloud.bigquery.TableResult;
// Sample to run query with named types parameters.
public class QueryWithNamedTypesParameters {
public static void main(String[] args) {
String[] words = {"and", "is", "the", "moon"};
String corpus = "romeoandjuliet";
Integer wordsCount = 250;
String query =
"SELECT word, word_count"
+ " FROM `bigquery-public-data.samples.shakespeare`"
+ " WHERE word IN UNNEST(@wordList)"
+ " AND corpus = @corpus"
+ " AND word_count >= @minWordCount"
+ " ORDER BY word_count DESC";
queryWithNamedTypesParameters(query, words, corpus, wordsCount);
}
public static void queryWithNamedTypesParameters(
String query, String[] words, String corpus, Integer wordsCount) {
try {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests.
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
QueryParameterValue wordList = QueryParameterValue.array(words, StandardSQLTypeName.STRING);
QueryParameterValue corpusParam = QueryParameterValue.of(corpus, StandardSQLTypeName.STRING);
QueryParameterValue minWordCount =
QueryParameterValue.of(wordsCount, StandardSQLTypeName.INT64);
// Note: Standard SQL is required to use query parameters.
QueryJobConfiguration queryConfig =
QueryJobConfiguration.newBuilder(query)
.addNamedParameter("wordList", wordList)
.addNamedParameter("corpus", corpusParam)
.addNamedParameter("minWordCount", minWordCount)
.build();
TableResult results = bigquery.query(queryConfig);
results
.iterateAll()
.forEach(row -> row.forEach(val -> System.out.printf("%s,", val.toString())));
System.out.println("Query with named types parameters performed successfully.");
} catch (BigQueryException | InterruptedException e) {
System.out.println("Query not performed \n" + e.toString());
}
}
}
Node.js
このサンプルを試す前に、BigQuery クイックスタート: クライアント ライブラリの使用の Node.js の設定手順を実施してください。詳細については、BigQuery Node.js API のリファレンス ドキュメントをご覧ください。
// Import the Google Cloud client library
const {BigQuery} = require('@google-cloud/bigquery');
const bigquery = new BigQuery();
async function queryParamsNamedTypes() {
// Run a query using named query parameters and provided parameter types.
// The SQL query to run
const sqlQuery = `SELECT word, word_count
FROM \`bigquery-public-data.samples.shakespeare\`
WHERE word IN UNNEST(@wordList)
AND corpus = @corpus
AND word_count >= @minWordCount
ORDER BY word_count DESC`;
const queryOptions = {
query: sqlQuery,
params: {
wordList: ['and', 'is', 'the', 'moon'],
corpus: 'romeoandjuliet',
minWordCount: 250,
},
types: {wordList: ['STRING'], corpus: 'STRING', minWordCount: 'INT64'},
};
// Run the query
const [rows] = await bigquery.query(queryOptions);
console.log('Rows:');
rows.forEach(row => console.log(row));
}