将文本中的数据批量写入redis

在redis批量造数辅助测试impala中UDF的性能

pom.xml

        <!-- redis -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>

 BeidouUdfToRedis.java

import org.apache.commons.io.FileUtils;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;

public class BeidouUdfToRedis {

    public void beidouUDF(int count,String key) {
        LinkedHashSet<HostAndPort> redisClusterNodes = RedisUtil.getRedisClusterNodes();
        JedisCluster jedisCluster = new JedisCluster(redisClusterNodes, 30 * 100, 10);
        JedisClusterPipeline pipelined = JedisClusterPipeline.pipelined(jedisCluster);
        String hashKey = key;

        //将数据从txt中取出保存在hashMap中
        List list = null;
        try {
            list = FileUtils.readLines(new File("/Users/yiqin/Desktop/crm.txt"));

        } catch (IOException e) {
            e.printStackTrace();
        }

        HashMap<String,String> hashMap = new HashMap<String,String>();

        for (Object str : list) {
            String[] city = str.toString().split(",");
            hashMap.put(city[0],city[1]);
        }
        System.out.println(hashMap);

        int num = 0;
        for(int i =0;i < count;i++){ //写入多少条

            pipelined.hmset(hashKey, hashMap);
            pipelined.expire(hashKey,60*60*24*2);

            num++;
            if(num > 500) {
                pipelined.sync();
            }
        }

        try {
            jedisCluster.close();
            pipelined.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        BeidouUdfToRedis beidouUdfToRedis = new BeidouUdfToRedis();
        beidouUdfToRedis.beidouUDF(1000,"P227:dict_property_$crm_id_mapping_value");
    }

}

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值