@Override public void processEvent(String sid, Tuple tuple) { try { String bid = tuple.getStringByField("bid"); String qq = tuple.getStringByField("qq"); String uid = tuple.getStringByField("uid"); String itemId = tuple.getStringByField("item_id"); String actionType = tuple.getStringByField("action_type"); String actionTime = tuple.getStringByField("action_time"); String lbsInfo = tuple.getStringByField("lbs_info"); String platform = tuple.getStringByField("platform"); Long bigType = tuple.getLongByField("big_type"); Long midType = tuple.getLongByField("mid_type"); Long smallType = tuple.getLongByField("small_type"); Long itemTime = tuple.getLongByField("item_time"); String shopId = tuple.getStringByField("shop_id"); if (!Utils.isItemIdValid(itemId) || Utils.isRecommendAction(actionType)) { return; } if (qq.equals("389687043") || qq.equals("475182144")) { logger.info("--input to combiner---" + tuple.toString()); } Recommend.UserActiveHistory.ActiveRecord.Builder actBuilder = Recommend.UserActiveHistory.ActiveRecord.newBuilder(); actBuilder .setItem(itemId) .setActTime(Long.valueOf(actionTime)) .setActType(Integer.valueOf(actionType)) .setBigType(bigType) .setMiddleType(midType) .setSmallType(smallType) .setLBSInfo(lbsInfo) .setPlatForm(platform) .setShopId(shopId) .setItemTime(itemTime); ActionCombinerValue value = new ActionCombinerValue(); value.init(itemId, actBuilder.build()); if (Utils.isQNumValid(qq)) { String key = bid + "#" + qq + "#" + Constants.topN_alg_name; combinerKeys(key, value); } else if (!uid.equals("0") && !uid.equals("")) { String key = bid + "#" + uid + "#" + Constants.topN_Noqq_alg_name; if (debug) { logger.info("input ,key=" + key); } combinerKeys(key, value); } } catch (Exception e) { logger.error(e.getMessage(), e); } }
private void changeMapToPB( HashMap<Long, HashMap<String, HashMap<Integer, ActType>>> detailMap, Builder mergeValueBuilder) { Long time = System.currentTimeMillis() / 1000; ArrayList<Long> sortList = new ArrayList<Long>(detailMap.keySet()); Collections.sort( sortList, new Comparator<Long>() { @Override public int compare(Long arg0, Long arg1) { if (arg1.longValue() > arg0.longValue()) { return 1; } else { return -1; } } }); for (Long timeId : sortList) { if (timeId < Utils.getDateByTime(time - dataExpireTime)) { continue; } Recommend.UserActiveDetail.TimeSegment.Builder timeBuilder = Recommend.UserActiveDetail.TimeSegment.newBuilder(); timeBuilder.setTimeId(timeId); int count = 0; for (String item : detailMap.get(timeId).keySet()) { count++; if (count > topNum) { break; } Recommend.UserActiveDetail.TimeSegment.ItemInfo.Builder itemBuilder = Recommend.UserActiveDetail.TimeSegment.ItemInfo.newBuilder(); itemBuilder.setItem(item); float maxWeight = 0; for (Integer act : detailMap.get(timeId).get(item).keySet()) { ActType actValue = detailMap.get(timeId).get(item).get(act); float actWeight = Utils.getActionWeight(actValue.getActType()); if (actWeight > maxWeight) { maxWeight = actWeight; } itemBuilder.addActs(actValue); } if (maxWeight > 0) { timeBuilder.addItems(itemBuilder); } } mergeValueBuilder.addTsegs(timeBuilder.build()); } }
@SuppressWarnings({"unchecked", "rawtypes"}) @Override public void prepare(Map conf, TopologyContext context, OutputCollector collector) { super.prepare(conf, context, collector); updateConfig(super.config); this.mtClientList = TDEngineClientFactory.createMTClientList(conf); this.mt = MonitorTools.getMonitorInstance(conf); this.liveCombinerMap = new HashMap<String, ActionCombinerValue>(1024); this.putCallBack = new UpdateCallBack(mt, this.nsTableId, debug); int combinerExpireTime = Utils.getInt(conf, "combiner.expireTime", 5); setCombinerTime(combinerExpireTime); }
private void mergeToHeap( ActionCombinerValue newValueList, UserActiveDetail oldValueHeap, UserActiveDetail.Builder mergeValueBuilder) { HashMap<Long, HashMap<String, HashMap<Integer, ActType>>> detailMap = new HashMap<Long, HashMap<String, HashMap<Integer, ActType>>>(); if (oldValueHeap != null && oldValueHeap.getTsegsCount() > 0) { mergeOldToMap(oldValueHeap, detailMap); if (debug) { logger.info("add old values,now size=" + oldValueHeap.getTsegsCount()); } } for (String item : newValueList.getActRecodeMap().keySet()) { ActiveRecord action = newValueList.getActRecodeMap().get(item); Long winId = Utils.getDateByTime(action.getActTime()); mergeNewRecordsToMap(winId, item, action, detailMap); if (debug) { logger.info("add new values,size=" + detailMap.get(winId).size()); } } changeMapToPB(detailMap, mergeValueBuilder); }
@Override public void processEvent(String sid, Tuple tuple) { try { String bid = tuple.getStringByField("bid"); String adpos = Constants.DEFAULT_ADPOS; String qq = tuple.getStringByField("qq"); String itemId = tuple.getStringByField("item_id"); if (!Utils.isQNumValid(qq)) { return; } if (qq.equals("389687043") || qq.equals("475182144")) { logger.info("--input to combiner---" + tuple.toString()); } String actionType = tuple.getStringByField("action_type"); String actionTime = tuple.getStringByField("action_time"); String lbsInfo = tuple.getStringByField("lbs_info"); String platform = tuple.getStringByField("platform"); if (sid.equals(Constants.recommend_action_stream) && Utils.isRecommendAction(actionType)) { String actionResult = tuple.getStringByField("action_result"); String[] items = actionResult.split(";", -1); for (String resultItem : items) { if (resultItem.endsWith("#1") || resultItem.endsWith("#2")) { resultItem = resultItem.substring(0, resultItem.length() - 2); } if (Utils.isItemIdValid(resultItem)) { Recommend.UserActiveHistory.ActiveRecord.Builder actBuilder = Recommend.UserActiveHistory.ActiveRecord.newBuilder(); actBuilder .setItem(resultItem) .setActTime(Long.valueOf(actionTime)) .setActType(Integer.valueOf(actionType)) .setLBSInfo(lbsInfo) .setPlatForm(platform); ActionCombinerValue value = new ActionCombinerValue(); value.init(resultItem, actBuilder.build()); UpdateKey key = new UpdateKey(bid, Long.valueOf(qq), 0, adpos, resultItem); if (qq.equals("389687043") || qq.equals("475182144")) { logger.info( "--input impress to combiner---,itemId=" + resultItem + ",key=" + key.getDetailKey()); } combinerKeys(key.getDetailKey(), value); } } } else if (sid.equals(Constants.actions_stream) && !Utils.isRecommendAction(actionType)) { Recommend.UserActiveHistory.ActiveRecord.Builder actBuilder = Recommend.UserActiveHistory.ActiveRecord.newBuilder(); actBuilder .setItem(itemId) .setActTime(Long.valueOf(actionTime)) .setActType(Integer.valueOf(actionType)) .setLBSInfo(lbsInfo) .setPlatForm(platform); if (Utils.isItemIdValid(itemId)) { ActionCombinerValue value = new ActionCombinerValue(); value.init(itemId, actBuilder.build()); UpdateKey key = new UpdateKey(bid, Long.valueOf(qq), 0, adpos, itemId); combinerKeys(key.getDetailKey(), value); } } } catch (Exception e) { logger.error(e.getMessage(), e); } }