このコードサンプルが含まれるドキュメント ページ
コードサンプル
Go
このサンプルを試す前に、BigQuery クイックスタート: クライアント ライブラリの使用の Go の手順に従って設定を行ってください。詳細については、BigQuery Go API のリファレンス ドキュメントをご覧ください。
import (
"context"
"fmt"
"time"
"cloud.google.com/go/bigquery"
)
// createTableClustered demonstrates creating a BigQuery table with advanced properties like
// partitioning and clustering features.
func createTableClustered(projectID, datasetID, tableID string) error {
// projectID := "my-project-id"
// datasetID := "mydatasetid"
// tableID := "mytableid"
ctx := context.Background()
client, err := bigquery.NewClient(ctx, projectID)
if err != nil {
return fmt.Errorf("bigquery.NewClient: %v", err)
}
defer client.Close()
sampleSchema := bigquery.Schema{
{Name: "timestamp", Type: bigquery.TimestampFieldType},
{Name: "origin", Type: bigquery.StringFieldType},
{Name: "destination", Type: bigquery.StringFieldType},
{Name: "amount", Type: bigquery.NumericFieldType},
}
metaData := &bigquery.TableMetadata{
Schema: sampleSchema,
TimePartitioning: &bigquery.TimePartitioning{
Field: "timestamp",
Expiration: 90 * 24 * time.Hour,
},
Clustering: &bigquery.Clustering{
Fields: []string{"origin", "destination"},
},
}
tableRef := client.Dataset(datasetID).Table(tableID)
if err := tableRef.Create(ctx, metaData); err != nil {
return err
}
return nil
}
Java
このサンプルを試す前に、BigQuery クイックスタート: クライアント ライブラリの使用の Java の設定手順を実施してください。詳細については、BigQuery Java API のリファレンス ドキュメントをご覧ください。
import com.google.cloud.bigquery.BigQuery;
import com.google.cloud.bigquery.BigQueryException;
import com.google.cloud.bigquery.BigQueryOptions;
import com.google.cloud.bigquery.Clustering;
import com.google.cloud.bigquery.Field;
import com.google.cloud.bigquery.Schema;
import com.google.cloud.bigquery.StandardSQLTypeName;
import com.google.cloud.bigquery.StandardTableDefinition;
import com.google.cloud.bigquery.TableId;
import com.google.cloud.bigquery.TableInfo;
import com.google.cloud.bigquery.TimePartitioning;
import com.google.common.collect.ImmutableList;
import java.util.List;
public class CreateClusteredTable {
public static void main(String[] args) {
// TODO(developer): Replace these variables before running the sample.
String datasetName = "MY_DATASET_NAME";
String tableName = "MY_TABLE_NAME";
Schema schema =
Schema.of(
Field.of("name", StandardSQLTypeName.STRING),
Field.of("post_abbr", StandardSQLTypeName.STRING),
Field.of("date", StandardSQLTypeName.DATE));
createClusteredTable(datasetName, tableName, schema, ImmutableList.of("name", "post_abbr"));
}
public static void createClusteredTable(
String datasetName, String tableName, Schema schema, List<String> clusteringFields) {
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);
TimePartitioning partitioning = TimePartitioning.of(TimePartitioning.Type.DAY);
// Clustering fields will be consisted of fields mentioned in the schema.
// BigQuery supports clustering for both partitioned and non-partitioned tables.
Clustering clustering = Clustering.newBuilder().setFields(clusteringFields).build();
StandardTableDefinition tableDefinition =
StandardTableDefinition.newBuilder()
.setSchema(schema)
.setTimePartitioning(partitioning)
.setClustering(clustering)
.build();
TableInfo tableInfo = TableInfo.newBuilder(tableId, tableDefinition).build();
bigquery.create(tableInfo);
System.out.println("Clustered table created successfully");
} catch (BigQueryException e) {
System.out.println("Clustered table was not created. \n" + e.toString());
}
}
}