In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
How to use the Redis protocol? I believe that many inexperienced people are at a loss about this, so this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Redis protocol
The process of parsing the data mainly depends on the protocol of redis. Let's write a simple example and take a look at redis's protocol:
Public class RedisTest {public static void main (String [] args) {Jedis jedis = new Jedis ("127.0.0.1", 6379); jedis.set ("eat", "I want to eat");}}
Monitor socket:
Public static void main (String [] args) throws IOException {ServerSocket server = new ServerSocket (6379); Socket socket = server.accept (); byte [] chars = new byte [64]; socket.getInputStream (). Read (chars); System.out.println (new String (chars));}
Take a look at the data:
* 3 $3SET$3eat$13I want to eat
Refer to the official agreement document https://redis.io/topics/protocol to parse the data.
(1) simple string Simple Strings, beginning with "+" plus sign (2) error Errors, starting with "-" minus sign (3) integer type Integer, beginning with ":" colon (4) large string type Bulk Strings, beginning with "$" dollar sign, length limit 512m (5) group type Arrays, beginning with "*" asterisk and Each part of the agreement ends with "\ r\ n" (CRLF).
So the meaning of the above data is:
* 3 the array contains three elements, namely, SET, eat, I want to eat$3 is a string, and the string length is 3SET string content $3 is a string, and the string length is 3eat string content $13 is a string, and the string length is the content of 13I want to eat string
The data for performing get 'eat' is as follows:
* 2 $3GET$3eat
Hire a client.
Once we have mastered the redis protocol and socket, we can try to play a client.
Socket:
Public RedisClient (String host, int port) {try {this.socket = new Socket (host,port); this.outputStream = this.socket.getOutputStream (); this.inputStream = this.socket.getInputStream ();} catch (IOException e) {/ / TODO Auto-generated catch block e.printStackTrace ();}}
Set protocol:
Public String set (final String key, String value) {StringBuilder sb = new StringBuilder (); / / although the output will be escaped, we still have to send it with\ r\ nsb.append ("* 3"). Append ("\ r\ n"); sb.append ("$3"). Append ("\ r\ n"); sb.append ("SET"). Append ("\ r\ n") Sb.append ("$") .append (key.length ()) .append ("\ r\ n"); sb.append (key) .append ("\ r\ n"); sb.append ("$") .append (value.length ()) .append ("\ r\ n"); sb.append (value) .append ("\ r\ n"); byte [] bytes= new byte [1024]; try {outputStream.write (sb.toString (). GetBytes ()) InputStream.read (bytes);} catch (IOException e) {/ / TODO Auto-generated catch block e.printStackTrace ();} return new String (bytes);}
Test:
RedisClient redisClient = new RedisClient ("127.0.0.1", 6379); String result = redisClient.set ("eat", "please eat"); System.out.println (result)
Execution result:
OK after reading the above, have you mastered how to use the Redis protocol? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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.