-
Notifications
You must be signed in to change notification settings - Fork 0
/
bq_ptable_pbycol.java
56 lines (48 loc) · 2.45 KB
/
bq_ptable_pbycol.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import com.google.cloud.bigquery.BigQuery;
import com.google.cloud.bigquery.BigQueryOptions;
import com.google.cloud.bigquery.Dataset;
import com.google.cloud.bigquery.DatasetInfo;
import com.google.cloud.bigquery.Field;
import com.google.cloud.bigquery.Schema;
import com.google.cloud.bigquery.StandardTableDefinition;
import com.google.cloud.bigquery.Table;
import com.google.cloud.bigquery.TableId;
import com.google.cloud.bigquery.TableInfo;
import com.google.cloud.bigquery.LegacySQLTypeName;
import com.google.cloud.bigquery.TimePartitioning;
import com.google.cloud.bigquery.TimePartitioning.Type;
public class QuickstartSample {
private static final Type TYPE = Type.DAY;
private static final long EXPIRATION_MS = 42;
private static final boolean REQUIRE_PARTITION_FILTER = false;
private static final String FIELD = "TSField";
private static final TimePartitioning TIME_PARTITIONING =
TimePartitioning.newBuilder(TYPE)
.setExpirationMs(EXPIRATION_MS)
.setRequirePartitionFilter(REQUIRE_PARTITION_FILTER)
.setField(FIELD)
.build();
public static void main(String... args) throws Exception {
// Instantiate a client. If you don't specify credentials when constructing a client, the
// client library will look for credentials in the environment, such as the
// GOOGLE_APPLICATION_CREDENTIALS environment variable.
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
// The name for the new dataset
String datasetName = "my_new_dataset";
// Prepares a new dataset
Dataset dataset = null;
DatasetInfo datasetInfo = DatasetInfo.newBuilder(datasetName).build();
// Creates the dataset
dataset = bigquery.create(datasetInfo);
System.out.printf("Dataset %s created.%n", dataset.getDatasetId().getDataset());
TableId tableId = TableId.of(datasetName, "my_table_id");
// Table field definition
Field stringField = Field.of("StringField", LegacySQLTypeName.STRING);
Field tsField = Field.of("TSField", LegacySQLTypeName.TIMESTAMP);
// Table schema definition
Schema schema = Schema.of(stringField, tsField);
// Create a table
StandardTableDefinition tableDefinition = StandardTableDefinition.of(schema).toBuilder().setTimePartitioning(TIME_PARTITIONING).build();
Table createdTable = bigquery.create(TableInfo.of(tableId, tableDefinition));
}
}