diff --git a/samples/snippets/src/main/java/com/example/bigquery/CreatePartitionedTable.java b/samples/snippets/src/main/java/com/example/bigquery/CreatePartitionedTable.java index 1279d65ed..e64010d5c 100644 --- a/samples/snippets/src/main/java/com/example/bigquery/CreatePartitionedTable.java +++ b/samples/snippets/src/main/java/com/example/bigquery/CreatePartitionedTable.java @@ -28,6 +28,7 @@ import com.google.cloud.bigquery.TableInfo; import com.google.cloud.bigquery.TimePartitioning; +// Sample to create a partition table public class CreatePartitionedTable { public static void runCreatePartitionedTable() { @@ -36,9 +37,9 @@ public static void runCreatePartitionedTable() { String tableName = "MY_TABLE_NAME"; Schema schema = Schema.of( - Field.of("stringField", StandardSQLTypeName.STRING), - Field.of("booleanField", StandardSQLTypeName.BOOL), - Field.of("dateField", StandardSQLTypeName.DATE)); + Field.of("name", StandardSQLTypeName.STRING), + Field.of("post_abbr", StandardSQLTypeName.STRING), + Field.of("date", StandardSQLTypeName.DATE)); createPartitionedTable(datasetName, tableName, schema); } @@ -50,7 +51,11 @@ public static void createPartitionedTable(String datasetName, String tableName, TableId tableId = TableId.of(datasetName, tableName); - TimePartitioning partitioning = TimePartitioning.of(TimePartitioning.Type.DAY); + TimePartitioning partitioning = + TimePartitioning.newBuilder(TimePartitioning.Type.DAY) + .setField("date") // name of column to use for partitioning + .setExpirationMs(7776000000L) // 90 days + .build(); StandardTableDefinition tableDefinition = StandardTableDefinition.newBuilder() diff --git a/samples/snippets/src/test/java/com/example/bigquery/CreatePartitionedTableIT.java b/samples/snippets/src/test/java/com/example/bigquery/CreatePartitionedTableIT.java index 35ab85b38..2e15e65e8 100644 --- a/samples/snippets/src/test/java/com/example/bigquery/CreatePartitionedTableIT.java +++ b/samples/snippets/src/test/java/com/example/bigquery/CreatePartitionedTableIT.java @@ -24,12 +24,15 @@ import com.google.cloud.bigquery.StandardSQLTypeName; import java.io.ByteArrayOutputStream; import java.io.PrintStream; +import java.util.UUID; import org.junit.After; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; public class CreatePartitionedTableIT { + + private String tableName; private ByteArrayOutputStream bout; private PrintStream out; @@ -48,6 +51,7 @@ public static void checkRequirements() { @Before public void setUp() { + tableName = "MY_PARTITIONED_TABLE_TEST_" + UUID.randomUUID().toString().substring(0, 8); bout = new ByteArrayOutputStream(); out = new PrintStream(bout); System.setOut(out); @@ -55,23 +59,19 @@ public void setUp() { @After public void tearDown() { + // Clean up + DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName); System.setOut(null); } @Test public void testCreatePartitionedTable() { - String tableName = "MY_PARTITIONED_TABLE"; Schema schema = Schema.of( - Field.of("stringField", StandardSQLTypeName.STRING), - Field.of("booleanField", StandardSQLTypeName.BOOL), - Field.of("dateField", StandardSQLTypeName.DATE)); - + Field.of("name", StandardSQLTypeName.STRING), + Field.of("post_abbr", StandardSQLTypeName.STRING), + Field.of("date", StandardSQLTypeName.DATE)); CreatePartitionedTable.createPartitionedTable(BIGQUERY_DATASET_NAME, tableName, schema); - assertThat(bout.toString()).contains("Partitioned table created successfully"); - - // Clean up - DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName); } }