zoukankan      html  css  js  c++  java
  • Java 之 批量从mysql导入Redis

    一、pom

    从插入redis数据需要转成JSON => 需要jackson的三个包!

    java操作redis => 使用Jedis

    <dependencies>
        <dependency>
          <groupId>org.mybatis</groupId>
          <artifactId>mybatis</artifactId>
          <version>3.4.6</version>
        </dependency>
        <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
          <version>5.1.38</version>
        </dependency>
    <!-- java拿到List<Object> => 转成JSON -->
        <dependency>
          <groupId>com.fasterxml.jackson.core</groupId>
          <artifactId>jackson-databind</artifactId>
          <version>2.10.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
        <dependency>
          <groupId>com.fasterxml.jackson.core</groupId>
          <artifactId>jackson-core</artifactId>
          <version>2.10.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
        <dependency>
          <groupId>com.fasterxml.jackson.core</groupId>
          <artifactId>jackson-annotations</artifactId>
          <version>2.10.0</version>
        </dependency>
        <!-- redis -->
        <!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
        <dependency>
          <groupId>redis.clients</groupId>
          <artifactId>jedis</artifactId>
          <version>3.1.0</version>
        </dependency>
      </dependencies>

    二、目录

    三、配置

    mybatis.cfg.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE configuration PUBLIC
            "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
        <typeAliases>
            <typeAlias type="com.njbdqn.mor.entity.SalrsOrder" alias="so"></typeAlias>
        </typeAliases>
        <environments default="cm">
            <environment id="cm">
                <transactionManager type="JDBC"></transactionManager>
                <dataSource type="POOLED">
                    <property name="driver" value="com.mysql.jdbc.Driver"/>
                    <property name="url" value="jdbc:mysql://192.168.56.111:3306/sales_source"/>
                    <property name="username" value="root"/>
                    <property name="password" value="root"/>
                </dataSource>
            </environment>
        </environments>
        <mappers>
            <mapper resource="mapper/so.xml"></mapper>
        </mappers>
    </configuration>

    mapper/so.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    
    <mapper namespace="com.njbdqn.mor.dao.SalesOrderDao">
        <select id="findAll" resultType="so">
        SELECT order_number AS orderNumber,
                customer_number AS customerNumber,
                product_code AS productCode,
                order_date AS orderDate,
                entry_date AS entryDate,
                order_amount AS orderAmount FROM sales_order
        </select>
    </mapper>

    四、代码

    实体类:

    public class SalesOrder {
        private int orderNumber;
        private int customerNumber;
        private int productCode;
        private Date orderDate;
        private Date entryDate;
        private int orderAmount ;

    dao层:

    public interface SalesOrderDao {
        List<SalesOrder> findAll();
    }

    Test层:

    public static String tucketNo(int i){
            // 此方法如果String值特别大会出现负数
            // hadoop默认的hashpartitioner中为了解决这个问题,使用与运算:(key.hashCode() & Integer.MAX_VALUE) % numReduceTasks;
            return (i+"").hashCode()%100000+"";
        }
    
        public static void main(String[] args) throws Exception {
            Reader read = Resources.getResourceAsReader("mybatis.cfg.xml");
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(read);
            SqlSession session = factory.openSession();
            SalesOrderDao sod = session.getMapper(SalesOrderDao.class);
            List<SalesOrder> all = sod.findAll();
            // 转化成json,在redis默认第0个hashmap中,salesorder这个key中存所有记录
            ObjectMapper om = new ObjectMapper();
            Jedis jedis = new Jedis("192.168.56.111",6379);
            for (SalesOrder so :all){ // 存JSON格式的String
                jedis.lpush("salesorder",om.writeValueAsString(so));
            }
            jedis.close();
            session.close();

    五、验证Redis中数据

    192.168.56.111:6379> LLEN salesorder
    (integer) 100000
    192.168.56.111:6379> LINDEX salesorder 100
    "{"orderNumber":99900,"customerNumber":2,"productCode":3,"orderDate":1531929600000,"entryDate":1531929600000,"orderAmount":1454}"
  • 相关阅读:
    CSS3相关编码规范
    WEB开发中常见的漏洞
    Python常用端口扫描
    33、Django实战第33天:我的消息
    32、Django实战第32天:我的收藏
    31、Django实战第31天:我的课程
    30、Django实战第30天:修改邮箱和用户信息
    29、Django实战第29天:修改密码和头像
    28、Django实战第28天:个人信息展示
    27、Django实战第27天:全局搜索功能开发
  • 原文地址:https://www.cnblogs.com/sabertobih/p/14101970.html
Copyright © 2011-2022 走看看