return load(format, ImmutableList.of(sourceUri), options);
/** 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 ] 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( RetryOption.initialRetryDelay(Duration.ofSeconds(1)), RetryOption.totalTimeout(Duration.ofMinutes(3))); if (completedJob != null && completedJob.getStatus().getError() == null) { // Job completed successfully } else { // Handle error case } } catch (InterruptedException e) { // Handle interrupted wait } // [END ] return job; }
public static void main(String... args) throws InterruptedException, TimeoutException { BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); TableId tableId = TableId.of("dataset", "table"); Table table = bigquery.getTable(tableId); if (table == null) { System.out.println("Creating table " + tableId); Field integerField = Field.of("fieldName", LegacySQLTypeName.INTEGER); Schema schema = Schema.of(integerField); table = bigquery.create(TableInfo.of(tableId, StandardTableDefinition.of(schema))); } System.out.println("Loading data into table " + tableId); Job loadJob = table.load(FormatOptions.csv(), "gs://bucket/path"); loadJob = loadJob.waitFor(); if (loadJob.getStatus().getError() != null) { System.out.println("Job completed with errors"); } else { System.out.println("Job succeeded"); } } }
@Test public void testLoadDataUris() throws Exception { initializeExpectedTable(2); expect(bigquery.getOptions()).andReturn(mockOptions); Job expectedJob = new Job(serviceMockReturnsOptions, new JobInfo.BuilderImpl(LOAD_JOB_INFO)); expect(bigquery.create(LOAD_JOB_INFO)).andReturn(expectedJob); replay(bigquery); initializeTable(); Job job = table.load(FormatOptions.json(), ImmutableList.of("URI")); assertSame(expectedJob, job); }
@Test public void testLoadDataUri() throws Exception { initializeExpectedTable(2); expect(bigquery.getOptions()).andReturn(mockOptions); Job expectedJob = new Job(serviceMockReturnsOptions, new JobInfo.BuilderImpl(LOAD_JOB_INFO)); expect(bigquery.create(LOAD_JOB_INFO)).andReturn(expectedJob); replay(bigquery); initializeTable(); Job job = table.load(FormatOptions.json(), "URI"); assertSame(expectedJob, job); }
/** Example loading data from a single Google Cloud Storage file. */ // [TARGET load(FormatOptions, String, JobOption...)] // [VARIABLE "gs://my_bucket/filename.csv"] public Job loadSingle(String sourceUri) { // [START bigquery_load_table_gcs_csv] Job job = table.load(FormatOptions.csv(), sourceUri); // Wait for the job to complete try { Job completedJob = job.waitFor( RetryOption.initialRetryDelay(Duration.ofSeconds(1)), RetryOption.totalTimeout(Duration.ofMinutes(3))); if (completedJob != null && completedJob.getStatus().getError() == null) { // Job completed successfully } else { // Handle error case } } catch (InterruptedException e) { // Handle interrupted wait } // [END bigquery_load_table_gcs_csv] return job; } }
/** * Starts a BigQuery Job to load data into the current table from the provided source URI. Returns * the started {@link Job} object. * * @param format the format of the data to load * @param sourceUri the fully-qualified Google Cloud Storage URI (e.g. gs://bucket/path) from * which to load the data * @param options job options * @throws BigQueryException upon failure */ public Job load(FormatOptions format, String sourceUri, BigQuery.JobOption... options) throws BigQueryException { return load(format, ImmutableList.of(sourceUri), options); }
return load(format, ImmutableList.of(sourceUri), options);