Java uses the redis database, javaredis Database
Use maven to manage jar and add dependency:
<Dependency>
<GroupId> redis. clients </groupId>
<ArtifactId> jedis </artifactId>
<Version> 2.7.0 </version>
</Dependency> // The required jar
Use the redis database configuration (set the attribute value as needed) to store data
JedisPool jedisPool = null;
JedisPoolConfig poolConfig = new JedisPoolConfig ();
PoolConfig. setMaxIdle (10 );
PoolConfig. setMaxTotal (100 );
PoolConfig. setMaxWaitMillis (10000 );
PoolConfig. setTestOnBorrow (true );
JedisPool = new JedisPool (poolConfig, "192.168.0.109", 6379); // configure the Jedis configuration, port, and server address.
Stored Procedure
List <String> lines = Files. readAllLines (Paths. get ("E:/oooooooo/task.txt"); // the data source to be stored. The data format is mostly a json String.
Jedis jedis = jedisPool. getResource ();
Jedis. select (3); // 3 refers to the partition on the database, which can be set freely.
Pipeline pipelined = jedis. pipelined ();
Lines. forEach (l-> {
Pipelined. lpush (Database Name, l );
});
Pipelined. sync (); // This code is used to store data in redis databases in batches through pipelines.
Obtain redis data through java
JedisPoolConfig poolConfig = new JedisPoolConfig ();
PoolConfig. setMaxIdle (10 );
PoolConfig. setMaxTotal (500 );
PoolConfig. setMaxWaitMillis (100000 );
PoolConfig. setTestOnBorrow (true );
JedisPool = new JedisPool (poolConfig, server address, port, timeout, Database Password );
Jedis jedis = jedisPool. getResource ();
Jedis. select (3); // 3 refers to the partition on the database, which can be set freely.
String targetString = jedis. lpop (Database Name); // targetString is the retrieved target data.
JedisPool. returnResourceObject (jedis); // after an operation, remember to release jedis resources.