use of com.google.cloud.bigquery.Job in project google-cloud-java by GoogleCloudPlatform.
the class TableSnippets method loadList.
/**
* Example loading data from a list of Google Cloud Storage files.
*/
// [TARGET load(FormatOptions, List, JobOption...)]
// [VARIABLE "gs://my_bucket/filename1.csv"]
// [VARIABLE "gs://my_bucket/filename2.csv"]
public Job loadList(String gcsUrl1, String gcsUrl2) {
// [START loadList]
List<String> sourceUris = new ArrayList<>();
sourceUris.add(gcsUrl1);
sourceUris.add(gcsUrl2);
Job job = table.load(FormatOptions.csv(), sourceUris);
// 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 loadList]
return job;
}
use of com.google.cloud.bigquery.Job in project google-cloud-java by GoogleCloudPlatform.
the class TableSnippets method extractList.
/**
* Example of partitioning data to a list of Google Cloud Storage files.
*/
// [TARGET extract(String, List, JobOption...)]
// [VARIABLE "CSV"]
// [VARIABLE "gs://my_bucket/PartitionA_*.csv"]
// [VARIABLE "gs://my_bucket/PartitionB_*.csv"]
public Job extractList(String format, String gcsUrl1, String gcsUrl2) {
// [START extractList]
List<String> destinationUris = new ArrayList<>();
destinationUris.add(gcsUrl1);
destinationUris.add(gcsUrl2);
Job job = table.extract(format, destinationUris);
// 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 extractList]
return job;
}
use of com.google.cloud.bigquery.Job in project google-cloud-java by GoogleCloudPlatform.
the class ITBigQueryTest method testExtractJob.
@Test
public void testExtractJob() throws InterruptedException, TimeoutException {
String tableName = "test_export_job_table";
TableId destinationTable = TableId.of(DATASET, tableName);
LoadJobConfiguration configuration = LoadJobConfiguration.newBuilder(destinationTable, "gs://" + BUCKET + "/" + LOAD_FILE).setSchema(SIMPLE_SCHEMA).build();
Job remoteLoadJob = bigquery.create(JobInfo.of(configuration));
remoteLoadJob = remoteLoadJob.waitFor();
assertNull(remoteLoadJob.getStatus().getError());
ExtractJobConfiguration extractConfiguration = ExtractJobConfiguration.newBuilder(destinationTable, "gs://" + BUCKET + "/" + EXTRACT_FILE).setPrintHeader(false).build();
Job remoteExtractJob = bigquery.create(JobInfo.of(extractConfiguration));
remoteExtractJob = remoteExtractJob.waitFor();
assertNull(remoteExtractJob.getStatus().getError());
assertEquals(CSV_CONTENT, new String(storage.readAllBytes(BUCKET, EXTRACT_FILE), StandardCharsets.UTF_8));
assertTrue(bigquery.delete(DATASET, tableName));
}
use of com.google.cloud.bigquery.Job in project google-cloud-java by GoogleCloudPlatform.
the class ITBigQueryTest method testListJobsWithSelectedFields.
@Test
public void testListJobsWithSelectedFields() {
Page<Job> jobs = bigquery.listJobs(JobListOption.fields(JobField.USER_EMAIL));
for (Job job : jobs.getValues()) {
assertNotNull(job.getJobId());
assertNotNull(job.getStatus());
assertNotNull(job.getUserEmail());
assertNull(job.getStatistics());
assertNull(job.getGeneratedId());
}
}
use of com.google.cloud.bigquery.Job in project google-cloud-java by GoogleCloudPlatform.
the class ITBigQuerySnippets method testJob.
@Test
public void testJob() throws ExecutionException, InterruptedException {
Job job1 = bigquerySnippets.createJob(QUERY);
Job job2 = bigquerySnippets.createJob(QUERY);
assertNotNull(job1);
assertNotNull(job2);
assertEquals(job1.getJobId(), bigquerySnippets.getJob(job1.getJobId().getJob()).getJobId());
assertEquals(job2.getJobId(), bigquerySnippets.getJobFromId(job2.getJobId().getJob()).getJobId());
Set<JobId> jobs = Sets.newHashSet(Iterators.transform(bigquerySnippets.listJobs().iterateAll().iterator(), TO_JOB_ID_FUNCTION));
while (!jobs.contains(job1.getJobId()) || !jobs.contains(job2.getJobId())) {
Thread.sleep(500);
jobs = Sets.newHashSet(Iterators.transform(bigquerySnippets.listJobs().iterateAll().iterator(), TO_JOB_ID_FUNCTION));
}
assertTrue(bigquerySnippets.cancelJob(job1.getJobId().getJob()));
assertTrue(bigquerySnippets.cancelJobFromId(job2.getJobId().getJob()));
}
Aggregations