记录一次手动导入redis lpush的代码 , 主要功能 使用 lpush(key, v1, v2, ...) 的方式 ,批量导入
实现类
import redis.clients.jedis.Jedis;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* @author : gorey
*/
public class App {
public static void main(String[] args) throws IOException {
if (args.length == 0){
System.out.println("usage : ./app.jar filename.log");
return;
}
Jedis j = new Jedis("target ip", port, false);
j.connect();
j.auth("redis-password");
String key = "backup.key";
BufferedReader br = new BufferedReader(new FileReader(args[0]));
String line ;
List<String> list = new ArrayList<String>();
int pushCount = 1;
Long rst = null;
while((line = br.readLine())!= null){
if (list.size() >= 1000) {
rst = j.lpush(key, list.toArray(new String[0]));
list.clear();
System.out.println("push count " + pushCount ++ + " push rst : " + rst);
}
}
if (list.size() > 0) {
rst = j.lpush(key, list.toArray(new String[0]));
System.out.println("push count " + pushCount ++ + " push rst : " + rst);
}
j.disconnect();
System.out.println("Done");
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.example</groupId>
<artifactId>reids-left-push</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.2.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
```xml