Java Code Examples for org.apache.kafka.connect.data.Schema#parameters()

The following examples show how to use org.apache.kafka.connect.data.Schema#parameters() . 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: DecimalTypeParser.java    From connect-utils with Apache License 2.0 6 votes vote down vote up
private static int scaleInternal(Schema schema) {
  if (null == schema.parameters()) {
    throw new DataException(NOT_FOUND_MESSAGE);
  }

  String scaleString = schema.parameters().get(Decimal.SCALE_FIELD);
  if (scaleString == null) {
    throw new DataException(NOT_FOUND_MESSAGE);
  } else {
    try {
      return Integer.parseInt(scaleString);
    } catch (NumberFormatException var3) {
      throw new DataException(NOT_PARSABLE_MESSAGE, var3);
    }
  }
}
 
Example 2
Source File: SchemaSerializationModule.java    From connect-utils with Apache License 2.0 6 votes vote down vote up
Storage(Schema schema) {
  this.name = schema.name();
  this.doc = schema.doc();
  this.type = schema.type();
  this.defaultValue = schema.defaultValue();
  this.version = schema.version();
  this.parameters = schema.parameters();
  this.isOptional = schema.isOptional();

  if (Schema.Type.MAP == this.type) {
    this.keySchema = schema.keySchema();
    this.valueSchema = schema.valueSchema();
  } else if (Schema.Type.ARRAY == this.type) {
    this.keySchema = null;
    this.valueSchema = schema.valueSchema();
  } else if (Schema.Type.STRUCT == this.type) {
    this.fieldSchemas = new LinkedHashMap<>();
    for (Field field : schema.fields()) {
      this.fieldSchemas.put(field.name(), field.schema());
    }
  }
}
 
Example 3
Source File: AvroData.java    From apicurio-registry with Apache License 2.0 5 votes vote down vote up
public static Schema nonOptional(Schema schema) {
    return new ConnectSchema(schema.type(), false, schema.defaultValue(), schema.name(),
                             schema.version(), schema.doc(),
                             schema.parameters(),
                             fields(schema),
                             keySchema(schema),
                             valueSchema(schema));
}
 
Example 4
Source File: PatternRename.java    From kafka-connect-transform-common with Apache License 2.0 5 votes vote down vote up
@Override
protected SchemaAndValue processStruct(R record, Schema inputSchema, Struct inputStruct) {
  final SchemaBuilder outputSchemaBuilder = SchemaBuilder.struct();
  outputSchemaBuilder.name(inputSchema.name());
  outputSchemaBuilder.doc(inputSchema.doc());
  if (null != inputSchema.defaultValue()) {
    outputSchemaBuilder.defaultValue(inputSchema.defaultValue());
  }
  if (null != inputSchema.parameters() && !inputSchema.parameters().isEmpty()) {
    outputSchemaBuilder.parameters(inputSchema.parameters());
  }
  if (inputSchema.isOptional()) {
    outputSchemaBuilder.optional();
  }
  Map<String, String> fieldMappings = new HashMap<>(inputSchema.fields().size());
  for (final Field inputField : inputSchema.fields()) {
    log.trace("process() - Processing field '{}'", inputField.name());
    final Matcher fieldMatcher = this.config.pattern.matcher(inputField.name());
    final String outputFieldName;
    if (fieldMatcher.find()) {
      outputFieldName = fieldMatcher.replaceAll(this.config.replacement);
    } else {
      outputFieldName = inputField.name();
    }
    log.trace("process() - Mapping field '{}' to '{}'", inputField.name(), outputFieldName);
    fieldMappings.put(inputField.name(), outputFieldName);
    outputSchemaBuilder.field(outputFieldName, inputField.schema());
  }
  final Schema outputSchema = outputSchemaBuilder.build();
  final Struct outputStruct = new Struct(outputSchema);
  for (Map.Entry<String, String> entry : fieldMappings.entrySet()) {
    final String inputField = entry.getKey(), outputField = entry.getValue();
    log.trace("process() - Copying '{}' to '{}'", inputField, outputField);
    final Object value = inputStruct.get(inputField);
    outputStruct.put(outputField, value);
  }
  return new SchemaAndValue(outputSchema, outputStruct);
}
 
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: SchemaBuildersTest.java    From connect-utils with Apache License 2.0 5 votes vote down vote up
String builderOfTestCaseName(Schema schema) {
  StringBuilder builder = new StringBuilder();
  builder.append(schema.type());
  if (!Strings.isNullOrEmpty(schema.name())) {
    builder.append("(");
    builder.append(schema.name());
    builder.append(")");
  }
  switch (schema.type()) {
    case ARRAY:
      builder.append("[");
      builder.append(builderOfTestCaseName(schema.valueSchema()));
      builder.append("]");
      break;
    case MAP:
      builder.append("[");
      builder.append(builderOfTestCaseName(schema.valueSchema()));
      builder.append(",");
      builder.append(builderOfTestCaseName(schema.valueSchema()));
      builder.append("]");
      break;
  }

  if (schema.isOptional()) {
    builder.append(":optional");
  }
  if (null != schema.version()) {
    builder.append(":version(");
    builder.append(schema.version());
    builder.append(")");
  }
  if (null != schema.parameters() && !schema.parameters().isEmpty()) {
    builder.append(":parameters(");
    builder.append(schema.parameters());
    builder.append(")");
  }


  return builder.toString();
}
 
Example 7
Source File: ConversionHandler.java    From kafka-connect-transform-common with Apache License 2.0 4 votes vote down vote up
public DecimalConversionHandler(Schema headerSchema, String header, String field) {
  super(headerSchema, header, field);
  String scaleText = null != headerSchema.parameters() ? headerSchema.parameters().get(Decimal.SCALE_FIELD) : null;
  Preconditions.checkNotNull(scaleText, "schema parameters must contain a '%s' parameter.", Decimal.SCALE_FIELD);
  scale = Integer.parseInt(scaleText);
}