实例
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 的方法
public class MysqlDataCache2Db { private static Logger LOG = LoggerFactory.getLogger(MysqlDataCache2Db.class); private static ThreadLocal<Map<String, List<Object>>> THREAD_DATA = new ThreadLocal<>(); public static String KEY_WX = "wx_base_info"; public static String KEY_WX_ARTICLE = "wx_article_info"; public static void cacheData(String key, Object obj) { Map<String, List<Object>> cacheMap = THREAD_DATA.get(); if (cacheMap == null) { cacheMap = new HashMap<>(); } List<Object> tmp = cacheMap.get(key); if (tmp == null) { tmp = new ArrayList<>(); } tmp.add(obj); cacheMap.put(key, tmp); THREAD_DATA.set(cacheMap); } public static void flushData() { try { Map<String, List<Object>> cacheMap = THREAD_DATA.get(); if (cacheMap == null) { return; } for (String tableName : cacheMap.keySet()) { List<Object> tmpList = cacheMap.get(tableName); SqlUtilBusiness.data2DB(tableName, tmpList); String sql = "update " + tableName + " set deptCode=cityCode where deptCode=‘‘ OR deptCode is null"; SqlUtilBusiness.exceteSql(sql); } cacheMap.clear(); THREAD_DATA.set(cacheMap); } catch (Exception e) { LOG.error("MYSQL 数据写入异常:", e); } } public static void cacheData(Map<String, List<Object>> cacheMap, String key, Object obj) { List<Object> tmp = cacheMap.get(key); if (tmp == null) { tmp = new ArrayList<>(); } tmp.add(obj); cacheMap.put(key, tmp); } public static void flushData(Map<String, List<Object>> cacheMap) { try { for (String tableName : cacheMap.keySet()) { List<Object> tmpList = cacheMap.get(tableName); SqlUtilBusiness.data2DB(tableName, tmpList); String sql = "update " + tableName + " set deptCode=cityCode where deptCode=‘‘ OR deptCode is null"; SqlUtilBusiness.exceteSql(sql); cacheMap.clear(); } } catch (Exception e) { LOG.error("MYSQL 数据写入异常:", e); } } }