[Hive基本概念之--hive分区]

目录

前言:

添加MyBatis和Hive依赖

配置MyBatis和Hive连接信息

在Spring Boot应用中定义MyBatis Mapper,例如:

 定义实体类

MyBatis Mapper接口

 Batis Mapper接口,insert方法对应Mapper中的insert方法,selectByPartition方法对应Mapper中的selectByPartition方法。

测试分区代码:


前言:

   记录笔记

添加MyBatis和Hive依赖



    org.mybatis.spring.boot
    mybatis-spring-boot-starter
    2.2.0


    com.alibaba
    druid
    1.2.8




    org.apache.hive
    hive-jdbc
    3.1.2


    org.apache.hadoop
    hadoop-common
    3.2.1

mybatis-spring-boot-starter是MyBatis的Spring Boot启动器,druid是连接池库。

配置MyBatis和Hive连接信息

在application.properties文件中配置MyBatis和Hive的连接信息

spring.datasource.url=jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=root

mybatis.mapper-locations=classpath:mapper/**/*.xml

hive.jdbc.url=jdbc:hive2://localhost:10000/default
hive.jdbc.username=hiveuser
hive.jdbc.password=hivepassword

其中,spring.datasource.url、spring.datasource.username和spring.datasource.password分别指定MyBatis连接的数据库URL、用户名和密码;mybatis.mapper-locations指定MyBatis的Mapper文件所在位置;hive.jdbc.url、hive.jdbc.username和hive.jdbc.password分别指定连接Hive的用户名和密码。

创建Hive分区表:

CREATE TABLE mytable (
    col1 string,
    col2 int
)
PARTITIONED BY (dt string);

    mytable是表名,col1和col2是表的普通列,dt是分区列。注意,创建表需要在Hive客户端中进行,不能在Spring Boot应用中执行。

在Spring Boot应用中定义MyBatis Mapper,例如:




    
        
        
        
        
    

    
        INSERT INTO mytable (col1, col2, dt) VALUES (#{col1}, #{col2}, #{dt})
    

    

其中,insert方法用于向Hive分区表中插入数据,selectByPartition方法用于查询指定分区的数据。

 定义实体类

public class MyTable { 2 private Long id; 3 private String col1; 4 private Integer col2; 5 private String dt; 6 // getter和setter省略 7}

其中,id、col1和col2分别对应表中的列,dt对应分区列。

MyBatis Mapper接口

@Mapper
public interface MyTableMapper {
    void insert(MyTable myTable);

    List selectByPartition(String partition);
}

 Batis Mapper接口,insert方法对应Mapper中的insert方法,selectByPartition方法对应Mapper中的selectByPartition方法。

在Spring Boot应用中实现向Hive分区表插入数据的代码,例如:


@Service
public class HivePartitionService {

    @Autowired
    private MyTableMapper myTableMapper;

    public void insertPartition(MyTable myTable) {
        myTableMapper.insert(myTable);
    }


    public List queryPartition(String partition) {
        return myTableMapper.selectByPartition(partition);
    }
}

 

其中,partition参数代表要查询的分区。

测试分区代码:

@SpringBootTest
class HivePartitionServiceTest {

    @Autowired
    private HivePartitionService hivePartitionService;

    @Test
    void insertPartition() {
        MyTable myTable = new MyTable();
        myTable.setCol1("test");
        myTable.setCol2(123);
        myTable.setDt("20210901");
        hivePartitionService.insertPartition(myTable);
    }

    @Test
    void queryPartition() {
        List myTables = hivePartitionService.queryPartition("20210901");
        for (MyTable myTable : myTables) {
            System.out.println(myTable.getCol1() + "," + myTable.getCol2() + "," + myTable.getDt());
        }
    }
}

在测试代码中,insertPartition方法插入一条数据,queryPartition方法查询指定分区的数据,并打印出来。

 

你可能感兴趣的:(hive,hadoop,数据仓库)