[ https://issues.apache.org/jira/browse/BEAM-4076?focusedWorklogId=122942&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-122942 ]
ASF GitHub Bot logged work on BEAM-4076: ---------------------------------------- Author: ASF GitHub Bot Created on: 13/Jul/18 16:39 Start Date: 13/Jul/18 16:39 Worklog Time Spent: 10m Work Description: apilloud commented on a change in pull request #5941: [BEAM-4076] Schema utilities for converting between types URL: https://github.com/apache/beam/pull/5941#discussion_r202404887 ########## File path: sdks/java/core/src/main/java/org/apache/beam/sdk/schemas/transforms/Convert.java ########## @@ -0,0 +1,162 @@ +/* + * 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.schemas.transforms; + +import javax.annotation.Nullable; +import org.apache.beam.sdk.annotations.Experimental; +import org.apache.beam.sdk.annotations.Experimental.Kind; +import org.apache.beam.sdk.schemas.NoSuchSchemaException; +import org.apache.beam.sdk.schemas.SchemaCoder; +import org.apache.beam.sdk.schemas.SchemaRegistry; +import org.apache.beam.sdk.transforms.DoFn; +import org.apache.beam.sdk.transforms.PTransform; +import org.apache.beam.sdk.transforms.ParDo; +import org.apache.beam.sdk.transforms.SerializableFunctions; +import org.apache.beam.sdk.values.PCollection; +import org.apache.beam.sdk.values.Row; +import org.apache.beam.sdk.values.TypeDescriptor; + +/** A set of utilities for converting between different objects supporting schemas. */ +@Experimental(Kind.SCHEMAS) +public class Convert { + /** + * Convert a {@link PCollection<InputT>} into a {@link PCollection<Row>}. + * + * <p>The input {@link PCollection} must have a schema attached. The output collection will have + * the same schema as the iput. + */ + public static <InputT> PTransform<PCollection<InputT>, PCollection<Row>> toRows() { + return to(Row.class); + } + + /** + * Convert a {@link PCollection<Row>} into a {@link PCollection<OutputT>}. + * + * <p>The output schema will be inferred using the schema registry. A schema must be registered + * for this type, or the conversion will fail. + */ + public static <OutputT> PTransform<PCollection<Row>, PCollection<OutputT>> fromRows( + Class<OutputT> clazz) { + return to(clazz); + } + + /** + * Convert a {@link PCollection<Row>} into a {@link PCollection<OutputT>}. + * + * <p>The output schema will be inferred using the schema registry. A schema must be registered + * for this type, or the conversion will fail. + */ + public static <OutputT> PTransform<PCollection<Row>, PCollection<OutputT>> fromRows( + TypeDescriptor<OutputT> typeDescriptor) { + return to(typeDescriptor); + } + + /** + * Convert a {@link PCollection<InputT>} to a {@link PCollection<OutputT>}. + * + * <p>This function allows converting between two types as long as the two types have + * <i>compatible</i> schemas. Two schemas are said to be <i>compatible</i> if they recursively + * have fields with the same names, but possibly different orders. + */ + public static <InputT, OutputT> PTransform<PCollection<InputT>, PCollection<OutputT>> to( + Class<OutputT> clazz) { + return to(TypeDescriptor.of(clazz)); + } + + /** + * Convert a {@link PCollection<InputT>} to a {@link PCollection<OutputT>}. + * + * <p>This function allows converting between two types as long as the two types have + * <i>compatible</i> schemas. Two schemas are said to be <i>compatible</i> if they recursively + * have fields with the same names, but possibly different orders. + */ + public static <InputT, OutputT> PTransform<PCollection<InputT>, PCollection<OutputT>> to( + TypeDescriptor<OutputT> typeDescriptor) { + return new ConvertTransform<>(typeDescriptor); + } + + private static class ConvertTransform<InputT, OutputT> + extends PTransform<PCollection<InputT>, PCollection<OutputT>> { + @Nullable TypeDescriptor<OutputT> outputTypeDescriptor = null; + SchemaCoder<OutputT> outputSchemaCoder; + + ConvertTransform(Class<OutputT> outputClass) { + this(TypeDescriptor.of(outputClass)); + } + + ConvertTransform(TypeDescriptor<OutputT> outputTypeDescriptor) { + this.outputTypeDescriptor = outputTypeDescriptor; + } + + @Override + @SuppressWarnings("unchecked") Review comment: This seems related to the incorrect `@Nullable` annotation above. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org Issue Time Tracking ------------------- Worklog Id: (was: 122942) Time Spent: 9h 20m (was: 9h 10m) > Schema followups > ---------------- > > Key: BEAM-4076 > URL: https://issues.apache.org/jira/browse/BEAM-4076 > Project: Beam > Issue Type: Improvement > Components: beam-model, dsl-sql, sdk-java-core > Reporter: Kenneth Knowles > Priority: Major > Time Spent: 9h 20m > Remaining Estimate: 0h > > This umbrella bug contains subtasks with followups for Beam schemas, which > were moved from SQL to the core Java SDK and made to be type-name-based > rather than coder based. -- This message was sent by Atlassian JIRA (v7.6.3#76005)