Skip to content

Commit

Permalink
refactor(samples): create a partition table (#525)
Browse files Browse the repository at this point in the history
  • Loading branch information
Praful Makani committed Jul 10, 2020
1 parent 82341cb commit ca11fdc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
Expand Up @@ -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() {
Expand All @@ -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);
}

Expand All @@ -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()
Expand Down
Expand Up @@ -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;

Expand All @@ -48,30 +51,27 @@ 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);
}

@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);
}
}

0 comments on commit ca11fdc

Please sign in to comment.