Search in sources :

Example 1 with Experimental

use of org.apache.beam.sdk.annotations.Experimental in project beam by apache.

the class ProtoByteBuddyUtils method createBuilderCreator.

@Experimental(Kind.SCHEMAS)
static <ProtoBuilderT extends MessageLite.Builder> SchemaUserTypeCreator createBuilderCreator(Class<?> protoClass, Class<?> builderClass, List<FieldValueSetter<ProtoBuilderT, Object>> setters, Schema schema) {
    try {
        DynamicType.Builder<Supplier> builder = BYTE_BUDDY.with(new InjectPackageStrategy(builderClass)).subclass(Supplier.class).method(ElementMatchers.named("get")).intercept(new BuilderSupplier(protoClass));
        Supplier supplier = builder.visit(new AsmVisitorWrapper.ForDeclaredMethods().writerFlags(ClassWriter.COMPUTE_FRAMES)).make().load(ReflectHelpers.findClassLoader(), ClassLoadingStrategy.Default.INJECTION).getLoaded().getDeclaredConstructor().newInstance();
        return new ProtoCreatorFactory<>(supplier, setters);
    } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
        throw new RuntimeException("Unable to generate a creator for class " + builderClass + " with schema " + schema);
    }
}
Also used : DynamicType(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.dynamic.DynamicType) InvocationTargetException(java.lang.reflect.InvocationTargetException) InjectPackageStrategy(org.apache.beam.sdk.schemas.utils.ByteBuddyUtils.InjectPackageStrategy) AsmVisitorWrapper(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.asm.AsmVisitorWrapper) FieldValueTypeSupplier(org.apache.beam.sdk.schemas.utils.FieldValueTypeSupplier) Supplier(java.util.function.Supplier) Experimental(org.apache.beam.sdk.annotations.Experimental)

Example 2 with Experimental

use of org.apache.beam.sdk.annotations.Experimental in project beam by apache.

the class TestBigQuery method insertRows.

@Experimental(Kind.SCHEMAS)
public TableDataInsertAllResponse insertRows(Schema rowSchema, Row... rows) throws IOException {
    List<Rows> bqRows = Arrays.stream(rows).map(row -> new Rows().setJson(BigQueryUtils.toTableRow(row))).collect(ImmutableList.toImmutableList());
    Bigquery bq = newBigQueryClient(pipelineOptions);
    return bq.tabledata().insertAll(pipelineOptions.getBigQueryProject() == null ? pipelineOptions.getProject() : pipelineOptions.getBigQueryProject(), pipelineOptions.getTargetDataset(), table.getTableReference().getTableId(), new TableDataInsertAllRequest().setRows(bqRows)).setPrettyPrint(false).execute();
}
Also used : NullCredentialInitializer(org.apache.beam.sdk.extensions.gcp.auth.NullCredentialInitializer) Statement(org.junit.runners.model.Statement) Arrays(java.util.Arrays) TestPipelineOptions(org.apache.beam.sdk.testing.TestPipelineOptions) TestRule(org.junit.rules.TestRule) Experimental(org.apache.beam.sdk.annotations.Experimental) Duration(org.joda.time.Duration) Rows(com.google.api.services.bigquery.model.TableDataInsertAllRequest.Rows) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer) Kind(org.apache.beam.sdk.annotations.Experimental.Kind) Transport(org.apache.beam.sdk.extensions.gcp.util.Transport) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) TestPipeline(org.apache.beam.sdk.testing.TestPipeline) TableRow(com.google.api.services.bigquery.model.TableRow) TableSchema(com.google.api.services.bigquery.model.TableSchema) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Row(org.apache.beam.sdk.values.Row) DateTimeFormat(org.joda.time.format.DateTimeFormat) TableReference(com.google.api.services.bigquery.model.TableReference) RetryHttpRequestInitializer(org.apache.beam.sdk.extensions.gcp.util.RetryHttpRequestInitializer) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) DateTime(org.joda.time.DateTime) Credentials(com.google.auth.Credentials) IOException(java.io.IOException) FieldType(org.apache.beam.sdk.schemas.Schema.FieldType) Description(org.junit.runner.Description) Seconds.secondsBetween(org.joda.time.Seconds.secondsBetween) Collectors(java.util.stream.Collectors) Schema(org.apache.beam.sdk.schemas.Schema) Table(com.google.api.services.bigquery.model.Table) List(java.util.List) Bigquery(com.google.api.services.bigquery.Bigquery) TableDataInsertAllResponse(com.google.api.services.bigquery.model.TableDataInsertAllResponse) TableDataInsertAllRequest(com.google.api.services.bigquery.model.TableDataInsertAllRequest) HttpCredentialsAdapter(com.google.auth.http.HttpCredentialsAdapter) Matcher(org.hamcrest.Matcher) Instant(org.joda.time.Instant) ChainingHttpRequestInitializer(com.google.cloud.hadoop.util.ChainingHttpRequestInitializer) ImmutableList(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList) Collections(java.util.Collections) TableDataInsertAllRequest(com.google.api.services.bigquery.model.TableDataInsertAllRequest) Bigquery(com.google.api.services.bigquery.Bigquery) Rows(com.google.api.services.bigquery.model.TableDataInsertAllRequest.Rows) Experimental(org.apache.beam.sdk.annotations.Experimental)

Example 3 with Experimental

use of org.apache.beam.sdk.annotations.Experimental in project beam by apache.

the class PubsubIO method readAvroGenericRecords.

/**
 * Returns a {@link PTransform} that continuously reads binary encoded Avro messages into the Avro
 * {@link GenericRecord} type.
 *
 * <p>Beam will infer a schema for the Avro schema. This allows the output to be used by SQL and
 * by the schema-transform library.
 */
@Experimental(Kind.SCHEMAS)
public static Read<GenericRecord> readAvroGenericRecords(org.apache.avro.Schema avroSchema) {
    Schema schema = AvroUtils.getSchema(GenericRecord.class, avroSchema);
    AvroCoder<GenericRecord> coder = AvroCoder.of(GenericRecord.class, avroSchema);
    return Read.newBuilder(parsePayloadUsingCoder(coder)).setCoder(SchemaCoder.of(schema, TypeDescriptor.of(GenericRecord.class), AvroUtils.getToRowFunction(GenericRecord.class, avroSchema), AvroUtils.getFromRowFunction(GenericRecord.class))).build();
}
Also used : ProtoDynamicMessageSchema(org.apache.beam.sdk.extensions.protobuf.ProtoDynamicMessageSchema) Schema(org.apache.beam.sdk.schemas.Schema) GenericRecord(org.apache.avro.generic.GenericRecord) Experimental(org.apache.beam.sdk.annotations.Experimental)

Example 4 with Experimental

use of org.apache.beam.sdk.annotations.Experimental in project beam by apache.

the class PubsubIO method readAvrosWithBeamSchema.

/**
 * Returns a {@link PTransform} that continuously reads binary encoded Avro messages of the
 * specific type.
 *
 * <p>Beam will infer a schema for the Avro schema. This allows the output to be used by SQL and
 * by the schema-transform library.
 */
@Experimental(Kind.SCHEMAS)
public static <T> Read<T> readAvrosWithBeamSchema(Class<T> clazz) {
    if (clazz.equals(GenericRecord.class)) {
        throw new IllegalArgumentException("For GenericRecord, please call readAvroGenericRecords");
    }
    org.apache.avro.Schema avroSchema = ReflectData.get().getSchema(clazz);
    AvroCoder<T> coder = AvroCoder.of(clazz);
    Schema schema = AvroUtils.getSchema(clazz, null);
    return Read.newBuilder(parsePayloadUsingCoder(coder)).setCoder(SchemaCoder.of(schema, TypeDescriptor.of(clazz), AvroUtils.getToRowFunction(clazz, avroSchema), AvroUtils.getFromRowFunction(clazz))).build();
}
Also used : ProtoDynamicMessageSchema(org.apache.beam.sdk.extensions.protobuf.ProtoDynamicMessageSchema) Schema(org.apache.beam.sdk.schemas.Schema) Experimental(org.apache.beam.sdk.annotations.Experimental)

Example 5 with Experimental

use of org.apache.beam.sdk.annotations.Experimental in project beam by apache.

the class BigQueryUtils method fromTableFieldSchemaType.

/**
 * Get the Beam {@link FieldType} from a BigQuery type name.
 *
 * <p>Supports both standard and legacy SQL types.
 *
 * @param typeName Name of the type
 * @param nestedFields Nested fields for the given type (eg. RECORD type)
 * @return Corresponding Beam {@link FieldType}
 */
@Experimental(Kind.SCHEMAS)
private static FieldType fromTableFieldSchemaType(String typeName, List<TableFieldSchema> nestedFields, SchemaConversionOptions options) {
    switch(typeName) {
        case "STRING":
            return FieldType.STRING;
        case "BYTES":
            return FieldType.BYTES;
        case "INT64":
        case "INTEGER":
            return FieldType.INT64;
        case "FLOAT64":
        case "FLOAT":
            return FieldType.DOUBLE;
        case "BOOL":
        case "BOOLEAN":
            return FieldType.BOOLEAN;
        case "NUMERIC":
            return FieldType.DECIMAL;
        case "TIMESTAMP":
            return FieldType.DATETIME;
        case "TIME":
            return FieldType.logicalType(SqlTypes.TIME);
        case "DATE":
            return FieldType.logicalType(SqlTypes.DATE);
        case "DATETIME":
            return FieldType.logicalType(SqlTypes.DATETIME);
        case "STRUCT":
        case "RECORD":
            if (options.getInferMaps() && nestedFields.size() == 2) {
                TableFieldSchema key = nestedFields.get(0);
                TableFieldSchema value = nestedFields.get(1);
                if (BIGQUERY_MAP_KEY_FIELD_NAME.equals(key.getName()) && BIGQUERY_MAP_VALUE_FIELD_NAME.equals(value.getName())) {
                    return FieldType.map(fromTableFieldSchemaType(key.getType(), key.getFields(), options), fromTableFieldSchemaType(value.getType(), value.getFields(), options));
                }
            }
            Schema rowSchema = fromTableFieldSchema(nestedFields, options);
            return FieldType.row(rowSchema);
        default:
            throw new UnsupportedOperationException("Converting BigQuery type " + typeName + " to Beam type is unsupported");
    }
}
Also used : TableSchema(com.google.api.services.bigquery.model.TableSchema) TableFieldSchema(com.google.api.services.bigquery.model.TableFieldSchema) Schema(org.apache.beam.sdk.schemas.Schema) TableFieldSchema(com.google.api.services.bigquery.model.TableFieldSchema) Experimental(org.apache.beam.sdk.annotations.Experimental)

Aggregations

Experimental (org.apache.beam.sdk.annotations.Experimental)7 TableSchema (com.google.api.services.bigquery.model.TableSchema)4 Schema (org.apache.beam.sdk.schemas.Schema)4 IOException (java.io.IOException)3 TableReference (com.google.api.services.bigquery.model.TableReference)2 ProtoDynamicMessageSchema (org.apache.beam.sdk.extensions.protobuf.ProtoDynamicMessageSchema)2 HttpRequestInitializer (com.google.api.client.http.HttpRequestInitializer)1 Bigquery (com.google.api.services.bigquery.Bigquery)1 JobStatistics (com.google.api.services.bigquery.model.JobStatistics)1 Table (com.google.api.services.bigquery.model.Table)1 TableDataInsertAllRequest (com.google.api.services.bigquery.model.TableDataInsertAllRequest)1 Rows (com.google.api.services.bigquery.model.TableDataInsertAllRequest.Rows)1 TableDataInsertAllResponse (com.google.api.services.bigquery.model.TableDataInsertAllResponse)1 TableFieldSchema (com.google.api.services.bigquery.model.TableFieldSchema)1 TableRow (com.google.api.services.bigquery.model.TableRow)1 Credentials (com.google.auth.Credentials)1 HttpCredentialsAdapter (com.google.auth.http.HttpCredentialsAdapter)1 ChainingHttpRequestInitializer (com.google.cloud.hadoop.util.ChainingHttpRequestInitializer)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Arrays (java.util.Arrays)1