[
https://issues.apache.org/jira/browse/BEAM-10654?focusedWorklogId=469302&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-469302
]
ASF GitHub Bot logged work on BEAM-10654:
-----------------------------------------
Author: ASF GitHub Bot
Created on: 11/Aug/20 16:45
Start Date: 11/Aug/20 16:45
Worklog Time Spent: 10m
Work Description: sclukas77 commented on a change in pull request #12498:
URL: https://github.com/apache/beam/pull/12498#discussion_r468720134
##########
File path:
sdks/java/extensions/schemaio-expansion-service/src/main/java/org/apache/beam/sdk/extensions/schemaio/expansion/ExternalSchemaIOTransformRegistrar.java
##########
@@ -0,0 +1,142 @@
+/*
+ * 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.extensions.schemaio.expansion;
+
+import com.google.auto.service.AutoService;
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.util.Map;
+import java.util.ServiceLoader;
+import javax.annotation.Nullable;
+import org.apache.beam.model.pipeline.v1.SchemaApi;
+import org.apache.beam.sdk.annotations.Experimental;
+import org.apache.beam.sdk.coders.RowCoder;
+import org.apache.beam.sdk.expansion.ExternalTransformRegistrar;
+import org.apache.beam.sdk.schemas.Schema;
+import org.apache.beam.sdk.schemas.SchemaTranslation;
+import org.apache.beam.sdk.schemas.io.SchemaIOProvider;
+import org.apache.beam.sdk.transforms.ExternalTransformBuilder;
+import org.apache.beam.sdk.transforms.PTransform;
+import org.apache.beam.sdk.values.PBegin;
+import org.apache.beam.sdk.values.PCollection;
+import org.apache.beam.sdk.values.PDone;
+import org.apache.beam.sdk.values.Row;
+import
org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap;
+
+@Experimental(Experimental.Kind.PORTABILITY)
+@AutoService(ExternalTransformRegistrar.class)
+public class ExternalSchemaIOTransformRegistrar implements
ExternalTransformRegistrar {
+
+ @Override
+ public Map<String, ExternalTransformBuilder<?, ?, ?>>
knownBuilderInstances() {
+ ImmutableMap.Builder builder = ImmutableMap.<String,
ExternalTransformRegistrar>builder();
+ try {
+ for (SchemaIOProvider schemaIOProvider :
ServiceLoader.load(SchemaIOProvider.class)) {
+ builder.put(
+ "beam:external:java:" + schemaIOProvider.identifier() + ":read:v1",
+ new ReaderBuilder(schemaIOProvider));
+ builder.put(
+ "beam:external:java:" + schemaIOProvider.identifier() +
":write:v1",
+ new WriterBuilder(schemaIOProvider));
+ }
+ } catch (Exception e) {
+ throw new RuntimeException(e.getMessage());
+ }
+ return builder.build();
+ }
+
+ public static class Configuration {
+ String location = "";
+ byte[] config = new byte[0];
+ @Nullable byte[] dataSchema = null;
+
+ public void setLocation(String location) {
+ this.location = location;
+ }
+
+ public void setConfig(byte[] config) {
+ this.config = config;
+ }
+
+ public void setDataSchema(byte[] dataSchema) {
+ this.dataSchema = dataSchema;
+ }
+ }
+
+ @Nullable
+ private static Schema translateSchema(@Nullable byte[] schemaBytes) throws
Exception {
+ if (schemaBytes == null) {
+ return null;
+ }
+ SchemaApi.Schema protoSchema = SchemaApi.Schema.parseFrom(schemaBytes);
+ return SchemaTranslation.schemaFromProto(protoSchema);
+ }
+
+ private static Row translateRow(byte[] rowBytes, Schema configSchema) throws
Exception {
+ RowCoder rowCoder = RowCoder.of(configSchema);
+ InputStream stream = new ByteArrayInputStream(rowBytes);
+ return rowCoder.decode(stream);
+ }
+
+ private static class ReaderBuilder
+ implements ExternalTransformBuilder<Configuration, PBegin,
PCollection<Row>> {
+ SchemaIOProvider schemaIOProvider;
+
+ ReaderBuilder(SchemaIOProvider schemaIOProvider) {
+ this.schemaIOProvider = schemaIOProvider;
+ }
+
+ @Override
+ public PTransform<PBegin, PCollection<Row>> buildExternal(Configuration
configuration) {
+ try {
+ return schemaIOProvider
+ .from(
+ configuration.location,
+ translateRow(configuration.config,
schemaIOProvider.configurationSchema()),
+ translateSchema(configuration.dataSchema))
+ .buildReader();
+ } catch (Exception e) {
+ throw new RuntimeException("Could not convert configuration proto to
row or schema.");
+ }
+ }
+ }
+
+ private static class WriterBuilder
Review comment:
ACK, I will add these in.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
Issue Time Tracking
-------------------
Worklog Id: (was: 469302)
Time Spent: 2.5h (was: 2h 20m)
> Implement ExternalSchemaIOTransformRegistrar, implement for jdbc
> ----------------------------------------------------------------
>
> Key: BEAM-10654
> URL: https://issues.apache.org/jira/browse/BEAM-10654
> Project: Beam
> Issue Type: Improvement
> Components: io-java-jdbc
> Reporter: Scott Lukas
> Assignee: Scott Lukas
> Priority: P2
> Labels: schema-io
> Time Spent: 2.5h
> Remaining Estimate: 0h
>
> Implement ExternalSchemaIOTransformRegistrar for cross language schemaIO
> transforms. Implement JdbcSchemaIOProvider to test registrar.
> Within project:
> [https://docs.google.com/document/d/1ic3P8EVGHIydHQ-VMDKbN9kEdwm7sBXMo80VrhwksvI/edit#heading=h.x9snb54sjlu9]
> [~bhulette]
--
This message was sent by Atlassian Jira
(v8.3.4#803005)