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

The following examples show how to use org.apache.kafka.connect.data.SchemaBuilder#int32() . 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: LogFieldConverterFactory.java    From kafka-connect-spooldir with Apache License 2.0 6 votes vote down vote up
static Schema schema(Class<?> logClass, String logFieldName) {
  final SchemaBuilder builder;
  if (LocalDate.class.equals(logClass)) {
    builder = Date.builder();
  } else if (LocalTime.class.equals(logClass)) {
    builder = Time.builder();
  } else if (Integer.class.equals(logClass)) {
    builder = SchemaBuilder.int32();
  } else if (Long.class.equals(logClass)) {
    builder = SchemaBuilder.int64();
  } else if (String.class.equals(logClass)) {
    builder = SchemaBuilder.string();
  } else {
    throw new UnsupportedOperationException(
        String.format("%s is not a supported type.", logClass.getName())
    );
  }
  builder.optional();


  return builder.build();
}
 
Example 2
Source File: DataUtilityTest.java    From kinesis-kafka-connector with Apache License 2.0 5 votes vote down vote up
@Test
public void parseIntValueTest(){
	
	Schema schemaInt32 = SchemaBuilder.int32();
	ByteBuffer actualInt32 = DataUtility.parseValue(schemaInt32, (int) 2);
	ByteBuffer expectedInt32 = ByteBuffer.allocate(4).putInt( (int) 2);
	
	Assert.assertTrue(actualInt32.equals(expectedInt32));
}
 
Example 3
Source File: CloudPubSubSinkTaskTest.java    From pubsub with Apache License 2.0 5 votes vote down vote up
/** Tests that an exception is thrown when the schema of the value is not BYTES. */
@Test
public void testPutPrimitives() {
  task.start(props);
  SinkRecord record8 =
      new SinkRecord(null, -1, null, null, SchemaBuilder.int8(), (byte) 5, -1);
  SinkRecord record16 =
      new SinkRecord(null, -1, null, null, SchemaBuilder.int16(), (short) 5, -1);
  SinkRecord record32 =
      new SinkRecord(null, -1, null, null, SchemaBuilder.int32(), (int) 5, -1);
  SinkRecord record64 =
      new SinkRecord(null, -1, null, null, SchemaBuilder.int64(), (long) 5, -1);
  SinkRecord recordFloat32 =
      new SinkRecord(null, -1, null, null, SchemaBuilder.float32(), (float) 8, -1);
  SinkRecord recordFloat64 =
      new SinkRecord(null, -1, null, null, SchemaBuilder.float64(), (double) 8, -1);
  SinkRecord recordBool =
      new SinkRecord(null, -1, null, null, SchemaBuilder.bool(), true, -1);
  SinkRecord recordString =
      new SinkRecord(null, -1, null, null, SchemaBuilder.string(), "Test put.", -1);
  List<SinkRecord> list = new ArrayList<>();
  list.add(record8);
  list.add(record16);
  list.add(record32);
  list.add(record64);
  list.add(recordFloat32);
  list.add(recordFloat64);
  list.add(recordBool);
  list.add(recordString);
  task.put(list);
}
 
Example 4
Source File: MsSqlTableMetadataProvider.java    From kafka-connect-cdc-mssql with Apache License 2.0 4 votes vote down vote up
Schema generateSchema(ResultSet resultSet,
                      final ChangeKey changeKey,
                      final String columnName) throws SQLException {
  boolean optional = resultSet.getBoolean(2);
  String dataType = resultSet.getString(3);
  int scale = resultSet.getInt(4);
  SchemaBuilder builder;

  log.trace("{}: columnName='{}' dataType='{}' scale={} optional={}", changeKey, columnName, dataType, scale, optional);

  switch (dataType) {
    case "bigint":
      builder = SchemaBuilder.int64();
      break;
    case "bit":
      builder = SchemaBuilder.bool();
      break;
    case "char":
    case "varchar":
    case "text":
    case "nchar":
    case "nvarchar":
    case "ntext":
    case "uniqueidentifier":
      builder = SchemaBuilder.string();
      break;
    case "smallmoney":
    case "money":
    case "decimal":
    case "numeric":
      builder = Decimal.builder(scale);
      break;
    case "binary":
    case "image":
    case "varbinary":
      builder = SchemaBuilder.bytes();
      break;
    case "date":
      builder = Date.builder();
      break;
    case "datetime":
    case "datetime2":
    case "smalldatetime":
      builder = Timestamp.builder();
      break;
    case "time":
      builder = Time.builder();
      break;
    case "int":
      builder = SchemaBuilder.int32();
      break;
    case "smallint":
      builder = SchemaBuilder.int16();
      break;
    case "tinyint":
      builder = SchemaBuilder.int8();
      break;
    case "real":
      builder = SchemaBuilder.float32();
      break;
    case "float":
      builder = SchemaBuilder.float64();
      break;

    default:
      throw new DataException(
          String.format("Could not process (dataType = '%s', optional = %s, scale = %d) for %s.",
              dataType, optional, scale, changeKey
          )
      );
  }

  log.trace("{}: columnName='{}' schema.type='{}' schema.name='{}'", changeKey, columnName, builder.type(), builder.name());

  builder.parameters(
      ImmutableMap.of(Change.ColumnValue.COLUMN_NAME, columnName)
  );

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

  return builder.build();
}