Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Implementation of batch insertion using pipeline in Redis Cluster

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Because the bulk insert feature needs to be used in the project, you can use pipeline to efficiently insert Redis batch insert found on the Internet. The sample code is as follows:

String key = "key"; Jedis jedis = new Jedis ("xx.xx.xx.xx"); Pipeline p = jedis.pipelined (); List myData =. / / data list to be inserted for (String data: myData) {p.hset (key, data);} p.sync (); jedis.close ()

But in fact, the problem encountered is that the Redis used in the project is a cluster, and the class used in initialization is JedisCluster instead of Jedis. I checked the JedisCluster documentation and found no pipelined () method to get the Pipeline object like Jedis.

Google found a solution for a while.

The Redis cluster specification says that the key space of the Redis cluster is divided into 16384 slot, and the maximum number of nodes in the cluster is 16384. Each master node is responsible for handling some of the 16384 hash slots. When we say that a cluster is in a "stable" state, it means that the cluster is not performing a reconfiguration operation, and each hash slot is handled by only one node.

So we can know the number of the slot corresponding to this key according to the key to be inserted, and then find the corresponding Jedis from the cluster through the number of this slot. The specific implementation is as follows

/ / after initializing the jedis cluster, you will not write how to get the HostAndPort collection code Set nodes = .JedisCluster jedisCluster = new JedisCluster (nodes); Map nodeMap = jedisCluster.getClusterNodes (); String anyHost = nodeMap.keySet (). Iterator (). Next (); / / getSlotHostMap method has TreeMap slotHostMap = getSlotHostMap (anyHost); private static TreeMap getSlotHostMap (String anyHostAndPortStr) {TreeMap tree = new TreeMap (); String parts [] = anyHostAndPortStr.split (":") HostAndPort anyHostAndPort = new HostAndPort (parts [0], Integer.parseInt (parts [1])); try {Jedis jedis = new Jedis (anyHostAndPort.getHost (), anyHostAndPort.getPort ()); List list = jedis.clusterSlots (); for (Object object: list) {List list1 = (List) object; List master = (List) list1.get (2) String hostAndPort = new String ((byte []) master.get (0)) + ": + master.get (1); tree.put ((Long) list1.get (0), hostAndPort); tree.put ((Long) list1.get (1), hostAndPort);} jedis.close ();} catch (Exception e) {} return tree;}

The above steps can be completed at initialization time. Do not need to call every time, define nodeMap and slotHostMap as static variables.

/ / get slot number int slot = JedisClusterCRC16.getSlot (key); / / get the corresponding Jedis object Map.Entry entry = slotHostMap.lowerEntry (Long.valueOf (slot)); Jedis jedis = nodeMap.get (entry.getValue ()) .getResource ()

It is recommended that the above operation can be encapsulated into a static method, such as named public static Jedis getJedisByKey (String key) and so on. It means that in the cluster, the Jedis object corresponding to the key is obtained through key.

In this way, batch inserts can be carried out through the above jedis.pipelined ();.

Note: this method is searched from Google, so far I haven't found any problems in using it. If any great god finds anything wrong, you are welcome to bring it up.

The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report