• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

redis批量导入数据

武飞扬头像
TimetravelDa
帮助1

一、采用redis 管道 --pipe

1、先生成文本数据 redis.txt 的 文件格式

有两种文件格式 都是可以的(以下采用的是redis 的hash村粗怒)

  1.  
    *6
  2.  
    $5
  3.  
    hmset
  4.  
    $24
  5.  
    user_profile:user_info:0
  6.  
    $4
  7.  
    name
  8.  
    $3
  9.  
    317
  10.  
    $3
  11.  
    age
  12.  
    $3
  13.  
    187

解释:

*6 表示有三个字符  

$5 表示 hmset字符长度为5 也就是我们的命令

$24表示 user_profile:user_info:0的长度为24 也就是我们的key

以此类推

格式二:

  1.  
    hmset user_profile:user_info:0 name 张三 age 12
  2.  
    hmset user_profile:user_info:0 name 李四 age 13

2、文件格式

[atguigu@hadoop105 ~] unix2dos redis.txt

上面的命令会去掉行尾的^M符号

3、执行命令

[atguigu@hadoop105 ~] cat redis.txt | ./redis-cli -h 127.0.0.1 -a password - p 6379 --pipe

遇到的坑

1: 没有 unix2dos 命令

yum install unix2dos 

二、采用Java api 方式

  1.  
    @Test
  2.  
    public void importRedis(){
  3.  
    Jedis jedis = new Jedis("127.0.0.1", 6379);
  4.  
    //管道
  5.  
    Pipeline pipe = jedis.pipelined();
  6.  
     
  7.  
    for (int i=0;i<1000000;i ){
  8.  
    Map<String, String> map = new HashMap<>();
  9.  
    map.put("name", "张三");
  10.  
    map.put("age","12");
  11.  
    pipe.hmset("user_profile:user_info:" i, map);
  12.  
    }
  13.  
    // 执行命令
  14.  
    pipe.sync();
  15.  
    jedis.close();
  16.  
    }
学新通

三、批量删除key

[atguigu@hadoop105 ~] ./redis-cli -h 127.0.0.1 -p 6379 KEYS "user_profile*" | xargs ./redis-cli -h 127.0.0.1 -p 6379 DEL

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfkkfif
系列文章
更多 icon
同类精品
更多 icon
继续加载