Data warehouse-zipper table (java/hive implementation)

One, Java implements the zipper table

Technical selection:MyBatis

1) mysql build table

create database mydemo;
use mydemo;
create table orders(
orderid int primary key not null auto_increment,
orderno varchar(20),
orderstatus int,
begintime date,
overtime date
);

2) Use Maven's quickstart project to add two dependencies

	<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.38</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.4.6</version>
    </dependency>

3) Create a new resource folder, add xml configuration

①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="cn.kgc.dwlearn.mylink.entity.Orders" alias="order"></typeAlias>
    </typeAliases>
    <environments default="jun">
        <environment id="jun">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://192.168.247.188:3306/mydemo"/>
                <property name="username" value="root"/>
                <property name="password" value="javakb10"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="mapper/orders.xml"></mapper>
    </mappers>
</configuration>

②orders.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="cn.kgc.dwlearn.mylink.dao.OrdersDAO">
    <insert id="addOrder" parameterType="order">
        insert into orders(orderno,orderstatus,begintime,overtime)
            values(#{orderno},#{orderstatus},#{begintime},'9999-12-31')
    </insert>
    <update id="updateOrder" parameterType="order">
        update orders set overtime=#{overtime} where orderid=#{orderid}
    </update>
    <select id="findByOrderno" parameterType="java.lang.String" resultType="order">
        select * from orders where orderno=#{orderno} and overtime='9999-12-31'
    </select>
</mapper>

4) Create an entity class Orders

package cn.kgc.dwlearn.mylink.entity;

import java.util.Date;

public class Orders {
    
    
    private int orderid;
    private String orderno;
    private int orderstatus;
    private Date begintime;
    private Date overtime;

    public Orders() {
    
    
    }

    public Orders(int orderid, String orderno, int orderstatus, Date begintime, Date overtime) {
    
    
        this.orderid = orderid;
        this.orderno = orderno;
        this.orderstatus = orderstatus;
        this.begintime = begintime;
        this.overtime = overtime;
    }

    public void setOrderid(int orderid) {
    
    
        this.orderid = orderid;
    }

    public void setOrderno(String orderno) {
    
    
        this.orderno = orderno;
    }

    public void setOrderstatus(int orderstatus) {
    
    
        this.orderstatus = orderstatus;
    }

    public void setBegintime(Date begintime) {
    
    
        this.begintime = begintime;
    }

    public void setOvertime(Date overtime) {
    
    
        this.overtime = overtime;
    }

    public int getOrderid() {
    
    
        return orderid;
    }

    public String getOrderno() {
    
    
        return orderno;
    }

    public int getOrderstatus() {
    
    
        return orderstatus;
    }

    public Date getBegintime() {
    
    
        return begintime;
    }

    public Date getOvertime() {
    
    
        return overtime;
    }
}

5) OrdersDAO interface

package cn.kgc.dwlearn.mylink.dao;

import cn.kgc.dwlearn.mylink.entity.Orders;

public interface OrdersDAO {
    
    
    public void addOrder(Orders orders);
    public void updateOrder(Orders orders);
    public Orders findByOrderno(String orderno);
}

6) Test category

package cn.kgc.dwlearn.mylink.demo;

import cn.kgc.dwlearn.mylink.dao.OrdersDAO;
import cn.kgc.dwlearn.mylink.entity.Orders;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.Reader;
import java.util.Date;

public class MyDemo {
    
    
    public static void main(String[] args) throws Exception{
    
    
        Reader reader = Resources.getResourceAsReader("mybatis.cfg.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
        SqlSession session = factory.openSession();
        OrdersDAO odao = session.getMapper(OrdersDAO.class);

        //先去数据库查询是否有订单123456
        //创建一个新订单
        Orders ord = new Orders(1,"123456",1,new Date(),null);
        Orders order = odao.findByOrderno(ord.getOrderno());
        if (order != null){
    
    
            order.setOvertime(new Date());
            odao.updateOrder(order);
        }
        odao.addOrder(ord);
        session.commit();
        session.close();
    }
}

Let's execute the program once, the orders table will have one more data
Insert picture description here
and then insert another record.
Insert picture description here
At this point, we will insert another data with orderno 123456and orderstatus to 2update the order status.
Insert picture description here
The principle is that when inserting data, it will determine whether the orderno exists, if it does not exist, insert the data; if it does, it will update the end time of the previous data overtimeand insert the ordernonew order status data


Two, Hive realizes the zipper table

pending upgrade…

Guess you like

Origin blog.csdn.net/weixin_48482704/article/details/114953982