gpt4 book ai didi

java - 通过 Jedis lpush 将百万条记录添加到 Redis - 连接由 peer : socket write error 重置

转载 作者:可可西里 更新时间:2023-11-01 11:31:21 24 4
gpt4 key购买 nike

当我向 Redis 中添加一百万(1,000,000)时,就可以了。
当我添加两百万 (2,000,000) 条记录时,出现错误 Connection reset by peer: socket write error

根据 Redis data types list ,

列表的最大长度为 232 - 1 个元素(4294967295,每个列表超过 40 亿个元素)。

/*Creating the json list*/
Gson gson = new GsonBuilder().create();
List<String> employeeList = new ArrayList<String>();
for (int i = 1; i <= 2000000; i++) {
Employee employee = new Employee(i + "", "Jhon", "My Country", "redis@gmail.com", "+777 92157325");
String json = gson.toJson(employee);
employeeList.add(json);
}

/*add json list to Redis*/
Jedis jedis = pool.getResource();
// employeeList size is = 2,000,000
String[] jsonArray = employeeList.toArray(new String[employeeList.size()]);
jedis.lpush("employee_list_1", jsonArray);

日志

Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketException: Connection reset by peer: socket write error
at redis.clients.jedis.Protocol.sendCommand(Protocol.java:83)
at redis.clients.jedis.Protocol.sendCommand(Protocol.java:63)
at redis.clients.jedis.Connection.sendCommand(Connection.java:84)
at redis.clients.jedis.BinaryClient.lpush(BinaryClient.java:281)
at redis.clients.jedis.Client.lpush(Client.java:205)
at redis.clients.jedis.Jedis.lpush(Jedis.java:869)
at com.mutu.redis.AddJosn.add(AddJosn.java:29)
at com.mutu.redis.AddJosn.main(AddJosn.java:48)
Caused by: java.net.SocketException: Connection reset by peer: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109)
at java.net.SocketOutputStream.write(SocketOutputStream.java:153)
at redis.clients.util.RedisOutputStream.flushBuffer(RedisOutputStream.java:31)
at redis.clients.util.RedisOutputStream.write(RedisOutputStream.java:54)
at redis.clients.util.RedisOutputStream.write(RedisOutputStream.java:44)
at redis.clients.jedis.Protocol.sendCommand(Protocol.java:79)
... 7 more

如何通过单笔交易/流程添加两百万条或更多条记录?

最佳答案

Connection reset by peer 表示Redis断开Jedis实例底层的socket连接。检查您的 Redis 日志记录了哪些警告/错误消息。

顺便说一句,由于带数组的 lpush 被发送到一个命令,请求正文应该非常大。我认为你这样做只是为了测试目的,但我建议你需要使用 multi - exec 并将你的请求分解为大量请求。它仍然保证原子性。

如果您不需要原子性,您可以使用管道来分解您的请求。

关于java - 通过 Jedis lpush 将百万条记录添加到 Redis - 连接由 peer : socket write error 重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39829236/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com