实例
public class HbaseDataCache2Db { private static Logger LOG = LoggerFactory.getLogger(HbaseDataCache2Db.class); private static ThreadLocal<Map<String, List<Put>>> THREAD_DATA = new ThreadLocal<>(); public static void cacheData(String key, Put put) { Map<String, List<Put>> cacheMap = THREAD_DATA.get(); if (cacheMap == null) { cacheMap = new HashMap<>(); } List<Put> tmp = cacheMap.get(key); if (tmp == null) { tmp = new ArrayList<>(); } tmp.add(put); cacheMap.put(key, tmp); THREAD_DATA.set(cacheMap); } public static void flushData() { try { Map<String, List<Put>> cacheMap = THREAD_DATA.get(); if (cacheMap == null) { return; } for (String tableName : cacheMap.keySet()) { HbaseUtil.putData(tableName, cacheMap.get(tableName)); } cacheMap.clear(); THREAD_DATA.set(cacheMap); } catch (Exception e) { LOG.error("Hbase 数据写入异常:", e); } } public static void cacheData(Map<String, List<Put>> cacheMap, String key, Put put) { List<Put> tmp = cacheMap.get(key); if (tmp == null) { tmp = new ArrayList<>(); } tmp.add(put); cacheMap.put(key, tmp); } public static void flushData(Map<String, List<Put>> cacheMap) { try { for (String tableName : cacheMap.keySet()) { HbaseUtil.putData(tableName, cacheMap.get(tableName)); } } catch (Exception e) { LOG.error("Hbase 数据写入异常:", e); } } }
采用了map k,v 的方法