-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(samples): update WriteToDefaultStream.java sample (#1305)
Towards #1249 BQToBQStorageSchemaConverter.java will go into google-cloud-bigquery eventually. For now it acts as a resource for developers who want to use default stream to write.
- Loading branch information
1 parent
7d08d7b
commit 83c8e23
Showing
4 changed files
with
86 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
samples/snippets/src/main/java/com/example/bigquerystorage/BQToBQStorageSchemaConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package com.example.bigquerystorage; | ||
|
||
import com.google.cloud.bigquery.Field; | ||
import com.google.cloud.bigquery.Schema; | ||
import com.google.cloud.bigquery.StandardSQLTypeName; | ||
import com.google.cloud.bigquery.storage.v1beta2.TableFieldSchema; | ||
import com.google.cloud.bigquery.storage.v1beta2.TableSchema; | ||
import com.google.common.collect.ImmutableMap; | ||
|
||
/** Converts structure from BigQuery client to BigQueryStorage client */ | ||
public class BQToBQStorageSchemaConverter { | ||
private static ImmutableMap<Field.Mode, TableFieldSchema.Mode> BQTableSchemaModeMap = | ||
ImmutableMap.of( | ||
Field.Mode.NULLABLE, TableFieldSchema.Mode.NULLABLE, | ||
Field.Mode.REPEATED, TableFieldSchema.Mode.REPEATED, | ||
Field.Mode.REQUIRED, TableFieldSchema.Mode.REQUIRED); | ||
|
||
private static ImmutableMap<StandardSQLTypeName, TableFieldSchema.Type> BQTableSchemaTypeMap = | ||
new ImmutableMap.Builder<StandardSQLTypeName, TableFieldSchema.Type>() | ||
.put(StandardSQLTypeName.BOOL, TableFieldSchema.Type.BOOL) | ||
.put(StandardSQLTypeName.BYTES, TableFieldSchema.Type.BYTES) | ||
.put(StandardSQLTypeName.DATE, TableFieldSchema.Type.DATE) | ||
.put(StandardSQLTypeName.DATETIME, TableFieldSchema.Type.DATETIME) | ||
.put(StandardSQLTypeName.FLOAT64, TableFieldSchema.Type.DOUBLE) | ||
.put(StandardSQLTypeName.GEOGRAPHY, TableFieldSchema.Type.GEOGRAPHY) | ||
.put(StandardSQLTypeName.INT64, TableFieldSchema.Type.INT64) | ||
.put(StandardSQLTypeName.NUMERIC, TableFieldSchema.Type.NUMERIC) | ||
.put(StandardSQLTypeName.STRING, TableFieldSchema.Type.STRING) | ||
.put(StandardSQLTypeName.STRUCT, TableFieldSchema.Type.STRUCT) | ||
.put(StandardSQLTypeName.TIME, TableFieldSchema.Type.TIME) | ||
.put(StandardSQLTypeName.TIMESTAMP, TableFieldSchema.Type.TIMESTAMP) | ||
.build(); | ||
|
||
/** | ||
* Converts from BigQuery client Table Schema to bigquery storage API Table Schema. | ||
* | ||
* @param schema the BigQuery client Table Schema | ||
* @return the bigquery storage API Table Schema | ||
*/ | ||
public static TableSchema ConvertTableSchema(Schema schema) { | ||
TableSchema.Builder result = TableSchema.newBuilder(); | ||
for (int i = 0; i < schema.getFields().size(); i++) { | ||
result.addFields(i, ConvertFieldSchema(schema.getFields().get(i))); | ||
} | ||
return result.build(); | ||
} | ||
|
||
/** | ||
* Converts from bigquery v2 Field Schema to bigquery storage API Field Schema. | ||
* | ||
* @param field the BigQuery client Field Schema | ||
* @return the bigquery storage API Field Schema | ||
*/ | ||
public static TableFieldSchema ConvertFieldSchema(Field field) { | ||
TableFieldSchema.Builder result = TableFieldSchema.newBuilder(); | ||
if (field.getMode() == null) { | ||
field = field.toBuilder().setMode(Field.Mode.NULLABLE).build(); | ||
} | ||
result.setMode(BQTableSchemaModeMap.get(field.getMode())); | ||
result.setName(field.getName()); | ||
result.setType(BQTableSchemaTypeMap.get(field.getType().getStandardType())); | ||
if (field.getDescription() != null) { | ||
result.setDescription(field.getDescription()); | ||
} | ||
if (field.getSubFields() != null) { | ||
for (int i = 0; i < field.getSubFields().size(); i++) { | ||
result.addFields(i, ConvertFieldSchema(field.getSubFields().get(i))); | ||
} | ||
} | ||
return result.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters