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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
| com.cartravel.kafka.package com.cartravel.kafka;
import com.cartravel.common.Constants; import com.cartravel.common.Order; import com.cartravel.common.TopicName; import com.cartravel.util.HBaseUtil; import com.cartravel.util.JedisUtil; import com.cartravel.util.ObjUtil; import org.apache.commons.lang.StringUtils; import org.apache.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.client.Table; import org.apache.kafka.clients.consumer.Consumer; import org.apache.kafka.clients.consumer.ConsumerRecord; import org.apache.kafka.clients.consumer.ConsumerRecords; import org.apache.kafka.clients.consumer.KafkaConsumer; import org.apache.log4j.Level; import org.apache.log4j.Logger; import redis.clients.jedis.Jedis;
import java.io.BufferedOutputStream; import java.io.FileOutputStream; import java.io.PrintWriter; import java.text.SimpleDateFormat; import java.util.*;
public class GpsConsumer implements Runnable { private static Logger log = Logger.getLogger(GpsConsumer.class); private final KafkaConsumer<String, String> consumer; private final String topic; private static int count = 0; private FileOutputStream file = null; private BufferedOutputStream out = null; private PrintWriter printWriter = null; private String lineSeparator = null; private int batchNum = 0; JedisUtil instance = null; Jedis jedis = null;
private String cityCode = ""; private Map<String, String> gpsMap = new HashMap<String, String>(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public GpsConsumer(String topic, String groupId) { if (topic.equalsIgnoreCase(TopicName.CHENG_DU_GPS_TOPIC.getTopicName())) { cityCode = Constants.CITY_CODE_CHENG_DU; } else if (topic.equalsIgnoreCase(TopicName.XI_AN_GPS_TOPIC.getTopicName())) { cityCode = Constants.CITY_CODE_XI_AN; } else if (topic.equalsIgnoreCase(TopicName.HAI_KOU_ORDER_TOPIC.getTopicName())) { cityCode = Constants.CITY_CODE_HAI_KOU; }else{ throw new IllegalArgumentException(topic+",主题名称不合法!"); }
Properties props = new Properties();
props.put("bootstrap.servers", Constants.KAFKA_BOOTSTRAP_SERVERS);
props.put("group.id", groupId); props.put("enable.auto.commit", "true"); props.put("auto.offset.reset", "earliest");
props.put("session.timeout.ms", "30000"); props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
consumer = new KafkaConsumer<String,String>(props); this.topic = topic; }
@Override public void run() { while (true) { try { doWork(); } catch (Exception e) { e.printStackTrace(); } } }
public void doWork() throws Exception { batchNum++; consumer.subscribe(Collections.singletonList(this.topic)); ConsumerRecords<String, String> records = consumer.poll(1000); System.out.println("第" + batchNum + "批次," + records.count()); String driverId = ""; String orderId = ""; String lng = ""; String lat = ""; String timestamp = ""; Order order = null; Order startEndTimeOrder = null; Object tmpOrderObj = null; if (records.count() > 0) { Table table = HBaseUtil.getTable(Constants.HTAB_GPS); JedisUtil instance = JedisUtil.getInstance(); jedis = instance.getJedis(); List<Put> puts = new ArrayList<>(); String rowkey = "";
if (gpsMap.size() > 0) { gpsMap.clear(); }
if (!HBaseUtil.tableExists(Constants.HTAB_GPS)) { HBaseUtil.createTable(HBaseUtil.getConnection(), Constants.HTAB_GPS, Constants.DEFAULT_FAMILY); }
for (ConsumerRecord<String, String> record : records) { count++; log.warn("Received message: (" + record.key() + ", " + record.value() + ") at offset " + record.offset() + ",count:" + count); String value = record.value(); if (value.contains(",")) { order = new Order(); String[] split = value.split(","); driverId = split[0]; orderId = split[1]; timestamp = split[2]; lng = split[3]; lat = split[4];
rowkey = orderId + "_" + timestamp; gpsMap.put("CITYCODE", cityCode); gpsMap.put("DRIVERID", driverId); gpsMap.put("ORDERID", orderId); gpsMap.put("TIMESTAMP", timestamp + ""); gpsMap.put("TIME", sdf.format(new Date(Long.parseLong(timestamp+"000")))); gpsMap.put("LNG", lng); gpsMap.put("LAT", lat);
order.setOrderId(orderId);
puts.add(HBaseUtil.createPut(rowkey, Constants.DEFAULT_FAMILY.getBytes(), gpsMap));
jedis.sadd(Constants.REALTIME_ORDERS, cityCode + "_" + orderId); jedis.lpush(cityCode + "_" + orderId, lng + "," + lat);
byte[] orderBytes = jedis.hget(Constants.ORDER_START_ENT_TIME.getBytes() , orderId.getBytes());
if (orderBytes != null) { tmpOrderObj = ObjUtil.deserialize(orderBytes); }
if (null != tmpOrderObj) { startEndTimeOrder = (Order) tmpOrderObj; startEndTimeOrder.setEndTime(Long.parseLong(timestamp+"000")); jedis.hset(Constants.ORDER_START_ENT_TIME.getBytes(), orderId.getBytes(), ObjUtil.serialize(startEndTimeOrder)); } else { order.setStartTime(Long.parseLong(timestamp)); order.setEndTime(Long.parseLong(timestamp)); jedis.hset(Constants.ORDER_START_ENT_TIME.getBytes(), orderId.getBytes(), ObjUtil.serialize(order)); } hourOrderInfoGather(jedis,gpsMap); } else if (value.contains("end")) { jedis.lpush(cityCode + "_" + orderId, value); } } table.put(puts); instance.returnJedis(jedis); } log.warn("正常结束..."); }
public void hourOrderInfoGather(Jedis jedis,Map<String, String> gpsMap) throws Exception{ String time = gpsMap.get("TIME"); String orderId = gpsMap.get("ORDERID"); String day = time.substring(0,time.indexOf(" ")); String hour = time.split(" ")[1].substring(0,2); String hourOrderCountTab = cityCode+"_"+day+"_hour_order_count";
String hourOrderField = cityCode+"_"+day+"_"+hour; String hourOrder = cityCode+"_order";
int hourOrderCount = 0; if(!jedis.sismember(hourOrder,orderId)){ jedis.sadd(hourOrder,orderId); String hourOrdernum = jedis.hget(hourOrderCountTab, hourOrderField); if(StringUtils.isEmpty(hourOrdernum)){ hourOrderCount = 1; }else{ hourOrderCount = Integer.parseInt(hourOrdernum)+1; }
jedis.hset(hourOrderCountTab,hourOrderField,hourOrderCount+""); } }
public static void main(String[] args) {
Logger.getLogger("org.apache.kafka").setLevel(Level.INFO); String topic = "cheng_du_gps_topic"; String groupId = "cheng_du_gps_consumer_01";
GpsConsumer gpsConsumer = new GpsConsumer(topic, groupId); Thread start = new Thread(gpsConsumer); start.start(); } }
|