Search in sources :

Example 61 with SchemaBuilder

use of org.apache.kafka.connect.data.SchemaBuilder in project ksql by confluentinc.

the class SchemaKStream method createSelectValueMapperAndSchema.

Pair<Schema, SelectValueMapper> createSelectValueMapperAndSchema(final List<Pair<String, Expression>> expressionPairList) {
    try {
        final CodeGenRunner codeGenRunner = new CodeGenRunner(schema, functionRegistry);
        final SchemaBuilder schemaBuilder = SchemaBuilder.struct();
        final List<ExpressionMetadata> expressionEvaluators = new ArrayList<>();
        for (Pair<String, Expression> expressionPair : expressionPairList) {
            final ExpressionMetadata expressionEvaluator = codeGenRunner.buildCodeGenFromParseTree(expressionPair.getRight());
            schemaBuilder.field(expressionPair.getLeft(), expressionEvaluator.getExpressionType());
            expressionEvaluators.add(expressionEvaluator);
        }
        return new Pair<>(schemaBuilder.build(), new SelectValueMapper(genericRowValueTypeEnforcer, expressionPairList, expressionEvaluators));
    } catch (Exception e) {
        throw new KsqlException("Code generation failed for SelectValueMapper", e);
    }
}
Also used : ExpressionMetadata(io.confluent.ksql.util.ExpressionMetadata) CodeGenRunner(io.confluent.ksql.codegen.CodeGenRunner) DereferenceExpression(io.confluent.ksql.parser.tree.DereferenceExpression) Expression(io.confluent.ksql.parser.tree.Expression) SchemaBuilder(org.apache.kafka.connect.data.SchemaBuilder) ArrayList(java.util.ArrayList) KsqlException(io.confluent.ksql.util.KsqlException) KsqlException(io.confluent.ksql.util.KsqlException) Pair(io.confluent.ksql.util.Pair)

Example 62 with SchemaBuilder

use of org.apache.kafka.connect.data.SchemaBuilder in project ksql by confluentinc.

the class LogicalPlanner method buildAggregateNode.

private AggregateNode buildAggregateNode(final Schema inputSchema, final PlanNode sourcePlanNode) {
    SchemaBuilder aggregateSchema = SchemaBuilder.struct();
    ExpressionTypeManager expressionTypeManager = new ExpressionTypeManager(inputSchema, functionRegistry);
    for (int i = 0; i < analysis.getSelectExpressions().size(); i++) {
        Expression expression = analysis.getSelectExpressions().get(i);
        String alias = analysis.getSelectExpressionAlias().get(i);
        Schema expressionType = expressionTypeManager.getExpressionType(expression);
        aggregateSchema = aggregateSchema.field(alias, expressionType);
    }
    return new AggregateNode(new PlanNodeId("Aggregate"), sourcePlanNode, aggregateSchema, analysis.getGroupByExpressions(), analysis.getWindowExpression(), aggregateAnalysis.getAggregateFunctionArguments(), aggregateAnalysis.getFunctionList(), aggregateAnalysis.getRequiredColumnsList(), aggregateAnalysis.getFinalSelectExpressions(), aggregateAnalysis.getHavingExpression());
}
Also used : PlanNodeId(io.confluent.ksql.planner.plan.PlanNodeId) ExpressionTypeManager(io.confluent.ksql.util.ExpressionTypeManager) AggregateNode(io.confluent.ksql.planner.plan.AggregateNode) Expression(io.confluent.ksql.parser.tree.Expression) Schema(org.apache.kafka.connect.data.Schema) SchemaBuilder(org.apache.kafka.connect.data.SchemaBuilder)

Example 63 with SchemaBuilder

use of org.apache.kafka.connect.data.SchemaBuilder in project ksql by confluentinc.

the class AggregateNode method buildAggregateSchema.

private Schema buildAggregateSchema(final Schema schema, final FunctionRegistry functionRegistry) {
    final SchemaBuilder schemaBuilder = SchemaBuilder.struct();
    final List<Field> fields = schema.fields();
    for (int i = 0; i < getRequiredColumnList().size(); i++) {
        schemaBuilder.field(fields.get(i).name(), fields.get(i).schema());
    }
    for (int aggFunctionVarSuffix = 0; aggFunctionVarSuffix < getFunctionList().size(); aggFunctionVarSuffix++) {
        String udafName = getFunctionList().get(aggFunctionVarSuffix).getName().getSuffix();
        KsqlAggregateFunction aggregateFunction = functionRegistry.getAggregateFunction(udafName, getFunctionList().get(aggFunctionVarSuffix).getArguments(), schema);
        schemaBuilder.field(AggregateExpressionRewriter.AGGREGATE_FUNCTION_VARIABLE_PREFIX + aggFunctionVarSuffix, aggregateFunction.getReturnType());
    }
    return schemaBuilder.build();
}
Also used : KsqlAggregateFunction(io.confluent.ksql.function.KsqlAggregateFunction) Field(org.apache.kafka.connect.data.Field) SchemaBuilder(org.apache.kafka.connect.data.SchemaBuilder)

Example 64 with SchemaBuilder

use of org.apache.kafka.connect.data.SchemaBuilder in project ksql by confluentinc.

the class SerDeUtil method getSchemaFromAvro.

public static Schema getSchemaFromAvro(String avroSchemaString) {
    org.apache.avro.Schema.Parser parser = new org.apache.avro.Schema.Parser();
    org.apache.avro.Schema avroSchema = parser.parse(avroSchemaString);
    SchemaBuilder inferredSchema = SchemaBuilder.struct().name(avroSchema.getName());
    for (org.apache.avro.Schema.Field avroField : avroSchema.getFields()) {
        inferredSchema.field(avroField.name(), getKsqlSchemaForAvroSchema(avroField.schema()));
    }
    return inferredSchema.build();
}
Also used : Schema(org.apache.kafka.connect.data.Schema) SchemaBuilder(org.apache.kafka.connect.data.SchemaBuilder)

Example 65 with SchemaBuilder

use of org.apache.kafka.connect.data.SchemaBuilder in project connect-utils by jcustenborder.

the class SchemaBuildersTest method excludeFields.

@Test
public void excludeFields() {
    final Schema input = SchemaBuilder.struct().field("firstName", Schema.STRING_SCHEMA).field("lastName", Schema.STRING_SCHEMA).build();
    final Schema expected = SchemaBuilder.struct().field("firstName", Schema.STRING_SCHEMA).build();
    SchemaBuilder builder = SchemaBuilders.of(input, "lastName");
    assertEquals(expected, builder.build());
}
Also used : Schema(org.apache.kafka.connect.data.Schema) AssertSchema.assertSchema(com.github.jcustenborder.kafka.connect.utils.AssertSchema.assertSchema) SchemaBuilder(org.apache.kafka.connect.data.SchemaBuilder) Test(org.junit.jupiter.api.Test) DynamicTest.dynamicTest(org.junit.jupiter.api.DynamicTest.dynamicTest) DynamicTest(org.junit.jupiter.api.DynamicTest)

Aggregations

SchemaBuilder (org.apache.kafka.connect.data.SchemaBuilder)88 Schema (org.apache.kafka.connect.data.Schema)40 Test (org.junit.Test)40 Struct (org.apache.kafka.connect.data.Struct)23 Field (org.apache.kafka.connect.data.Field)13 SourceRecord (org.apache.kafka.connect.source.SourceRecord)13 Test (org.junit.jupiter.api.Test)9 ConnectSchema (org.apache.kafka.connect.data.ConnectSchema)6 BigDecimal (java.math.BigDecimal)5 Date (java.util.Date)5 Requirements.requireStruct (org.apache.kafka.connect.transforms.util.Requirements.requireStruct)5 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 DataException (org.apache.kafka.connect.errors.DataException)4 KsqlStream (io.confluent.ksql.metastore.KsqlStream)3 KsqlTopic (io.confluent.ksql.metastore.KsqlTopic)3 Expression (io.confluent.ksql.parser.tree.Expression)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 SelectItem (io.confluent.ksql.parser.tree.SelectItem)2 SingleColumn (io.confluent.ksql.parser.tree.SingleColumn)2