A simple usage example showing how to create a table if it does not exist and load data into - * it. For the complete source code see - * CreateTableAndLoadData.java. + *
A simple usage example showing how to create a table in Bigquery. For the complete source code see + * CreateTable.java. * *
{@code - * 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", Field.Type.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"); - * } - * }+ * 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(); + * + * TableId tableId = TableId.of(datasetName, tableName); + * TableDefinition tableDefinition = StandardTableDefinition.of(schema); + * TableInfo tableInfo = TableInfo.newBuilder(tableId, tableDefinition).build(); + * + * bigquery.create(tableInfo); + * System.out.println("Table created successfully"); + * } catch (BigQueryException e) { + * System.out.println("Table was not created. \n" + e.toString()); + * } + * * * @see Google Cloud BigQuery */ diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/testing/package-info.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/testing/package-info.java index 6e5b32419..c2e6c03f5 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/testing/package-info.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/testing/package-info.java @@ -17,7 +17,14 @@ /** * A testing helper for Google BigQuery. * - *
A simple usage example: + *
A simple usage example: 1. Create a test Google Cloud project. + * + *
2. Download a JSON service account credentials file from the Google Developer's Console. + * + *
3. Create a RemoteBigQueryHelper object using your project ID and JSON key. Here is an example + * that uses the RemoteBigQueryHelper to create a dataset. + * + *
4. Run tests. * *
Before the test: * @@ -33,9 +40,5 @@ *
{@code * RemoteBigQueryHelper.forceDelete(bigquery, DATASET); * }- * - * @see - * Google Cloud Java tools for testing */ package com.google.cloud.bigquery.testing; diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/CloneDefinitionTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/CloneDefinitionTest.java new file mode 100644 index 000000000..33bcf5f40 --- /dev/null +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/CloneDefinitionTest.java @@ -0,0 +1,58 @@ +/* + * Copyright 2023 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.cloud.bigquery; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +public class CloneDefinitionTest { + private static final TableId BASE_TABLE_ID = TableId.of("DATASET_NAME", "BASE_TABLE_NAME"); + private static final String CLONE_TIME = "2021-05-19T11:32:26.553Z"; + private static final CloneDefinition CLONETABLE_DEFINITION = + CloneDefinition.newBuilder().setBaseTableId(BASE_TABLE_ID).setCloneTime(CLONE_TIME).build(); + + @Test + public void testToBuilder() { + compareCloneTableDefinition(CLONETABLE_DEFINITION, CLONETABLE_DEFINITION.toBuilder().build()); + CloneDefinition cloneTableDefinition = + CLONETABLE_DEFINITION.toBuilder().setCloneTime("2021-05-20T11:32:26.553Z").build(); + assertEquals("2021-05-20T11:32:26.553Z", cloneTableDefinition.getCloneTime()); + } + + @Test + public void testBuilder() { + assertEquals(BASE_TABLE_ID, CLONETABLE_DEFINITION.getBaseTableId()); + assertEquals(CLONE_TIME, CLONETABLE_DEFINITION.getCloneTime()); + CloneDefinition cloneDefinition = + CloneDefinition.newBuilder().setBaseTableId(BASE_TABLE_ID).setCloneTime(CLONE_TIME).build(); + assertEquals(CLONETABLE_DEFINITION, cloneDefinition); + } + + @Test + public void testToAndFromPb() { + CloneDefinition cloneDefinition = CLONETABLE_DEFINITION.toBuilder().build(); + assertTrue(CloneDefinition.fromPb(cloneDefinition.toPb()) instanceof CloneDefinition); + compareCloneTableDefinition(cloneDefinition, CloneDefinition.fromPb(cloneDefinition.toPb())); + } + + private void compareCloneTableDefinition(CloneDefinition expected, CloneDefinition value) { + assertEquals(expected.getBaseTableId(), value.getBaseTableId()); + assertEquals(expected.getCloneTime(), value.getCloneTime()); + } +} diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java index 0bbe1efdd..bf59c2b67 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java @@ -54,6 +54,7 @@ import com.google.cloud.bigquery.BigQueryException; import com.google.cloud.bigquery.BigQueryResult; import com.google.cloud.bigquery.BigQuerySQLException; +import com.google.cloud.bigquery.CloneDefinition; import com.google.cloud.bigquery.Clustering; import com.google.cloud.bigquery.Connection; import com.google.cloud.bigquery.ConnectionProperty; @@ -4334,6 +4335,7 @@ public void testSnapshotTableCopyJob() throws InterruptedException { assertNotNull(snapshotTable); assertEquals(snapshotTableId.getDataset(), snapshotTable.getTableId().getDataset()); assertEquals(snapshotTableName, snapshotTable.getTableId().getTable()); + System.out.println(snapshotTable.getDefinition()); assertTrue(snapshotTable.getDefinition() instanceof SnapshotTableDefinition); assertEquals(DDL_TABLE_SCHEMA, snapshotTable.getDefinition().getSchema()); assertNotNull(((SnapshotTableDefinition) snapshotTable.getDefinition()).getSnapshotTime()); @@ -5179,4 +5181,56 @@ public void testCreateExternalTableWithReferenceFileSchemaParquet() { boolean success = bigquery.delete(tableId); assertEquals(true, success); } + + @Test + public void testCloneTableCopyJob() throws InterruptedException { + String sourceTableName = "test_copy_job_base_table"; + String ddlTableName = TABLE_ID_DDL.getTable(); + String cloneTableName = String.format("test_clone_table"); + // Create source table with some data in it + String ddlQuery = + String.format( + "CREATE OR REPLACE TABLE %s (" + + "TimestampField TIMESTAMP OPTIONS(description='TimestampDescription'), " + + "StringField STRING OPTIONS(description='StringDescription'), " + + "BooleanField BOOLEAN OPTIONS(description='BooleanDescription') " + + ") AS SELECT * FROM %s", + sourceTableName, ddlTableName); + QueryJobConfiguration ddlConfig = + QueryJobConfiguration.newBuilder(ddlQuery).setDefaultDataset(DatasetId.of(DATASET)).build(); + TableId sourceTableId = TableId.of(DATASET, sourceTableName); + TableResult result = bigquery.query(ddlConfig); + assertEquals(DDL_TABLE_SCHEMA, result.getSchema()); + Table remoteTable = bigquery.getTable(DATASET, sourceTableName); + assertNotNull(remoteTable); + + // Create clone table using source table as the base table + TableId cloneTableId = TableId.of(DATASET, cloneTableName); + CopyJobConfiguration cloneConfiguration = + CopyJobConfiguration.newBuilder(cloneTableId, sourceTableId) + .setOperationType("CLONE") + .build(); + Job createdJob = bigquery.create(JobInfo.of(cloneConfiguration)); + CopyJobConfiguration createdConfiguration = createdJob.getConfiguration(); + assertNotNull(createdConfiguration.getSourceTables()); + assertNotNull(createdConfiguration.getOperationType()); + assertNotNull(createdConfiguration.getDestinationTable()); + Job completedJob = createdJob.waitFor(); + assertNull(completedJob.getStatus().getError()); + + Table cloneTable = bigquery.getTable(DATASET, cloneTableName); + assertNotNull(cloneTable); + assertEquals(cloneTableId.getDataset(), cloneTable.getTableId().getDataset()); + assertEquals(cloneTableName, cloneTable.getTableId().getTable()); + assertEquals(TableDefinition.Type.TABLE, cloneTable.getDefinition().getType()); + assertTrue(cloneTable.getDefinition() instanceof StandardTableDefinition); + assertEquals(DDL_TABLE_SCHEMA, cloneTable.getDefinition().getSchema()); + assertTrue(cloneTable.getCloneDefinition() instanceof CloneDefinition); + assertEquals(sourceTableName, cloneTable.getCloneDefinition().getBaseTableId().getTable()); + assertNotNull(cloneTable.getCloneDefinition().getCloneTime()); + + // Clean up + assertTrue(remoteTable.delete()); + assertTrue(cloneTable.delete()); + } } diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITRemoteUDFTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITRemoteUDFTest.java index 7e74a8f4c..7a3194e52 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITRemoteUDFTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITRemoteUDFTest.java @@ -47,7 +47,7 @@ public class ITRemoteUDFTest { private static final String ID = UUID.randomUUID().toString().substring(0, 8); private static final String PROJECT_ID = ServiceOptions.getDefaultProjectId(); - private static final String CONNECTION_ID = "test-connectin-id-" + ID; + private static final String CONNECTION_ID = "test-connection-id-" + ID; private static final String LOCATION = "US"; private static final String PARENT = LocationName.of(PROJECT_ID, LOCATION).toString(); private static final String REMOTE_ENDPOINT = "https://aaabbbccc-uc.a.run.app/"; diff --git a/pom.xml b/pom.xml index 1a0439262..cb264307e 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@