use of com.google.cloud.bigquery.Job in project google-cloud-java by GoogleCloudPlatform.
the class ITJobSnippets method testWaitForWithOptions.
@Test
public void testWaitForWithOptions() throws Exception {
JobConfiguration jobConfig = QueryJobConfiguration.newBuilder(QUERY).setUseLegacySql(false).build();
JobInfo jobInfo = JobInfo.newBuilder(jobConfig).build();
Job job = bigquery.create(jobInfo);
JobSnippets jobSnippets = new JobSnippets(job);
boolean result = jobSnippets.waitForWithOptions();
assertTrue(result);
}
use of com.google.cloud.bigquery.Job in project google-cloud-java by GoogleCloudPlatform.
the class ITJobSnippets method testReloadStatus.
@Test
public void testReloadStatus() throws Exception {
JobConfiguration jobConfig = QueryJobConfiguration.newBuilder(QUERY).setUseLegacySql(false).build();
JobInfo jobInfo = JobInfo.newBuilder(jobConfig).build();
Job job = bigquery.create(jobInfo);
JobSnippets jobSnippets = new JobSnippets(job);
JobStatus.State result = jobSnippets.reloadStatus();
assertEquals(JobStatus.State.DONE, result);
}
use of com.google.cloud.bigquery.Job in project google-cloud-java by GoogleCloudPlatform.
the class ITJobSnippets method testReload.
@Test
public void testReload() throws Exception {
JobConfiguration jobConfig = QueryJobConfiguration.newBuilder(QUERY).setUseLegacySql(false).build();
JobInfo jobInfo = JobInfo.newBuilder(jobConfig).build();
Job job = bigquery.create(jobInfo);
JobSnippets jobSnippets = new JobSnippets(job);
JobStatus.State result = jobSnippets.reload();
assertEquals(JobStatus.State.DONE, result);
}
use of com.google.cloud.bigquery.Job in project google-cloud-java by GoogleCloudPlatform.
the class BigQuerySnippets method writeFileToTable.
/**
* Example of writing a local file to a table.
*/
// [TARGET writer(WriteChannelConfiguration)]
// [VARIABLE "my_dataset_name"]
// [VARIABLE "my_table_name"]
// [VARIABLE FileSystems.getDefault().getPath(".", "my-data.csv")]
public long writeFileToTable(String datasetName, String tableName, Path csvPath) throws IOException, InterruptedException, TimeoutException {
// [START writeFileToTable]
TableId tableId = TableId.of(datasetName, tableName);
WriteChannelConfiguration writeChannelConfiguration = WriteChannelConfiguration.newBuilder(tableId).setFormatOptions(FormatOptions.csv()).build();
TableDataWriteChannel writer = bigquery.writer(writeChannelConfiguration);
// Write data to writer
try (OutputStream stream = Channels.newOutputStream(writer)) {
Files.copy(csvPath, stream);
}
// Get load job
Job job = writer.getJob();
job = job.waitFor();
LoadStatistics stats = job.getStatistics();
return stats.getOutputRows();
// [END writeFileToTable]
}
use of com.google.cloud.bigquery.Job in project google-cloud-java by GoogleCloudPlatform.
the class TableSnippets method copyTableId.
/**
* Example copying the table to a destination table.
*/
// [TARGET copy(TableId, JobOption...)]
// [VARIABLE "my_dataset"]
// [VARIABLE "my_destination_table"]
public Job copyTableId(String dataset, String tableName) throws BigQueryException {
// [START copyTableId]
TableId destinationId = TableId.of(dataset, tableName);
JobOption options = JobOption.fields(JobField.STATUS, JobField.USER_EMAIL);
Job job = table.copy(destinationId, options);
// Wait for the job to complete.
try {
Job completedJob = job.waitFor(WaitForOption.checkEvery(1, TimeUnit.SECONDS), WaitForOption.timeout(3, TimeUnit.MINUTES));
if (completedJob != null && completedJob.getStatus().getError() == null) {
// Job completed successfully.
} else {
// Handle error case.
}
} catch (InterruptedException | TimeoutException e) {
// Handle interrupted wait
}
// [END copyTableId]
return job;
}
Aggregations