@Override void run(BigQuery bigquery, TableInfo table) throws Exception { Table createTable = bigquery.create(table); System.out.println("Created table:"); System.out.println(createTable.toString()); }
@Override public void run(BigQuery bigquery, DatasetId datasetId) { bigquery.create(DatasetInfo.newBuilder(datasetId).build()); System.out.println("Created dataset " + datasetId); } }
/** Example of creating a query job. */ // [TARGET create(JobInfo, JobOption...)] // [VARIABLE "SELECT field FROM my_dataset_name.my_table_name"] public Job createJob(String query) { // [START ] Job job = null; JobConfiguration jobConfiguration = QueryJobConfiguration.of(query); JobInfo jobInfo = JobInfo.of(jobConfiguration); try { job = bigquery.create(jobInfo); } catch (BigQueryException e) { // the job was not created } // [END ] return job; }
private void generateTableWithDdl(String datasetId, String tableId) throws InterruptedException { String sql = String.format( "CREATE TABLE %s.%s " + "AS " + "SELECT " + "2000 + CAST(18 * RAND() as INT64) AS year, " + "IF(RAND() > 0.5,\"foo\",\"bar\") AS token " + "FROM " + "UNNEST(GENERATE_ARRAY(0,5,1)) AS r", datasetId, tableId); Job job = bigquery.create(JobInfo.of(QueryJobConfiguration.newBuilder(sql).build())); job.waitFor(); }
@Test public void testExtractDataUris() throws Exception { initializeExpectedTable(2); expect(bigquery.getOptions()).andReturn(mockOptions); Job expectedJob = new Job(serviceMockReturnsOptions, new JobInfo.BuilderImpl(EXTRACT_JOB_INFO)); expect(bigquery.create(EXTRACT_JOB_INFO)).andReturn(expectedJob); replay(bigquery); initializeTable(); Job job = table.extract("CSV", ImmutableList.of("URI")); assertSame(expectedJob, job); }
@Test public void testExtractDataUri() throws Exception { initializeExpectedTable(2); expect(bigquery.getOptions()).andReturn(mockOptions); Job expectedJob = new Job(serviceMockReturnsOptions, new JobInfo.BuilderImpl(EXTRACT_JOB_INFO)); expect(bigquery.create(EXTRACT_JOB_INFO)).andReturn(expectedJob); replay(bigquery); initializeTable(); Job job = table.extract("CSV", "URI"); assertSame(expectedJob, job); }
@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 testCreateTable() throws Exception { initializeExpectedDataset(2); Table expectedTable = new Table(serviceMockReturnsOptions, new TableInfo.BuilderImpl(TABLE_INFO1)); expect(bigquery.getOptions()).andReturn(mockOptions); expect(bigquery.create(TABLE_INFO1)).andReturn(expectedTable); replay(bigquery); initializeDataset(); Table table = dataset.create(TABLE_INFO1.getTableId().getTable(), TABLE_DEFINITION); assertEquals(expectedTable, table); }
@Test public void testCopyFromString() throws Exception { initializeExpectedTable(2); expect(bigquery.getOptions()).andReturn(mockOptions); Job expectedJob = new Job(serviceMockReturnsOptions, new JobInfo.BuilderImpl(COPY_JOB_INFO)); expect(bigquery.create(COPY_JOB_INFO)).andReturn(expectedJob); replay(bigquery); initializeTable(); Job job = table.copy(TABLE_ID2.getDataset(), TABLE_ID2.getTable()); assertSame(expectedJob, job); }
@Test public void testCopyFromId() throws Exception { initializeExpectedTable(2); expect(bigquery.getOptions()).andReturn(mockOptions); Job expectedJob = new Job(serviceMockReturnsOptions, new JobInfo.BuilderImpl(COPY_JOB_INFO)); expect(bigquery.create(COPY_JOB_INFO)).andReturn(expectedJob); replay(bigquery); initializeTable(); Job job = table.copy(TABLE_ID2.getDataset(), TABLE_ID2.getTable()); 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); }
@Test public void testCreateDataset() { DatasetInfo datasetInfo = DATASET_INFO.setProjectId(OTHER_PROJECT); EasyMock.expect(bigqueryRpcMock.create(datasetInfo.toPb(), EMPTY_RPC_OPTIONS)) .andReturn(datasetInfo.toPb()); EasyMock.replay(bigqueryRpcMock); bigquery = options.getService(); Dataset dataset = bigquery.create(datasetInfo); assertEquals(new Dataset(bigquery, new DatasetInfo.BuilderImpl(datasetInfo)), dataset); }
@Test public void testCreateTableWithOptions() throws Exception { initializeExpectedDataset(2); Table expectedTable = new Table(serviceMockReturnsOptions, new TableInfo.BuilderImpl(TABLE_INFO1)); expect(bigquery.getOptions()).andReturn(mockOptions); expect(bigquery.create(TABLE_INFO1, BigQuery.TableOption.fields())).andReturn(expectedTable); replay(bigquery); initializeDataset(); Table table = dataset.create( TABLE_INFO1.getTableId().getTable(), TABLE_DEFINITION, BigQuery.TableOption.fields()); assertEquals(expectedTable, table); }
@Test public void testCreateJobWithSelectedFields() { Capture<Map<BigQueryRpc.Option, Object>> capturedOptions = Capture.newInstance(); EasyMock.expect( bigqueryRpcMock.create( EasyMock.anyObject(com.google.api.services.bigquery.model.Job.class), EasyMock.capture(capturedOptions))) .andReturn(newJobPb()); EasyMock.replay(bigqueryRpcMock); BigQuery.JobOption jobOptions = BigQuery.JobOption.fields(BigQuery.JobField.USER_EMAIL); bigquery = options.getService(); bigquery.create(JobInfo.of(QueryJobConfiguration.of("SOME QUERY")), jobOptions); String selector = (String) capturedOptions.getValue().get(jobOptions.getRpcOption()); // jobReference and configuration are always sent; the RPC call won't succeed otherwise. assertThat(selector.split(",")) .asList() .containsExactly("jobReference", "configuration", "user_email"); }
@Test public void testCreateDatasetWithSelectedFields() { Capture<Map<BigQueryRpc.Option, Object>> capturedOptions = Capture.newInstance(); EasyMock.expect( bigqueryRpcMock.create(eq(DATASET_INFO_WITH_PROJECT.toPb()), capture(capturedOptions))) .andReturn(DATASET_INFO_WITH_PROJECT.toPb()); EasyMock.replay(bigqueryRpcMock); bigquery = options.getService(); Dataset dataset = bigquery.create(DATASET_INFO, DATASET_OPTION_FIELDS); String selector = (String) capturedOptions.getValue().get(DATASET_OPTION_FIELDS.getRpcOption()); assertTrue(selector.contains("datasetReference")); assertTrue(selector.contains("access")); assertTrue(selector.contains("etag")); assertEquals(28, selector.length()); assertEquals( new Dataset(bigquery, new DatasetInfo.BuilderImpl(DATASET_INFO_WITH_PROJECT)), dataset); }
@Test public void testCreateTable() { TableInfo tableInfo = TABLE_INFO.setProjectId(OTHER_PROJECT); EasyMock.expect(bigqueryRpcMock.create(tableInfo.toPb(), EMPTY_RPC_OPTIONS)) .andReturn(tableInfo.toPb()); EasyMock.replay(bigqueryRpcMock); BigQueryOptions bigQueryOptions = createBigQueryOptionsForProject(OTHER_PROJECT, rpcFactoryMock); bigquery = bigQueryOptions.getService(); Table table = bigquery.create(tableInfo); assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(tableInfo)), table); }
@Test public void testCreateJobSuccess() { String id = "testCreateJobSuccess-id"; JobId jobId = JobId.of(id); String query = "SELECT * in FOO"; Capture<com.google.api.services.bigquery.model.Job> jobCapture = EasyMock.newCapture(); EasyMock.expect( bigqueryRpcMock.create(EasyMock.capture(jobCapture), EasyMock.eq(EMPTY_RPC_OPTIONS))) .andReturn(newJobPb()); EasyMock.replay(bigqueryRpcMock); bigquery = options.getService(); assertThat(bigquery.create(JobInfo.of(jobId, QueryJobConfiguration.of(query)))).isNotNull(); assertThat(jobCapture.getValue().getJobReference().getJobId()).isEqualTo(id); }
@Test public void testCreateTableWithoutProject() { TableInfo tableInfo = TABLE_INFO.setProjectId(PROJECT); TableId tableId = TableId.of("", TABLE_ID.getDataset(), TABLE_ID.getTable()); tableInfo.toBuilder().setTableId(tableId); EasyMock.expect(bigqueryRpcMock.create(tableInfo.toPb(), EMPTY_RPC_OPTIONS)) .andReturn(tableInfo.toPb()); EasyMock.replay(bigqueryRpcMock); BigQueryOptions bigQueryOptions = createBigQueryOptionsForProject(PROJECT, rpcFactoryMock); bigquery = bigQueryOptions.getService(); Table table = bigquery.create(tableInfo); assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(tableInfo)), table); }
@Test public void testCancelJob() throws InterruptedException, TimeoutException { String destinationTableName = "test_cancel_query_job_table"; String query = "SELECT TimestampField, StringField, BooleanField FROM " + TABLE_ID.getTable(); TableId destinationTable = TableId.of(DATASET, destinationTableName); QueryJobConfiguration configuration = QueryJobConfiguration.newBuilder(query) .setDefaultDataset(DatasetId.of(DATASET)) .setDestinationTable(destinationTable) .build(); Job remoteJob = bigquery.create(JobInfo.of(configuration)); assertTrue(remoteJob.cancel()); remoteJob = remoteJob.waitFor(); assertNull(remoteJob.getStatus().getError()); }
@Test public void testQueryJobWithDryRun() throws InterruptedException, TimeoutException { String tableName = "test_query_job_table"; String query = "SELECT TimestampField, StringField, BooleanField FROM " + TABLE_ID.getTable(); TableId destinationTable = TableId.of(DATASET, tableName); QueryJobConfiguration configuration = QueryJobConfiguration.newBuilder(query) .setDefaultDataset(DatasetId.of(DATASET)) .setDestinationTable(destinationTable) .setDryRun(true) .build(); Job remoteJob = bigquery.create(JobInfo.of(configuration)); assertNull(remoteJob.getJobId().getJob()); assertEquals(DONE, remoteJob.getStatus().getState()); assertNotNull(remoteJob.getConfiguration()); }