-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathKafkaAvroProducerCommon.java
72 lines (65 loc) · 2.58 KB
/
KafkaAvroProducerCommon.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
* Licensed Materials - Property of IBM
* 5737-I23
* Copyright IBM Corp. 2021. All Rights Reserved.
* U.S. Government Users Restricted Rights:
* Use, duplication or disclosure restricted by GSA ADP Schedule
* Contract with IBM Corp.
*/
package com.ibm.dba.bai.avro.samples;
import org.apache.avro.Schema;
import org.apache.avro.generic.GenericDatumReader;
import org.apache.avro.generic.GenericRecord;
import org.apache.avro.io.Decoder;
import org.apache.avro.io.DecoderFactory;
import org.apache.avro.util.Utf8;
import org.apache.kafka.common.errors.SerializationException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
* Both Confluent and EventStream Avro implementation commonly use org.apache.avro API.
* This class is where the common code is stored.
* */
public class KafkaAvroProducerCommon {
/**
* Serializes a String json event into an avro human readable object along a schema for proper registration
* into a kafka topic.
* @param jsonEvent the JSON representation of the event to convert.
* @param schema the Avro schema to which the event must conform.
* @return an Avro representation of the event.
*/
public static Object jsonToAvro(String jsonEvent, Schema schema) {
try {
GenericDatumReader<GenericRecord> reader = new GenericDatumReader<>(schema);
DecoderFactory decoderFactory = new DecoderFactory();
Decoder decoder = decoderFactory.jsonDecoder(schema, jsonEvent);
Object object = reader.read(null, decoder);
if (schema.getType().equals(Schema.Type.STRING)) {
object = ((Utf8) object).toString();
}
return object;
} catch (Exception ex) {
throw new SerializationException(String.format("Error serializing json %s to Avro of schema %s", jsonEvent, schema), ex);
}
}
/**
* Read a file content from the specified path source.
* @param contentSource is considered as a file path.
* @return the content as a string.
* @throws IOException if any I/O error occurs.
*/
public static String readPathContent(String contentSource) throws IOException {
if (contentSource == null) {
throw new IllegalArgumentException("the event to send was not specified");
}
Path path = Paths.get(contentSource);
if (!Files.exists(path)) {
throw new IllegalArgumentException("the content source \"" + contentSource + "\" points to a non existing file");
}
StringBuilder event = new StringBuilder();
Files.lines(path).forEach(line -> event.append(line).append('\n'));
return event.toString();
}
}