追加構成で、指定された場所で BigQuery(クエリ、読み込み、抽出、コピー)ジョブを実行します。
このコードサンプルが含まれるドキュメント ページ
コードサンプル
C#
このサンプルを試す前に、BigQuery クイックスタート: クライアント ライブラリの使用の C# の設定手順を実施してください。詳細については、BigQuery C# API のリファレンス ドキュメントをご覧ください。
using Google.Cloud.BigQuery.V2;
using System;
using System.Collections.Generic;
public class BigQueryCreateJob
{
public BigQueryJob CreateJob(string projectId = "your-project-id")
{
string query = @"
SELECT country_name from `bigquery-public-data.utility_us.country_code_iso";
// Initialize client that will be used to send requests.
BigQueryClient client = BigQueryClient.Create(projectId);
QueryOptions queryOptions = new QueryOptions
{
JobLocation = "us",
JobIdPrefix = "code_sample_",
Labels = new Dictionary<string, string>
{
["example-label"] = "example-value"
},
MaximumBytesBilled = 1000000
};
BigQueryJob queryJob = client.CreateQueryJob(
sql: query,
parameters: null,
options: queryOptions);
Console.WriteLine($"Started job: {queryJob.Reference.JobId}");
return queryJob;
}
}
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.Job;
import com.google.cloud.bigquery.JobId;
import com.google.cloud.bigquery.JobInfo;
import com.google.cloud.bigquery.QueryJobConfiguration;
import com.google.common.collect.ImmutableMap;
import java.util.UUID;
// Sample to create a job
public class CreateJob {
public static void main(String[] args) {
// TODO(developer): Replace these variables before running the sample.
String query = "SELECT country_name from `bigquery-public-data.utility_us.country_code_iso`";
createJob(query);
}
public static void createJob(String query) {
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();
// Specify a job configuration to set optional job resource properties.
QueryJobConfiguration queryConfig =
QueryJobConfiguration.newBuilder(query)
.setLabels(ImmutableMap.of("example-label", "example-value"))
.build();
// The location and job name are optional,
// if both are not specified then client will auto-create.
String jobName = "jobId_" + UUID.randomUUID().toString();
JobId jobId = JobId.newBuilder().setLocation("us").setJob(jobName).build();
// Create a job with job ID
bigquery.create(JobInfo.of(jobId, queryConfig));
// Get a job that was just created
Job job = bigquery.getJob(jobId);
if (job.getJobId().getJob().equals(jobId.getJob())) {
System.out.print("Job created successfully." + job.getJobId().getJob());
} else {
System.out.print("Job was not created");
}
} catch (BigQueryException e) {
System.out.print("Job was not created. \n" + e.toString());
}
}
}
Node.js
このサンプルを試す前に、BigQuery クイックスタート: クライアント ライブラリの使用の Node.js の設定手順を実施してください。詳細については、BigQuery Node.js API のリファレンス ドキュメントをご覧ください。
// Import the Google Cloud client library and create a client
const {BigQuery} = require('@google-cloud/bigquery');
const bigquery = new BigQuery();
async function createJob() {
// Run a BigQuery query job.
// For all options, see https://cloud.google.com/bigquery/docs/reference/rest/v2/Job
const options = {
// Specify a job configuration to set optional job resource properties.
configuration: {
query: {
query: `SELECT country_name
FROM \`bigquery-public-data.utility_us.country_code_iso\`
LIMIT 10`,
useLegacySql: false,
},
labels: {'example-label': 'example-value'},
},
};
// Make API request.
const response = await bigquery.createJob(options);
const job = response[0];
// Wait for the query to finish
const [rows] = await job.getQueryResults(job);
// Print the results
console.log('Rows:');
rows.forEach(row => console.log(row));
}
Python
このサンプルを試す前に、BigQuery クイックスタート: クライアント ライブラリの使用の Python の手順に従って設定を行ってください。詳細については、BigQuery Python API のリファレンス ドキュメントをご覧ください。
from google.cloud import bigquery
# Construct a BigQuery client object.
client = bigquery.Client()
query_job = client.query(
"SELECT country_name from `bigquery-public-data.utility_us.country_code_iso`",
# Explicitly force job execution to be routed to a specific processing
# location.
location="US",
# Specify a job configuration to set optional job resource properties.
job_config=bigquery.QueryJobConfig(
labels={"example-label": "example-value"}, maximum_bytes_billed=1000000
),
# The client libraries automatically generate a job ID. Override the
# generated ID with either the job_id_prefix or job_id parameters.
job_id_prefix="code_sample_",
) # Make an API request.
print("Started job: {}".format(query_job.job_id))