簡體   English   中英

Jedis lpush將數百萬條記錄添加到Redis中-對等方重置連接:套接字寫入錯誤

[英]Adding the million records into Redis by Jedis lpush - Connection reset by peer: socket write error

當我向Redis添加一百萬(1,000,000)時,就可以了。
我收到錯誤, Connection reset by peer: socket write error當我添加兩百萬(2,000,000)記錄時, Connection reset by peer: socket write error

根據Redis數據類型列表

一個列表的最大長度為2個32 - 1元件(4294967295,超過4十億每列表中的元素)。

/*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

如何通過單個事務/流程添加兩個或更多百萬條記錄?

通過對等方重置連接意味着Redis斷開與Jedis實例相關的套接字連接。 檢查您的Redis日志,其中記錄了哪個警告/錯誤消息。

順便說一句,因為帶有數組的lpush被發送到一個命令,所以請求主體應該真的很大。 我認為您這樣做只是出於測試目的,但是我建議您需要使用multi-exec並將大量的請求分解為很多請求。 它仍然保證原子性。

如果不需要原子性,則可以使用管道分解請求。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM