-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
TableRowToStorageApiProto.java
304 lines (285 loc) · 12.4 KB
/
TableRowToStorageApiProto.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.beam.sdk.io.gcp.bigquery;
import static java.util.stream.Collectors.toList;
import com.google.api.services.bigquery.model.TableFieldSchema;
import com.google.api.services.bigquery.model.TableRow;
import com.google.api.services.bigquery.model.TableSchema;
import com.google.protobuf.ByteString;
import com.google.protobuf.DescriptorProtos.DescriptorProto;
import com.google.protobuf.DescriptorProtos.FieldDescriptorProto;
import com.google.protobuf.DescriptorProtos.FieldDescriptorProto.Label;
import com.google.protobuf.DescriptorProtos.FieldDescriptorProto.Type;
import com.google.protobuf.DescriptorProtos.FileDescriptorProto;
import com.google.protobuf.Descriptors.Descriptor;
import com.google.protobuf.Descriptors.DescriptorValidationException;
import com.google.protobuf.Descriptors.FieldDescriptor;
import com.google.protobuf.Descriptors.FileDescriptor;
import com.google.protobuf.DynamicMessage;
import com.google.protobuf.Message;
import java.util.AbstractMap;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.function.Function;
import javax.annotation.Nullable;
import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.annotations.VisibleForTesting;
import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap;
import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Iterables;
import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.io.BaseEncoding;
/**
* Utility methods for converting JSON {@link TableRow} objects to dynamic protocol message, for use
* with the Storage write API.
*/
public class TableRowToStorageApiProto {
static final Map<String, Type> PRIMITIVE_TYPES =
ImmutableMap.<String, Type>builder()
.put("INT64", Type.TYPE_INT64)
.put("INTEGER", Type.TYPE_INT64)
.put("FLOAT64", Type.TYPE_DOUBLE)
.put("FLOAT", Type.TYPE_DOUBLE)
.put("STRING", Type.TYPE_STRING)
.put("BOOL", Type.TYPE_BOOL)
.put("BOOLEAN", Type.TYPE_BOOL)
.put("BYTES", Type.TYPE_BYTES)
.put("NUMERIC", Type.TYPE_STRING) // Pass through the JSON encoding.
.put("BIGNUMERIC", Type.TYPE_STRING) // Pass through the JSON encoding.
.put("GEOGRAPHY", Type.TYPE_STRING) // Pass through the JSON encoding.
.put("DATE", Type.TYPE_STRING) // Pass through the JSON encoding.
.put("TIME", Type.TYPE_STRING) // Pass through the JSON encoding.
.put("DATETIME", Type.TYPE_STRING) // Pass through the JSON encoding.
.put("TIMESTAMP", Type.TYPE_STRING) // Pass through the JSON encoding.
.put("JSON", Type.TYPE_STRING)
.build();
/**
* Given a BigQuery TableSchema, returns a protocol-buffer Descriptor that can be used to write
* data using the BigQuery Storage API.
*/
public static Descriptor getDescriptorFromTableSchema(TableSchema jsonSchema)
throws DescriptorValidationException {
DescriptorProto descriptorProto = descriptorSchemaFromTableSchema(jsonSchema);
FileDescriptorProto fileDescriptorProto =
FileDescriptorProto.newBuilder().addMessageType(descriptorProto).build();
FileDescriptor fileDescriptor =
FileDescriptor.buildFrom(fileDescriptorProto, new FileDescriptor[0]);
return Iterables.getOnlyElement(fileDescriptor.getMessageTypes());
}
/**
* Given a BigQuery TableRow, returns a protocol-buffer message that can be used to write data
* using the BigQuery Storage API.
*/
public static DynamicMessage messageFromTableRow(Descriptor descriptor, TableRow tableRow) {
DynamicMessage.Builder builder = DynamicMessage.newBuilder(descriptor);
for (Map.Entry<String, Object> entry : tableRow.entrySet()) {
@Nullable
FieldDescriptor fieldDescriptor = descriptor.findFieldByName(entry.getKey().toLowerCase());
if (fieldDescriptor == null) {
throw new RuntimeException(
"TableRow contained unexpected field with name " + entry.getKey());
}
@Nullable Object value = messageValueFromFieldValue(fieldDescriptor, entry.getValue());
if (value != null) {
builder.setField(fieldDescriptor, value);
}
}
return builder.build();
}
@VisibleForTesting
static DescriptorProto descriptorSchemaFromTableSchema(TableSchema tableSchema) {
return descriptorSchemaFromTableFieldSchemas(tableSchema.getFields());
}
private static DescriptorProto descriptorSchemaFromTableFieldSchemas(
Iterable<TableFieldSchema> tableFieldSchemas) {
DescriptorProto.Builder descriptorBuilder = DescriptorProto.newBuilder();
// Create a unique name for the descriptor ('-' characters cannot be used).
descriptorBuilder.setName("D" + UUID.randomUUID().toString().replace("-", "_"));
int i = 1;
for (TableFieldSchema fieldSchema : tableFieldSchemas) {
fieldDescriptorFromTableField(fieldSchema, i++, descriptorBuilder);
}
return descriptorBuilder.build();
}
private static void fieldDescriptorFromTableField(
TableFieldSchema fieldSchema, int fieldNumber, DescriptorProto.Builder descriptorBuilder) {
FieldDescriptorProto.Builder fieldDescriptorBuilder = FieldDescriptorProto.newBuilder();
fieldDescriptorBuilder = fieldDescriptorBuilder.setName(fieldSchema.getName().toLowerCase());
fieldDescriptorBuilder = fieldDescriptorBuilder.setNumber(fieldNumber);
switch (fieldSchema.getType()) {
case "STRUCT":
case "RECORD":
DescriptorProto nested = descriptorSchemaFromTableFieldSchemas(fieldSchema.getFields());
descriptorBuilder.addNestedType(nested);
fieldDescriptorBuilder =
fieldDescriptorBuilder.setType(Type.TYPE_MESSAGE).setTypeName(nested.getName());
break;
default:
@Nullable Type type = PRIMITIVE_TYPES.get(fieldSchema.getType());
if (type == null) {
throw new UnsupportedOperationException(
"Converting BigQuery type " + fieldSchema.getType() + " to Beam type is unsupported");
}
fieldDescriptorBuilder = fieldDescriptorBuilder.setType(type);
}
Optional<Mode> fieldMode = Optional.ofNullable(fieldSchema.getMode()).map(Mode::valueOf);
if (fieldMode.filter(m -> m == Mode.REPEATED).isPresent()) {
fieldDescriptorBuilder = fieldDescriptorBuilder.setLabel(Label.LABEL_REPEATED);
} else if (!fieldMode.isPresent() || fieldMode.filter(m -> m == Mode.NULLABLE).isPresent()) {
fieldDescriptorBuilder = fieldDescriptorBuilder.setLabel(Label.LABEL_OPTIONAL);
} else {
fieldDescriptorBuilder = fieldDescriptorBuilder.setLabel(Label.LABEL_REQUIRED);
}
descriptorBuilder.addField(fieldDescriptorBuilder.build());
}
@Nullable
private static Object messageValueFromFieldValue(
FieldDescriptor fieldDescriptor, Object bqValue) {
if (bqValue == null) {
if (fieldDescriptor.isOptional()) {
return null;
} else if (fieldDescriptor.isRepeated()) {
return Collections.emptyList();
}
{
throw new IllegalArgumentException(
"Received null value for non-nullable field " + fieldDescriptor.getName());
}
}
return toProtoValue(fieldDescriptor, bqValue, fieldDescriptor.isRepeated());
}
private static final Map<FieldDescriptor.Type, Function<String, Object>>
JSON_PROTO_STRING_PARSERS =
ImmutableMap.<FieldDescriptor.Type, Function<String, Object>>builder()
.put(FieldDescriptor.Type.INT32, Integer::valueOf)
.put(FieldDescriptor.Type.INT64, Long::valueOf)
.put(FieldDescriptor.Type.FLOAT, Float::valueOf)
.put(FieldDescriptor.Type.DOUBLE, Double::valueOf)
.put(FieldDescriptor.Type.BOOL, Boolean::valueOf)
.put(FieldDescriptor.Type.STRING, str -> str)
.put(
FieldDescriptor.Type.BYTES,
b64 -> ByteString.copyFrom(BaseEncoding.base64().decode(b64)))
.build();
@Nullable
@SuppressWarnings({"nullness"})
@VisibleForTesting
static Object toProtoValue(
FieldDescriptor fieldDescriptor, Object jsonBQValue, boolean isRepeated) {
if (isRepeated) {
return ((List<Object>) jsonBQValue)
.stream().map(v -> toProtoValue(fieldDescriptor, v, false)).collect(toList());
}
if (fieldDescriptor.getType() == FieldDescriptor.Type.MESSAGE) {
if (jsonBQValue instanceof AbstractMap) {
// This will handle nested rows.
TableRow tr = new TableRow();
tr.putAll((AbstractMap<String, Object>) jsonBQValue);
return messageFromTableRow(fieldDescriptor.getMessageType(), tr);
} else {
throw new RuntimeException("Unexpected value " + jsonBQValue + " Expected a JSON map.");
}
}
@Nullable Object scalarValue = scalarToProtoValue(fieldDescriptor, jsonBQValue);
if (scalarValue == null) {
return toProtoValue(fieldDescriptor, jsonBQValue.toString(), isRepeated);
} else {
return scalarValue;
}
}
@VisibleForTesting
@Nullable
static Object scalarToProtoValue(FieldDescriptor fieldDescriptor, Object jsonBQValue) {
if (jsonBQValue instanceof String) {
Function<String, Object> mapper = JSON_PROTO_STRING_PARSERS.get(fieldDescriptor.getType());
if (mapper == null) {
throw new UnsupportedOperationException(
"Converting BigQuery type '"
+ jsonBQValue.getClass()
+ "' to '"
+ fieldDescriptor
+ "' is not supported");
}
return mapper.apply((String) jsonBQValue);
}
switch (fieldDescriptor.getType()) {
case BOOL:
if (jsonBQValue instanceof Boolean) {
return jsonBQValue;
}
break;
case BYTES:
break;
case INT64:
if (jsonBQValue instanceof Integer) {
return Long.valueOf((Integer) jsonBQValue);
} else if (jsonBQValue instanceof Long) {
return jsonBQValue;
}
break;
case INT32:
if (jsonBQValue instanceof Integer) {
return jsonBQValue;
}
break;
case STRING:
break;
case DOUBLE:
if (jsonBQValue instanceof Double) {
return jsonBQValue;
} else if (jsonBQValue instanceof Float) {
return Double.valueOf((Float) jsonBQValue);
}
break;
default:
throw new RuntimeException("Unsupported proto type " + fieldDescriptor.getType());
}
return null;
}
@VisibleForTesting
public static TableRow tableRowFromMessage(Message message) {
TableRow tableRow = new TableRow();
for (Map.Entry<FieldDescriptor, Object> field : message.getAllFields().entrySet()) {
FieldDescriptor fieldDescriptor = field.getKey();
Object fieldValue = field.getValue();
tableRow.putIfAbsent(
fieldDescriptor.getName(), jsonValueFromMessageValue(fieldDescriptor, fieldValue, true));
}
return tableRow;
}
public static Object jsonValueFromMessageValue(
FieldDescriptor fieldDescriptor, Object fieldValue, boolean expandRepeated) {
if (expandRepeated && fieldDescriptor.isRepeated()) {
List<Object> valueList = (List<Object>) fieldValue;
return valueList.stream()
.map(v -> jsonValueFromMessageValue(fieldDescriptor, v, false))
.collect(toList());
}
switch (fieldDescriptor.getType()) {
case GROUP:
case MESSAGE:
return tableRowFromMessage((Message) fieldValue);
case BYTES:
return BaseEncoding.base64().encode(((ByteString) fieldValue).toByteArray());
case ENUM:
throw new RuntimeException("Enumerations not supported");
default:
return fieldValue.toString();
}
}
}