|
13 | 13 | import com.fasterxml.jackson.core.JsonProcessingException; |
14 | 14 | import com.fasterxml.jackson.databind.ObjectMapper; |
15 | 15 | import com.fasterxml.jackson.databind.ObjectWriter; |
16 | | -import java.io.ByteArrayOutputStream; |
| 16 | +import com.linkedin.avroutil1.compatibility.AvroCodecUtil; |
| 17 | +import com.linkedin.avroutil1.compatibility.AvroCompatibilityHelper; |
| 18 | +import com.linkedin.avroutil1.compatibility.AvroVersion; |
17 | 19 | import java.io.IOException; |
18 | 20 | import java.lang.management.ManagementFactory; |
19 | 21 | import java.time.Duration; |
|
34 | 36 | import javax.management.ObjectName; |
35 | 37 | import kafka.admin.BrokerMetadata; |
36 | 38 | import org.apache.avro.generic.GenericData; |
37 | | -import org.apache.avro.generic.GenericDatumWriter; |
| 39 | +import org.apache.avro.generic.GenericDatumReader; |
38 | 40 | import org.apache.avro.generic.GenericRecord; |
39 | | -import org.apache.avro.io.Encoder; |
40 | | -import org.apache.avro.io.JsonEncoder; |
| 41 | +import org.apache.avro.io.Decoder; |
41 | 42 | import org.apache.kafka.clients.admin.AdminClient; |
42 | 43 | import org.apache.kafka.clients.admin.CreateTopicsResult; |
43 | 44 | import org.apache.kafka.clients.admin.ListPartitionReassignmentsResult; |
44 | 45 | import org.apache.kafka.clients.admin.NewTopic; |
45 | 46 | import org.apache.kafka.clients.admin.PartitionReassignment; |
46 | 47 | import org.apache.kafka.common.TopicPartition; |
47 | 48 | import org.apache.kafka.common.errors.TopicExistsException; |
48 | | -import org.json.JSONObject; |
49 | 49 | import org.slf4j.Logger; |
50 | 50 | import org.slf4j.LoggerFactory; |
51 | 51 |
|
@@ -224,29 +224,21 @@ public static String jsonFromFields(String topic, long idx, long timestamp, Stri |
224 | 224 | * @return GenericRecord that is de-serialized from kafka message w.r.t. expected schema. |
225 | 225 | */ |
226 | 226 | public static GenericRecord genericRecordFromJson(String message) { |
227 | | - GenericRecord record = new GenericData.Record(DefaultTopicSchema.MESSAGE_V0); |
228 | | - JSONObject jsonObject = new JSONObject(message); |
229 | | - record.put(DefaultTopicSchema.TOPIC_FIELD.name(), jsonObject.getString(DefaultTopicSchema.TOPIC_FIELD.name())); |
230 | | - record.put(DefaultTopicSchema.INDEX_FIELD.name(), jsonObject.getLong(DefaultTopicSchema.INDEX_FIELD.name())); |
231 | | - record.put(DefaultTopicSchema.TIME_FIELD.name(), jsonObject.getLong(DefaultTopicSchema.TIME_FIELD.name())); |
232 | | - record.put(DefaultTopicSchema.PRODUCER_ID_FIELD.name(), |
233 | | - jsonObject.getString(DefaultTopicSchema.PRODUCER_ID_FIELD.name())); |
234 | | - record.put(DefaultTopicSchema.CONTENT_FIELD.name(), jsonObject.getString(DefaultTopicSchema.CONTENT_FIELD.name())); |
235 | | - return record; |
| 227 | + try { |
| 228 | + Decoder jsonDecoder = AvroCompatibilityHelper.newCompatibleJsonDecoder(DefaultTopicSchema.MESSAGE_V0, message); |
| 229 | + GenericDatumReader<GenericRecord> reader = new GenericDatumReader<>(DefaultTopicSchema.MESSAGE_V0, DefaultTopicSchema.MESSAGE_V0); |
| 230 | + return reader.read(null, jsonDecoder); |
| 231 | + } catch (Exception e) { |
| 232 | + throw new IllegalStateException("unable to deserialize " + message, e); |
| 233 | + } |
236 | 234 | } |
237 | 235 |
|
238 | 236 | public static String jsonFromGenericRecord(GenericRecord record) { |
239 | | - ByteArrayOutputStream out = new ByteArrayOutputStream(); |
240 | | - GenericDatumWriter<GenericRecord> writer = new GenericDatumWriter<>(DefaultTopicSchema.MESSAGE_V0); |
241 | | - |
242 | 237 | try { |
243 | | - Encoder encoder = new JsonEncoder(DefaultTopicSchema.MESSAGE_V0, out); |
244 | | - writer.write(record, encoder); |
245 | | - encoder.flush(); |
| 238 | + return AvroCodecUtil.serializeJson(record, AvroVersion.AVRO_1_4); |
246 | 239 | } catch (IOException e) { |
247 | | - LOG.error("Unable to serialize avro record due to error " + e); |
| 240 | + throw new IllegalStateException("Unable to serialize avro record due to error: " + record, e); |
248 | 241 | } |
249 | | - return out.toString(); |
250 | 242 | } |
251 | 243 |
|
252 | 244 | public static List<MbeanAttributeValue> getMBeanAttributeValues(String mbeanExpr, String attributeExpr) { |
|
0 commit comments