解决方法:
1.核心方法
1 public static boolean pipelineHmset (int dbIndex,String key,List<Map<String, String>> list){ 2 3 Jedis jedis = null; 4 5 try { 6 7 jedis = getJedis();//获取jedis实例 8 9 jedis.select(dbIndex); 10 11 Pipeline pip = jedis.pipelined(); 12 13 for (Map<String, String> keysmap : list) { 14 15 //批量插入 16 17 pip.hmset(key, keysmap); 18 19 } 20 21 pip.sync();//同步 22 23 } catch (Exception e) { 24 25 e.printStackTrace(); 26 27 }finally{ 28 29 if (jedis != null) { 30 31 jedis.close(); 32 33 } 34 35 } 36 37 return false; 38 39 }
2.使用方法
批量插入10条记录到redis的rd_table表中
1 public static void main(String[] args) throws Exception{ 2 3 List<Map<String, String>> addList=new ArrayList<Map<String,String>>(); 4 5 for(int i=1;i<10;i++){ 6 7 HashMap<String, String> hashMap = new HashMap<String, String>(); 8 9 hashMap.put("key"+i,"value"+i); 10 11 addList.add(hashMap); 12 13 } 14 15 if(addList.size()>0){ 16 17 pipelineHmset(0, "rd_table", addList); 18 19 } 20 21 }