Java Code Examples for org.apache.kafka.connect.data.Schema#valueSchema()
The following examples show how to use
org.apache.kafka.connect.data.Schema#valueSchema() .
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: AvroJsonSchemafulRecordConverter.java From mongo-kafka with Apache License 2.0 | 6 votes |
private BsonValue toBsonArray(final Schema schema, final Object value) { if (value == null) { return BsonNull.VALUE; } Schema fieldSchema = schema.valueSchema(); BsonArray bsonArray = new BsonArray(); List<?> myList = (List) value; myList.forEach( v -> { if (fieldSchema.type().isPrimitive()) { if (v == null) { bsonArray.add(BsonNull.VALUE); } else { bsonArray.add(getConverter(fieldSchema).toBson(v)); } } else if (fieldSchema.type().equals(ARRAY)) { bsonArray.add(toBsonArray(fieldSchema, v)); } else { bsonArray.add(toBsonDoc(fieldSchema, v)); } }); return bsonArray; }
Example 2
Source File: SchemaSerializationModule.java From connect-utils with Apache License 2.0 | 6 votes |
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 |
public static Schema valueSchema(Schema schema) { Schema.Type type = schema.type(); if (Schema.Type.MAP.equals(type) || Schema.Type.ARRAY.equals(type)) { return schema.valueSchema(); } else { return null; } }
Example 4
Source File: AvroJsonSchemafulRecordConverter.java From mongo-kafka with Apache License 2.0 | 5 votes |
private BsonValue toBsonDoc(final Schema schema, final Object value) { if (value == null) { return BsonNull.VALUE; } BsonDocument doc = new BsonDocument(); if (schema.type() == MAP) { Schema fieldSchema = schema.valueSchema(); Map m = (Map) value; for (Object entry : m.keySet()) { String key = (String) entry; if (fieldSchema.type().isPrimitive()) { doc.put(key, getConverter(fieldSchema).toBson(m.get(key), fieldSchema)); } else if (fieldSchema.type().equals(ARRAY)) { doc.put(key, toBsonArray(fieldSchema, m.get(key))); } else { if (m.get(key) == null) { doc.put(key, BsonNull.VALUE); } else { doc.put(key, toBsonDoc(fieldSchema, m.get(key))); } } } } else { schema.fields().forEach(f -> doc.put(f.name(), processField((Struct) value, f))); } return doc; }
Example 5
Source File: DataUtility.java From kinesis-kafka-connector with Apache License 2.0 | 4 votes |
/** * Parses Kafka Values * * @param schema * - Schema of passed message as per * https://kafka.apache.org/0100/javadoc/org/apache/kafka/connect/data/Schema.html * @param value * - Value of the message * @return Parsed bytebuffer as per schema type */ public static ByteBuffer parseValue(Schema schema, Object value) { Schema.Type t = schema.type(); switch (t) { case INT8: ByteBuffer byteBuffer = ByteBuffer.allocate(1); byteBuffer.put((Byte) value); return byteBuffer; case INT16: ByteBuffer shortBuf = ByteBuffer.allocate(2); shortBuf.putShort((Short) value); return shortBuf; case INT32: ByteBuffer intBuf = ByteBuffer.allocate(4); intBuf.putInt((Integer) value); return intBuf; case INT64: ByteBuffer longBuf = ByteBuffer.allocate(8); longBuf.putLong((Long) value); return longBuf; case FLOAT32: ByteBuffer floatBuf = ByteBuffer.allocate(4); floatBuf.putFloat((Float) value); return floatBuf; case FLOAT64: ByteBuffer doubleBuf = ByteBuffer.allocate(8); doubleBuf.putDouble((Double) value); return doubleBuf; case BOOLEAN: ByteBuffer boolBuffer = ByteBuffer.allocate(1); boolBuffer.put((byte) ((Boolean) value ? 1 : 0)); return boolBuffer; case STRING: try { return ByteBuffer.wrap(((String) value).getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block System.out.println("Message cannot be translated:" + e.getLocalizedMessage()); } case ARRAY: Schema sch = schema.valueSchema(); if (sch.type() == Type.MAP || sch.type() == Type.STRUCT) { throw new DataException("Invalid schema type."); } Object[] objs = (Object[]) value; ByteBuffer[] byteBuffers = new ByteBuffer[objs.length]; int noOfByteBuffer = 0; for (Object obj : objs) { byteBuffers[noOfByteBuffer++] = parseValue(sch, obj); } ByteBuffer result = ByteBuffer.allocate(Arrays.stream(byteBuffers).mapToInt(Buffer::remaining).sum()); Arrays.stream(byteBuffers).forEach(bb -> result.put(bb.duplicate())); return result; case BYTES: if (value instanceof byte[]) return ByteBuffer.wrap((byte[]) value); else if (value instanceof ByteBuffer) return (ByteBuffer) value; case MAP: // TO BE IMPLEMENTED return ByteBuffer.wrap(null); case STRUCT: List<ByteBuffer> fieldList = new LinkedList<ByteBuffer>(); // Parsing each field of structure schema.fields().forEach(field -> fieldList.add(parseValue(field.schema(), ((Struct) value).get(field)))); // Initialize ByteBuffer ByteBuffer processedValue = ByteBuffer.allocate(fieldList.stream().mapToInt(Buffer::remaining).sum()); // Combine bytebuffer of all fields fieldList.forEach(buffer -> processedValue.put(buffer.duplicate())); return processedValue; } return null; }