Search in sources :

Example 56 with SchemaBuilder

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

the class SchemaUtilTest method shouldCreateCorrectAvroSchemaWithNullableFields.

@Test
public void shouldCreateCorrectAvroSchemaWithNullableFields() {
    SchemaBuilder schemaBuilder = SchemaBuilder.struct();
    schemaBuilder.field("ordertime", Schema.INT64_SCHEMA).field("orderid", Schema.STRING_SCHEMA).field("itemid", Schema.STRING_SCHEMA).field("orderunits", Schema.FLOAT64_SCHEMA).field("arraycol", SchemaBuilder.array(Schema.FLOAT64_SCHEMA)).field("mapcol", SchemaBuilder.map(Schema.STRING_SCHEMA, Schema.FLOAT64_SCHEMA));
    String avroSchemaString = SchemaUtil.buildAvroSchema(schemaBuilder.build(), "orders");
    assertThat(avroSchemaString, equalTo("{\"type\":\"record\",\"name\":\"orders\",\"namespace\":\"ksql\",\"fields\":" + "[{\"name\":\"ordertime\",\"type\":[\"null\",\"long\"],\"default\":null},{\"name\":" + "\"orderid\",\"type\":[\"null\",\"string\"],\"default\":null},{\"name\":\"itemid\"," + "\"type\":[\"null\",\"string\"],\"default\":null},{\"name\":\"orderunits\",\"type\":" + "[\"null\",\"double\"],\"default\":null},{\"name\":\"arraycol\",\"type\":[\"null\"," + "{\"type\":\"array\",\"items\":[\"null\",\"double\"]}],\"default\":null},{\"name\":" + "\"mapcol\",\"type\":[\"null\",{\"type\":\"map\",\"values\":[\"null\",\"double\"]}]" + ",\"default\":null}]}"));
}
Also used : SchemaBuilder(org.apache.kafka.connect.data.SchemaBuilder) Test(org.junit.Test)

Example 57 with SchemaBuilder

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

the class QueryEngine method getResultDatasource.

StructuredDataSource getResultDatasource(final Select select, final String name) {
    SchemaBuilder dataSource = SchemaBuilder.struct().name(name);
    for (SelectItem selectItem : select.getSelectItems()) {
        if (selectItem instanceof SingleColumn) {
            SingleColumn singleColumn = (SingleColumn) selectItem;
            String fieldName = singleColumn.getAlias().get();
            dataSource = dataSource.field(fieldName, Schema.BOOLEAN_SCHEMA);
        }
    }
    KsqlTopic ksqlTopic = new KsqlTopic(name, name, null);
    return new KsqlStream("QueryEngine-DDLCommand-Not-Needed", name, dataSource.schema(), null, null, ksqlTopic);
}
Also used : KsqlStream(io.confluent.ksql.metastore.KsqlStream) SelectItem(io.confluent.ksql.parser.tree.SelectItem) SchemaBuilder(org.apache.kafka.connect.data.SchemaBuilder) SingleColumn(io.confluent.ksql.parser.tree.SingleColumn) KsqlTopic(io.confluent.ksql.metastore.KsqlTopic)

Example 58 with SchemaBuilder

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

the class ConsoleTest method buildTestSchema.

private List<SourceDescription.FieldSchemaInfo> buildTestSchema(int size) {
    SchemaBuilder dataSourceBuilder = SchemaBuilder.struct().name("TestSchema");
    for (int i = 0; i < size; i++) {
        dataSourceBuilder.field("f_" + i, SchemaUtil.getTypeSchema("STRING"));
    }
    List<SourceDescription.FieldSchemaInfo> res = new ArrayList<>();
    List<Field> fields = dataSourceBuilder.build().fields();
    for (Field field : fields) {
        res.add(new SourceDescription.FieldSchemaInfo(field.name(), SchemaUtil.getSchemaFieldName(field)));
    }
    return res;
}
Also used : Field(org.apache.kafka.connect.data.Field) SchemaBuilder(org.apache.kafka.connect.data.SchemaBuilder) ArrayList(java.util.ArrayList) SourceDescription(io.confluent.ksql.rest.entity.SourceDescription)

Example 59 with SchemaBuilder

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

the class SchemaUtil method removeImplicitRowTimeRowKeyFromSchema.

public static Schema removeImplicitRowTimeRowKeyFromSchema(Schema schema) {
    SchemaBuilder schemaBuilder = SchemaBuilder.struct();
    for (Field field : schema.fields()) {
        String fieldName = field.name();
        fieldName = fieldName.substring(fieldName.indexOf('.') + 1);
        if (!fieldName.equalsIgnoreCase(SchemaUtil.ROWTIME_NAME) && !fieldName.equalsIgnoreCase(SchemaUtil.ROWKEY_NAME)) {
            schemaBuilder.field(fieldName, field.schema());
        }
    }
    return schemaBuilder.build();
}
Also used : Field(org.apache.kafka.connect.data.Field) SchemaBuilder(org.apache.kafka.connect.data.SchemaBuilder)

Example 60 with SchemaBuilder

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

the class SchemaUtil method getSchemaWithNoAlias.

/**
 * Remove the alias when reading/writing from outside
 */
public static Schema getSchemaWithNoAlias(Schema schema) {
    SchemaBuilder schemaBuilder = SchemaBuilder.struct();
    for (Field field : schema.fields()) {
        String name = getFieldNameWithNoAlias(field);
        schemaBuilder.field(name, field.schema());
    }
    return schemaBuilder.build();
}
Also used : Field(org.apache.kafka.connect.data.Field) SchemaBuilder(org.apache.kafka.connect.data.SchemaBuilder)

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