In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article shows you what are the eight ways to call Jedis on the Redis Java client. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
Here is a brief introduction and comparison of jedis's methods of transaction, pipeline, and distributed invocation:
I. ordinary synchronization method
The simplest and most basic way to call
@ Testpublic void test1Normal () {Jedis jedis = new Jedis ("localhost"); long start = System.currentTimeMillis (); for (int I = 0; I < 1000000; iTunes +) {String result = jedis.set ("n" + I, "n" + I);} long end = System.currentTimeMillis (); System.out.println ("Simple SET:" (end-start) / 1000.0) + "seconds"); jedis.disconnect ();}
It's very simple, you can return the result after each set, whether the tag is successful or not.
II. Transaction mode (Transactions)
The transaction of redis is simple, and its main purpose is to ensure that commands in a transaction initiated by client can be executed continuously without inserting other client commands in the middle.
Look at the following example:
@ Testpublic void test2Trans () {Jedis jedis = new Jedis ("localhost"); long start = System.currentTimeMillis (); Transaction tx = jedis.multi (); for (int I = 0; I < 1000000; iTunes +) {tx.set ("t" + I, "t" + I);} List results = tx.exec (); long end = System.currentTimeMillis () System.out.println ("Transaction SET:" + (end-start) / 1000.0) + "seconds"); jedis.disconnect ();}
We call jedis.watch (...) Method to monitor the key, and if the key value changes after the call, the entire transaction fails. In addition, one operation in the transaction fails and other operations are not rolled back. This needs to be noted. Also, we can use the discard () method to cancel the transaction.
III. Pipeline (Pipelining)
Sometimes we need to send multiple instructions asynchronously, waiting for the results to be returned asynchronously. In this way, very good execution efficiency can be achieved. This is the pipeline, and the calling method is as follows:
@ Testpublic void test3Pipelined () {Jedis jedis = new Jedis ("localhost"); Pipeline pipeline = jedis.pipelined (); long start = System.currentTimeMillis (); for (int I = 0; I < 1000000; iTunes +) {pipeline.set ("p" + I, "p" + I);} List results = pipeline.syncAndReturnAll (); long end = System.currentTimeMillis () System.out.println ("Pipelined SET:" + ((end-start) / 1000.0) + "seconds"); jedis.disconnect ();} IV. Call the transaction in the pipeline
As far as the method provided by Jedis is concerned, it is possible to use transactions in the pipeline, the code is as follows:
@ Testpublic void test4combPipelineTrans () {jedis = new Jedis ("localhost"); long start = System.currentTimeMillis (); Pipeline pipeline = jedis.pipelined (); pipeline.multi (); for (int I = 0; I < 1000000; iTunes +) {pipeline.set ("" + I, "+ I);} pipeline.exec (); List results = pipeline.syncAndReturnAll (); long end = System.currentTimeMillis () System.out.println ("Pipelined transaction:" + (end-start) / 1000.0) + "seconds"); jedis.disconnect ();}
However, after testing (see later in this article), it is found that its efficiency is about the same as that of using transactions alone, or even slightly worse.
5. Distributed direct connection synchronous call @ Testpublic void test5shardNormal () {List shards = Arrays.asList (new JedisShardInfo ("localhost", 6379), new JedisShardInfo ("localhost", 6380)); ShardedJedis sharding = new ShardedJedis (shards); long start = System.currentTimeMillis (); for (int I = 0; I < 1000000; iTunes +) {String result = sharding.set ("sn" + I, "n" + I) } long end = System.currentTimeMillis (); System.out.println ("Simple@Sharing SET:" + ((end-start) / 1000.0) + "seconds"); sharding.disconnect ();}
This is a distributed direct connection, and it is a synchronous call, and each step of execution returns the execution result. Similarly, there are asynchronous pipeline calls.
6. Distributed direct connection asynchronous call @ Testpublic void test6shardpipelined () {List shards = Arrays.asList (new JedisShardInfo ("localhost", 6379), new JedisShardInfo ("localhost", 6380)); ShardedJedis sharding = new ShardedJedis (shards); ShardedJedisPipeline pipeline = sharding.pipelined (); long start = System.currentTimeMillis (); for (int I = 0; I < 100000) Pipeline.set ("sp" + I, "p" + I);} List results = pipeline.syncAndReturnAll (); long end = System.currentTimeMillis (); System.out.println ("Pipelined@Sharing SET:" + ((end-start) / 1000.0) + "seconds"); sharding.disconnect ();} VII. Distributed connection pool synchronous call
If your distributed calling code is running in a thread, then the above two directly connected calls are not appropriate, because the directly connected mode is not thread safe, and you must choose the connection pool call.
@ Testpublic void test7shardSimplePool () {List shards = Arrays.asList (new JedisShardInfo ("localhost", 6379), new JedisShardInfo ("localhost", 6380)); ShardedJedisPool pool = new ShardedJedisPool (new JedisPoolConfig (), shards); ShardedJedis one = pool.getResource (); long start = System.currentTimeMillis (); for (int I = 0; I < 1000000; iTunes +) {String result = one.set ("spn" + I, "n" + I) } long end = System.currentTimeMillis (); pool.returnResource (one); System.out.println ("Simple@Pool SET:" + (end-start) / 1000.0) + "seconds"); pool.destroy ();}
The above is synchronous and, of course, asynchronous.
8. Distributed connection pool asynchronous call @ Testpublic void test8shardPipelinedPool () {List shards = Arrays.asList (new JedisShardInfo ("localhost", 6379), new JedisShardInfo ("localhost", 6380); ShardedJedisPool pool = new ShardedJedisPool (new JedisPoolConfig (), shards); ShardedJedis one = pool.getResource (); ShardedJedisPipeline pipeline = one.pipelined (); long start = System.currentTimeMillis (); for (int I = 0; I < 100000) Pipeline.set ("sppn" + I, "n" + I);} List results = pipeline.syncAndReturnAll (); long end = System.currentTimeMillis (); pool.returnResource (one); System.out.println ("Pipelined@Pool SET:" + ((end-start) / 1000.0) + "seconds"); pool.destroy ();} IX.
Both transactions and pipes are in asynchronous mode. Query results cannot be synchronized in transactions and pipes. For example, the following two calls are not allowed:
Transaction tx = jedis.multi (); for (int I = 0; I < 1000000; iTunes +) {tx.set ("t" + I, "t" + I);} System.out.println (tx.get ("t1000"). Get ()); / / List results = tx.exec ();... ... Pipeline pipeline = jedis.pipelined (); long start = System.currentTimeMillis (); for (int I = 0; I < 1000000; iTunes +) {pipeline.set ("p" + I, "p" + I);} System.out.println (pipeline.get ("p1000"). Get (); / List results = pipeline.syncAndReturnAll () is not allowed
Transactions and pipes are asynchronous, and I feel that it is not necessary to make another transaction call in the pipeline, so it is better to proceed with the transaction mode directly.
In distribution, the performance of connection pooling is slightly better than that of direct connections (see subsequent testing section).
Transactions are not supported in distributed calls.
Because the transaction is implemented on the server side, and in distribution, the calling object of each batch may access a different machine, so the transaction cannot be performed.
10. Testing
Run the above code and test it, and the result is as follows:
Simple SET: 5.227 secondsTransaction SET: 0.5 secondsPipelined SET: 0.353 secondsPipelined transaction: 0.509 secondsSimple@Sharing SET: 5.289 secondsPipelined@Sharing SET: 0.348 secondsSimple@Pool SET: 5.039 secondsPipelined@Pool SET: 0.401 seconds
In addition, the more machines used in the tested distribution, the slower the call will be. There are 2 pieces above and 5 pieces below:
Simple@Sharing SET: 5.494 secondsPipelined@Sharing SET: 0.51 secondsSimple@Pool SET: 5.223 secondsPipelined@Pool SET: 0.518 seconds
Here are 10 tablets:
Simple@Sharing SET: 5.9 secondsPipelined@Sharing SET: 0.794 secondsSimple@Pool SET: 5.624 secondsPipelined@Pool SET: 0.762 seconds
Here are 100 tablets:
Simple@Sharing SET: 14.055 secondsPipelined@Sharing SET: 8.185 secondsSimple@Pool SET: 13.29 secondsPipelined@Pool SET: 7.767 seconds
In distributed, connection pooling is not only thread-safe, but also can be seen from the above test data that connection pooling is more efficient than direct connection.
11. Complete test code package com.example.nosqlclient;import java.util.Arrays;import java.util.List;import org.junit.AfterClass;import org.junit.BeforeClass;import org.junit.Test;import redis.clients.jedis.Jedis;import redis.clients.jedis.JedisPoolConfig;import redis.clients.jedis.JedisShardInfo;import redis.clients.jedis.Pipeline;import redis.clients.jedis.ShardedJedis;import redis.clients.jedis.ShardedJedisPipeline;import redis.clients.jedis.ShardedJedisPool;import redis.clients.jedis.Transaction;import org.junit.FixMethodOrder Import org.junit.runners.MethodSorters;@FixMethodOrder (MethodSorters.NAME_ASCENDING) public class TestJedis {private static Jedis jedis; private static ShardedJedis sharding; private static ShardedJedisPool pool; @ BeforeClass public static void setUpBeforeClass () throws Exception {List shards = Arrays.asList (new JedisShardInfo ("localhost", 6379), new JedisShardInfo ("localhost", 6379)) / / use the same ip:port and only test jedis = new Jedis ("localhost"); sharding = new ShardedJedis (shards); pool = new ShardedJedisPool (new JedisPoolConfig (), shards);} @ AfterClass public static void tearDownAfterClass () throws Exception {jedis.disconnect (); sharding.disconnect (); pool.destroy () } @ Test public void test1Normal () {long start = System.currentTimeMillis (); for (int I = 0; I < 1000000; iTunes +) {String result = jedis.set ("n" + I, "n" + I);} long end = System.currentTimeMillis (); System.out.println ("Simple SET:" + ((end-start) / 1000.0) + "seconds") } @ Test public void test2Trans () {long start = System.currentTimeMillis (); Transaction tx = jedis.multi (); for (int I = 0; I < 1000000; iTunes +) {tx.set ("t" + I, "t" + I);} / / System.out.println (tx.get ("t1000"). Get (); List results = tx.exec () Long end = System.currentTimeMillis (); System.out.println ("Transaction SET:" + ((end-start) / 1000.0) + "seconds");} @ Test public void test3Pipelined () {Pipeline pipeline = jedis.pipelined (); long start = System.currentTimeMillis (); for (int I = 0; I < 1000000; iTunes +) {pipeline.set ("p" + I, "p" + I) } / / System.out.println (pipeline.get ("p1000"). Get (); List results = pipeline.syncAndReturnAll (); long end = System.currentTimeMillis (); System.out.println ("Pipelined SET:" + ((end-start) / 1000.0) + "seconds");} @ Test public void test4combPipelineTrans () {long start = System.currentTimeMillis (); Pipeline pipeline = jedis.pipelined () Pipeline.multi (); for (int I = 0; I < 1000000; iTunes +) {pipeline.set ("+ I," + I);} pipeline.exec (); List results = pipeline.syncAndReturnAll (); long end = System.currentTimeMillis (); System.out.println ("Pipelined transaction:" + ((end-start) / 1000.0) + "seconds") } @ Test public void test5shardNormal () {long start = System.currentTimeMillis (); for (int I = 0; I < 1000000; iTunes +) {String result = sharding.set ("sn" + I, "n" + I);} long end = System.currentTimeMillis (); System.out.println ("Simple@Sharing SET:" + ((end-start) / 1000.0) + "seconds") } @ Test public void test6shardpipelined () {ShardedJedisPipeline pipeline = sharding.pipelined (); long start = System.currentTimeMillis (); for (int I = 0; I < 1000000; iTunes +) {pipeline.set ("sp" + I, "p" + I);} List results = pipeline.syncAndReturnAll (); long end = System.currentTimeMillis () System.out.println ("Pipelined@Sharing SET:" + ((end-start) / 1000.0) + "seconds");} @ Test public void test7shardSimplePool () {ShardedJedis one = pool.getResource (); long start = System.currentTimeMillis (); for (int I = 0; I < 1000000; iTunes +) {String result = one.set ("spn" + I, "n" + I) } long end = System.currentTimeMillis (); pool.returnResource (one); System.out.println ("Simple@Pool SET:" + ((end-start) / 1000.0) + "seconds");} @ Test public void test8shardPipelinedPool () {ShardedJedis one = pool.getResource (); ShardedJedisPipeline pipeline = one.pipelined (); long start = System.currentTimeMillis (); for (int I = 0 I < 1000000; iTunes +) {pipeline.set ("sppn" + I, "n" + I);} List results = pipeline.syncAndReturnAll (); long end = System.currentTimeMillis (); pool.returnResource (one); System.out.println ("Pipelined@Pool SET:" + ((end-start) / 1000.0) + "seconds") }} what are the eight ways to call Jedis on the Redis Java client? have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.