Java Code Examples for org.apache.kafka.connect.data.SchemaBuilder#array()

The following examples show how to use org.apache.kafka.connect.data.SchemaBuilder#array() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SerDeUtil.java    From ksql-fork-with-deep-learning-function with Apache License 2.0 6 votes vote down vote up
private static Schema getKsqlSchemaForAvroSchema(org.apache.avro.Schema avroSchema) {
  switch (avroSchema.getType()) {
    case INT:
      return Schema.INT32_SCHEMA;
    case LONG:
      return Schema.INT64_SCHEMA;
    case DOUBLE:
    case FLOAT:
      return Schema.FLOAT64_SCHEMA;
    case BOOLEAN:
      return Schema.BOOLEAN_SCHEMA;
    case STRING:
      return Schema.STRING_SCHEMA;
    case ARRAY:
      return SchemaBuilder.array(getKsqlSchemaForAvroSchema(avroSchema.getElementType()));
    case MAP:
      return SchemaBuilder.map(Schema.STRING_SCHEMA,
                               getKsqlSchemaForAvroSchema(avroSchema.getValueType()));
    case UNION:
      return handleUnion(avroSchema);
      
    default:
      throw new KsqlException(String.format("KSQL doesn't currently support Avro type: %s",
                                            avroSchema.getFullName()));
  }
}
 
Example 2
Source File: SchemaUtil.java    From ksql-fork-with-deep-learning-function with Apache License 2.0 6 votes vote down vote up
private static Schema getKsqlComplexType(final String sqlType) {
  if (sqlType.startsWith(ARRAY)) {
    return SchemaBuilder.array(
        getTypeSchema(
            sqlType.substring(
                ARRAY.length() + 1,
                sqlType.length() - 1
            )
        )
    );
  } else if (sqlType.startsWith(MAP)) {
    //TODO: For now only primitive data types for map are supported. Will have to add nested
    // types.
    String[] mapTypesStrs = sqlType
        .substring("MAP".length() + 1, sqlType.length() - 1)
        .trim()
        .split(",");
    if (mapTypesStrs.length != 2) {
      throw new KsqlException("Map type is not defined correctly.: " + sqlType);
    }
    String keyType = mapTypesStrs[0].trim();
    String valueType = mapTypesStrs[1].trim();
    return SchemaBuilder.map(getTypeSchema(keyType), getTypeSchema(valueType));
  }
  throw new KsqlException("Unsupported type: " + sqlType);
}
 
Example 3
Source File: SetTypeDeserializer.java    From debezium-incubator with Apache License 2.0 5 votes vote down vote up
@Override
public SchemaBuilder getSchemaBuilder(AbstractType<?> abstractType) {
    SetType<?> listType = (SetType<?>) abstractType;
    AbstractType<?> elementsType = listType.getElementsType();
    Schema innerSchema = CassandraTypeDeserializer.getSchemaBuilder(elementsType).build();
    return SchemaBuilder.array(innerSchema);
}
 
Example 4
Source File: ListTypeDeserializer.java    From debezium-incubator with Apache License 2.0 5 votes vote down vote up
@Override
public SchemaBuilder getSchemaBuilder(AbstractType<?> abstractType) {
    ListType<?> listType = (ListType<?>) abstractType;
    AbstractType<?> elementsType = listType.getElementsType();
    Schema innerSchema = CassandraTypeDeserializer.getSchemaBuilder(elementsType).build();
    return SchemaBuilder.array(innerSchema);
}
 
Example 5
Source File: SchemaBuilders.java    From connect-utils with Apache License 2.0 5 votes vote down vote up
public static SchemaBuilder of(Schema schema, Collection<String> excludeFields) {

    Set<String> exclude = null != excludeFields ? ImmutableSet.copyOf(excludeFields) : ImmutableSet.of();
    SchemaBuilder builder;

    if (Schema.Type.ARRAY == schema.type()) {
      builder = SchemaBuilder.array(schema.valueSchema());
    } else if (Schema.Type.MAP == schema.type()) {
      builder = SchemaBuilder.map(schema.keySchema(), schema.valueSchema());
    } else {
      builder = SchemaBuilder.type(schema.type());
    }

    if (schema.isOptional()) {
      builder.optional();
    }
    if (!Strings.isNullOrEmpty(schema.name())) {
      builder.name(schema.name());
    }
    if (!Strings.isNullOrEmpty(schema.doc())) {
      builder.doc(schema.doc());
    }
    builder.version(schema.version());

    if (null != schema.parameters()) {
      builder.parameters(schema.parameters());
    }

    if (Schema.Type.STRUCT == schema.type()) {
      schema.fields()
          .stream()
          .filter(field -> !exclude.contains(field.name()))
          .forEach(field -> builder.field(field.name(), field.schema()));
    }

    return builder;
  }
 
Example 6
Source File: SchemaUtilTest.java    From ksql-fork-with-deep-learning-function with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldGetTheCorrectJavaTypeForArray() {
  Schema schema = SchemaBuilder.array(Schema.FLOAT64_SCHEMA);
  Class javaClass = SchemaUtil.getJavaType(schema);
  assertThat(javaClass, equalTo(new Double[]{}.getClass()));
}
 
Example 7
Source File: SchemaSerializationModule.java    From connect-utils with Apache License 2.0 4 votes vote down vote up
public Schema build() {
  log.trace(this.toString());
  SchemaBuilder builder;

  switch (this.type) {
    case MAP:
      Preconditions.checkNotNull(this.keySchema, "keySchema cannot be null.");
      Preconditions.checkNotNull(this.valueSchema, "valueSchema cannot be null.");
      builder = SchemaBuilder.map(this.keySchema, this.valueSchema);
      break;
    case ARRAY:
      Preconditions.checkNotNull(this.valueSchema, "valueSchema cannot be null.");
      builder = SchemaBuilder.array(this.valueSchema);
      break;
    default:
      builder = SchemaBuilder.type(this.type);
      break;
  }

  if (Schema.Type.STRUCT == this.type) {
    for (Map.Entry<String, Schema> kvp : this.fieldSchemas.entrySet()) {
      builder.field(kvp.getKey(), kvp.getValue());
    }
  }

  if (!Strings.isNullOrEmpty(this.name)) {
    builder.name(this.name);
  }

  if (!Strings.isNullOrEmpty(this.doc)) {
    builder.doc(this.doc);
  }

  if (null != this.defaultValue) {
    Object value;
    switch (this.type) {
      case INT8:
        value = ((Number) this.defaultValue).byteValue();
        break;
      case INT16:
        value = ((Number) this.defaultValue).shortValue();
        break;
      case INT32:
        value = ((Number) this.defaultValue).intValue();
        break;
      case INT64:
        value = ((Number) this.defaultValue).longValue();
        break;
      case FLOAT32:
        value = ((Number) this.defaultValue).floatValue();
        break;
      case FLOAT64:
        value = ((Number) this.defaultValue).doubleValue();
        break;
      default:
        value = this.defaultValue;
        break;
    }
    builder.defaultValue(value);
  }

  if (null != this.parameters) {
    builder.parameters(this.parameters);
  }

  if (this.isOptional) {
    builder.optional();
  }

  if (null != this.version) {
    builder.version(this.version);
  }

  return builder.build();
}
 
Example 8
Source File: DataUtilityTest.java    From kinesis-kafka-connector with Apache License 2.0 3 votes vote down vote up
@Test
public void parseArrayValueTest(){
	
	Schema valueSchema = SchemaBuilder.int8(); 
	Schema schema = SchemaBuilder.array(valueSchema);
	
	Object[] arrayOfInt8 = { (byte) 1, (byte) 2, (byte) 3, (byte) 4};
	ByteBuffer actual = DataUtility.parseValue(schema, arrayOfInt8);
	ByteBuffer expected = ByteBuffer.allocate(4).put((byte) 1).put( (byte) 2).put( (byte) 3).put( (byte) 4);
	
	Assert.assertTrue(actual.equals(expected));
	
}