
com.arpnetworking.metrics.common.kafka.ConsumerDeserializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of metrics-aggregator-daemon Show documentation
Show all versions of metrics-aggregator-daemon Show documentation
Aggregates samples published by metrics client libraries and writes to destinations.
/*
* Copyright 2019 Dropbox.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arpnetworking.metrics.common.kafka;
import com.arpnetworking.commons.jackson.databind.ObjectMapperFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.deser.std.JsonNodeDeserializer;
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.kafka.clients.consumer.Consumer;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.KafkaException;
import org.apache.kafka.common.config.ConfigException;
import java.io.IOException;
import java.util.List;
import java.util.Map;
/**
* Jackson {@code JsonDeserializer} implementation for {@code Consumer}.
*
* @param the type of key field in {@code Consumer}
* @param the type of value field in {@code Consumer}
*
* @author Joey Jackson (jjackson at dropbox dot com)
*/
public class ConsumerDeserializer extends JsonDeserializer> {
@Override
public Consumer deserialize(final JsonParser parser, final DeserializationContext context)
throws IOException, JsonProcessingException {
// Parse input json into JsonNode Tree
final JsonDeserializer extends JsonNode> deserializer = JsonNodeDeserializer.getDeserializer(ObjectNode.class);
final JsonNode node = deserializer.deserialize(parser, context);
// Pull out configs and topics fields and convert deserialize with standard mapper
final JsonNode configNode = node.get("configs");
final JsonNode topicsNode = node.get("topics");
if (configNode == null) {
throw MismatchedInputException.from(parser, Consumer.class,
"Consumer object missing \"configs\" field");
}
if (topicsNode == null) {
throw MismatchedInputException.from(parser, Consumer.class,
"Consumer object missing \"topics\" field");
}
final ObjectMapper mapper = ObjectMapperFactory.getInstance();
final TypeReference
© 2015 - 2025 Weber Informatics LLC | Privacy Policy