Java Code Examples for com.google.cloud.bigquery.StandardTableDefinition#Builder

The following examples show how to use com.google.cloud.bigquery.StandardTableDefinition#Builder . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BigQueryMapper.java    From DataflowTemplates with Apache License 2.0 6 votes vote down vote up
/**
 * Returns {@code Table} after creating the table with no columns in BigQuery.
 *
 * @param tableId a TableId referencing the BigQuery table being requested.
 */
private Table createBigQueryTable(TableId tableId) {
  // Create Blank BigQuery Table
  List<Field> fieldList = new ArrayList<Field>();
  Schema schema = Schema.of(fieldList);

  StandardTableDefinition.Builder tableDefinitionBuilder =
      StandardTableDefinition.newBuilder().setSchema(schema);
  if (dayPartitioning) {
    tableDefinitionBuilder.setTimePartitioning(
        TimePartitioning.newBuilder(TimePartitioning.Type.DAY).build());
  }
  TableInfo tableInfo = TableInfo.newBuilder(tableId, tableDefinitionBuilder.build()).build();
  Table table = bigquery.create(tableInfo);

  return table;
}
 
Example 2
Source File: BigQueryMapper.java    From DataflowTemplates with Apache License 2.0 6 votes vote down vote up
private Table createBigQueryTable(TableId tableId) {
  // Create Blank BigQuery Table
  LOG.info(String.format("Creating Table: %s", tableId.toString()));

  List<Field> fieldList = new ArrayList<Field>();
  Schema schema = Schema.of(fieldList);

  StandardTableDefinition.Builder tableDefinitionBuilder =
      StandardTableDefinition.newBuilder().setSchema(schema);
  if (dayPartitioning) {
    tableDefinitionBuilder.setTimePartitioning(
        TimePartitioning.newBuilder(TimePartitioning.Type.DAY).build());
  }
  TableInfo tableInfo = TableInfo.newBuilder(tableId, tableDefinitionBuilder.build()).build();
  Table table = bigquery.create(tableInfo);

  return table;
}
 
Example 3
Source File: BQTableDefinition.java    From beast with Apache License 2.0 5 votes vote down vote up
private StandardTableDefinition getPartitionedTableDefinition(Schema schema) {
    StandardTableDefinition.Builder tableDefinition = StandardTableDefinition.newBuilder();
    Optional<Field> partitionFieldOptional = schema.getFields().stream().filter(obj -> obj.getName().equals(bqConfig.getBQTablePartitionKey())).findFirst();
    if (!partitionFieldOptional.isPresent()) {
        throw new BQPartitionKeyNotSpecified(String.format("Partition key %s is not present in the schema", bqConfig.getBQTablePartitionKey()));
    }

    Field partitionField = partitionFieldOptional.get();
    if (isTimePartitionedField(partitionField)) {
        return createTimePartitionBuilder(partitionField, tableDefinition)
                .setSchema(schema)
                .build();
    } else {
        throw new UnsupportedOperationException("Range Bigquery partitioning is not supported, supported paritition fields have to be of DATE or TIMESTAMP type");
    }
}
 
Example 4
Source File: BQTableDefinition.java    From beast with Apache License 2.0 5 votes vote down vote up
private StandardTableDefinition.Builder createTimePartitionBuilder(Field partitionField, StandardTableDefinition.Builder tableBuilder) {
    TimePartitioning.Builder timePartitioningBuilder = TimePartitioning.newBuilder(TimePartitioning.Type.DAY);
    if (bqConfig.getBQTablePartitionKey() == null) {
        throw new BQPartitionKeyNotSpecified(String.format("Partition key not specified for the table: %s", bqConfig.getTable()));
    }
    timePartitioningBuilder.setField(bqConfig.getBQTablePartitionKey())
            .setRequirePartitionFilter(true);

    return tableBuilder
            .setTimePartitioning(timePartitioningBuilder.build());
}
 
Example 5
Source File: ITTableSnippets.java    From google-cloud-java with Apache License 2.0 5 votes vote down vote up
@Before
public void before() {
  ++nextTableNumber;
  StandardTableDefinition.Builder builder = StandardTableDefinition.newBuilder();
  builder.setSchema(SCHEMA);
  table = bigquery.create(TableInfo.of(getTableId(), builder.build()));
  bigquery.create(TableInfo.of(getCopyTableId(), builder.build()));
  tableSnippets = new TableSnippets(table);
}