我有一个简单的问题,我知道你可以帮我解决。我正在使用一个巨大的字符串有效负载,我想为我的卡夫卡消费者修剪。我编写了一个类来处理数据并删除某些数据属性。其中一些是 IntNode、TextNode 和 ArrayNode 类型。我目前正在尝试将所有内容都转换为 ObjectNode 并使用 remove(fieldName) 方法,但是我在堆栈跟踪中遇到异常“java.lang.ClassCastException: class com.fasterxml.jackson.databind.node.IntNode cannot be cast to com.fasterxml.jackson.databind.node.ObjectNode 类(com.fasterxml.jackson.databind.node.IntNode 和 com.fasterxml.jackson.databind.node.ObjectNode 位于加载程序‘app’的未命名模块中)”
找到附加的我的代码和示例有效负载。感谢您的时间(我正在尝试删除属性:IntNode 类型的 location_group_id、关系、里程碑、geographic_specifications、联系信息)
package com.target.locationsync.consumer;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.target.locationsync.models.LocationSync;
import com.target.locationsync.services.ElasticSearchService;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import java.util.*;
@Component
public class KafkaConsumerClean{
public static final Logger logger = LoggerFactory.getLogger(KafkaConsumer.class);
public static final String JSON_ELEMENT = "locations";
private final ObjectMapper objectMapper = new ObjectMapper();
@Autowired
ElasticSearchService elasticSearchService;
@Value("${processKafkaMessage}")
private Boolean processKafkaMessage;
@KafkaListener(
topics = "${kafka.topic}",
errorHandler = "kafkaErrorHandler",
containerFactory = "kafkaListenerContainerFactory",
groupId = "${kafka.consumer.group.id}")
public void receiveLocationData(ConsumerRecord<?, ?> consumerRecord, Acknowledgment acknowledgment) throws JsonProcessingException {
if(processKafkaMessage)
{
//String payload = consumerRecord.value().toString();
String payload = "{\"location_id\":9117,\"type_code\":\"NY\",\"type_description\":\"Distribution Center\",\"sub_type_code\":\"TFC\",\"status\":\"Open\",\"location_group_id\":7505,\"location_names\":[{\"name_type\":\"Proj Name1\",\"name\":\"Target.com T3857 M1FC\"}],\"address\":[{\"address_context_code\":\"M\",\"address_context_description\":\"Mailing\",\"intersection_description\":null,\"quadrant_code\":null,\"quadrant_description\":null,\"lot_and_block_text\":null,\"address_line1\":\"300 Creekview Ave, Bldg H\",\"address_line2\":null,\"address_line3\":null,\"city\":\"Logan Township\",\"region\":\"NJ\",\"state\":\"New Jersey\",\"postal_code\":\"08014\",\"county\":\"Gloucester\",\"country_code\":\"US\",\"country\":\"United States of America\"},{\"address_context_code\":\"S\",\"address_context_description\":\"Shipping\",\"intersection_description\":null,\"quadrant_code\":null,\"quadrant_description\":null,\"lot_and_block_text\":null,\"address_line1\":\"350 Creekview Ave\",\"address_line2\":\"Bldg H\",\"address_line3\":null,\"city\":\"Logan Township\",\"region\":\"NJ\",\"state\":\"New Jersey\",\"postal_code\":\"08014\",\"county\":\"Gloucester\",\"country_code\":\"US\",\"country\":\"United States of America\"}],\"capabilities\":[null],\"physical_specifications\":{\"total_building_area\":null,\"merchandise_level\":null,\"building_level\":null,\"prototype_code\":null,\"format\":null},\"contact_information\":[],\"geographic_specifications\":{\"latitude\":39.78992,\"longitude\":-75.34374,\"time_zone_code\":\"EST\",\"time_zone_description\":\"Eastern Std Time\"},\"milestones\":[{\"milestone_code\":\"Const Turnover\",\"milestone_date\":\"2021-04-08\"},{\"milestone_code\":\"Open Date\",\"milestone_date\":\"2021-06-22\"},{\"milestone_code\":\"Grand Open Date\",\"milestone_date\":\"2021-06-22\"}],\"remodel\":{\"last_remodel_type\":null,\"last_remodel_date\":null},\"miscellaneous\":{\"location_comment\":null,\"research_and_planning_market_name\":\"New York-Northern New Jersey-Long Island\",\"market_id\":35620,\"market_type\":\"Metro\",\"market_state\":\"NY\",\"is_rebuild\":false,\"site_type_code\":null,\"site_type_description\":null,\"sales_volume_code\":null,\"sales_volume_description\":null},\"relationship\":{\"offsite_warehouse\":null,\"sister_store\":null},\"region_group_district\":[null]}";
if(StringUtils.isEmpty(payload)){
acknowledgment.acknowledge();
}else{
try{
logger.info("About to process the payload" + payload);
String formattedPayload = processLocationData(payload);
logger.info("Processed payload: " + formattedPayload);
logger.info("Kafka Consumer received data at " + new Date() + " with this payload: " + formattedPayload);
acknowledgment.acknowledge();
}catch(Exception ex){
String message = "Error occurred while running kafka listener: " + ex.getMessage();
logger.error(new Date() + message);
acknowledgment.acknowledge();
}
}
}else{
acknowledgment.acknowledge();
}
}
private String processLocationData(String payload) throws JsonProcessingException {
while(payload != null && !payload.isEmpty()){
JsonNode locationKafkaMessage = objectMapper.readTree(payload);
Iterator<JsonNode> jsonNodeIterator = locationKafkaMessage.iterator();
while(jsonNodeIterator.hasNext()){
JsonNode locationMessage = jsonNodeIterator.next();//jsonNodeIterator.next is of location_id of type IntNode
ObjectNode kafkaPayload = (ObjectNode) locationMessage;//Problem here
kafkaPayload.remove("relationship");
kafkaPayload.remove("milestones");
kafkaPayload.remove("location_group_id");
kafkaPayload.remove("geographic_specifications");
kafkaPayload.remove("contact_information");
ObjectNode messageJson = objectMapper.createObjectNode();
try {
messageJson.set("locations", objectMapper.valueToTree(kafkaPayload));
logger.info("kafka msg" + objectMapper.writeValueAsString(messageJson));
logger.info("About to view message Json after trimming");
return messageJson.toString();
} catch (Exception ex) {
logger.error("Failed to parse item data for kafka message generation", ex);
}
LocationSync locationSync = objectMapper.readValue(messageJson.toString(), LocationSync.class);
elasticSearchService.updateIndexForLocationSync(locationSync);
}
}
return "No payload to format";
}
//"{\"location_id\":503,\"type_code\":\"OFFICE\",\"type_description\":\"Office Space\",
// \"sub_type_code\":\"BANK\",
// \"status\":\"Open\", \"location_group_id\":484,
// \"location_names\":[{\"name_type\":\"Proj Name1\",\"name\":\"Target Sioux Falls\"},
// {\"name_type\":\"WorkdayNm\",\"name\":\"HQ Target National Bank\"}],
// \"address\":[{\"address_context_code\":\"M\",
// \"address_context_description\":\"Mailing\",\"intersection_description\":null,\"quadrant_code\":null,
// \"quadrant_description\":null,\"lot_and_block_text\":null,\"address_line1\":\"3901 W 53rd St\",
// \"address_line2\":null,\"address_line3\":null,\"city\":\"Sioux Falls\",\"region\":\"SD\",
// \"state\":\"South Dakota\",\"postal_code\":\"57106-4221\",\"county\":\"Minnehaha\",\"country_code\":\"US\",
// \"country\":\"United States of America\"}],
// \"capabilities\":[null],
// \"physical_specifications\":{\"total_building_area\":null,\"merchandise_level\":null,\"building_level\":null,\"prototype_code\":null,\"format\":null},
// \"contact_information\":[{\"building_area\":\"MAIN\",\"capability\":null,\"telephone_type\":\"VOICE\",\"is_international_phone_number\":false,\"telephone_number\":\"605-323-2000\",\"extension\":null}],
// \"geographic_specifications\":{\"latitude\":43.503773,\"longitude\":-96.771912,\"time_zone_code\":\"CST\",\"time_zone_description\":\"Central Std Time\"},
// \"milestones\":[{\"milestone_code\":\"Open Date\",\"milestone_date\":\"1994-11-01\"}],
// \"remodel\":{\"last_remodel_type\":null,\"last_remodel_date\":null},
// \"miscellaneous\":{\"location_comment\":\"Limited Purpose Credit Card Bank that issues Target Visa Credit Card\",\"research_and_planning_market_name\":\"Sioux Falls\",\"market_id\":43620,\"market_type\":\"Metro\",\"market_state\":\"SD\",\"is_rebuild\":false,\"site_type_code\":null,\"site_type_description\":null,\"sales_volume_code\":null,\"sales_volume_description\":null},
// \"relationship\":{\"offsite_warehouse\":null,\"sister_store\":null},\"region_group_district\":[null]}";
}