|
@@ -0,0 +1,75 @@
|
|
|
+package org.jeecg.modules.kafka.consumer;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.alibaba.fastjson.TypeReference;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.kafka.clients.consumer.ConsumerRecord;
|
|
|
+import org.jeecg.modules.iotedgeCollectData.entity.IotedgeCollectData;
|
|
|
+import org.jeecg.modules.iotedgeCollectData.service.IIotedgeCollectDataService;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.kafka.annotation.KafkaListener;
|
|
|
+import org.springframework.kafka.support.Acknowledgment;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+@Component
|
|
|
+@Slf4j
|
|
|
+public class TestConsumer {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ @SuppressWarnings("all")
|
|
|
+ private IIotedgeCollectDataService dataService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 指定一个消费者组,一个主题主题。
|
|
|
+ * topicPattern有个问题,如果在本程序运行过程中新增了topic,则监听不到,需要重新启动本程序才可以
|
|
|
+ */
|
|
|
+// @KafkaListener(topicPattern = "thing___.*.___property")
|
|
|
+ @KafkaListener(topicPattern = "#{'${spring.kafka.topic-patterns}'}")
|
|
|
+ public void simpleConsumer(ConsumerRecord<?, ?> record, Acknowledgment ack) {
|
|
|
+ // 提交(用来标记一条消息已经消费完成,即将从消息队列里移除。)
|
|
|
+ ack.acknowledge();
|
|
|
+ saveDataR(record);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保存数据
|
|
|
+ * @param record kafka记录
|
|
|
+ */
|
|
|
+ private void saveDataR(ConsumerRecord<?, ?> record) {
|
|
|
+ System.out.println(record);
|
|
|
+ System.out.println(record.value());
|
|
|
+ ArrayList<IotedgeCollectData> list = new ArrayList<>();
|
|
|
+ String value = (String) record.value();
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(value);
|
|
|
+ Map<String,Object> services = (Map<String, Object>) jsonObject.get("services");
|
|
|
+ for (String key:services.keySet()) {
|
|
|
+ Map<String,Object> tag = (Map<String,Object>) services.get(key);
|
|
|
+ Map<String,Object> tagmap = (Map<String, Object>) tag.get("properties");
|
|
|
+ for (String item:tagmap.keySet()) {
|
|
|
+ Map<String,Object> valuemap = (Map<String, Object>) tagmap.get(item);
|
|
|
+ String value1;
|
|
|
+ if (valuemap.get("value") instanceof Integer){
|
|
|
+ value1 = String.valueOf(valuemap.get("value"));
|
|
|
+ }else {
|
|
|
+ value1 = (String) valuemap.get("value");
|
|
|
+ }
|
|
|
+
|
|
|
+ IotedgeCollectData iotedgeCollectData = new IotedgeCollectData();
|
|
|
+ iotedgeCollectData.setGroupid(jsonObject.get("g_id").toString());
|
|
|
+ iotedgeCollectData.setPid(jsonObject.get("p_id").toString());
|
|
|
+ iotedgeCollectData.setDevice(jsonObject.get("d_id").toString());
|
|
|
+ iotedgeCollectData.setTime(jsonObject.get("ts").toString());
|
|
|
+ iotedgeCollectData.setService(key);
|
|
|
+ iotedgeCollectData.setProperty(item);
|
|
|
+ iotedgeCollectData.setValue(value1);
|
|
|
+
|
|
|
+ list.add(iotedgeCollectData);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ dataService.saveBatch(list);
|
|
|
+ }
|
|
|
+}
|