Hive数据仓库操作

Hive数据库安装的三种方式

内嵌模式安装

内嵌derby数据库:一个会话连接,常用于简单测试

  1. 启动hadoop集群

    1 start-dfs.sh
    2 start-yarn.sh
    3 jps

     

  2. 查找hive压缩包

    1 find / -name *hive*.tar.gz

     

  3. 解压hive压缩包到/opt目录下

    1 tar -zxvf /root/experiment/file/apache-hive-2.1.1-bin.tar.gz -C /opt/
    2 # 查看是否解压成功
    3 ls /opt/
    4 # 重命名apache-hive-2.1.1-bin为hive
    5 mv apache-hive-2.1.1-bin hive
    6 # 查看是否重命名成功
    7 ls /opt/
  4. 在profile中配置hive环境变量

    1 vi /etc/profile
    2 # 在HADOOP环境变量下方添加HIVE环境变量
    3 export HIVE_HOME=/opt/hive
    4 export PATH=$HIVE_HOME/bin:$PATH
    5 # 使profile文件配置生效
    6 source /etc/profile
    7 # 查看所有环境变量,是否有/opt/hive/bin
    8 echo $PATH

     

  5. 初始化Hive元数据

    1 schematool -dbType derby -initSchema
    2 # 查看数据库文件
    3 ls

     

    初始化结束后会生成derby.log 和 metastore_db目录(存储derby数据库,保存元数据),数据存放在hdfs上

  6. 进入Hive Shell

    1 hive

     

  7. Hive Shell操作

     1 # 显示数据库
     2 show databases;
     3 # 显示表
     4 show tables;
     5 # 显示函数
     6 show functions;
     7 # 查看HDFS
     8 dfs -ls -R /;
     9 # 退出Hive Shell
    10 quit;
    11 exit;

     

本地模式安装

  1. 进入Mysql

    1 mysql
    2 # 创建数据库
    3 create database hive;
    4 # 查看是否创建成功
    5 show databases;

     

  2. Mysql授权

     1 grant all privileges on *.* to 'root'@'master' identified by 'root';
     2 grant all privileges on *.* to 'root'@'%' identified by 'root';
     3 # 刷新系统权限相关表
     4 flush privileges;
     5 # 查看权限
     6 show databases;
     7 use mysql;
     8 show tables;
     9 desc user;
    10 select Host,User,Super_priv from user;
    11 # 退出Mysql
    12 quit();

     

  3. 拷贝Hive需要的mysql依赖包mysql-connector-java-5.1.42.jar 至hive/lib目录下

    1 # 查找jar包的位置
    2 find / -name mysql*.jar
    3 # 拷贝jar包
    4 cp /root/experiment/file/mysql-connector-java-5.1.42.jar /opt/hive/lib
    5 # 查看/opt/hive/lib目录是否有
    6 ls /opt/hive/lib

     

  4. 进入hive的conf目录下,配置hive相关配置文件参数

    1 cd /opt/hive/conf
    2 # 查看conf目录下内容
    3 ls

     

    • hive-site.xml

       1 cp hive-default.xml.template hive-site.xml
       2 # 查看是否生成hive-site.xml
       3 ls
       4 # 配置hive-site.xml文件
       5 vi hive-site.xml
       6 # 查找ConnectionURL
       7 :?ConnectionURL
       8 # 显示行号
       9 :set nu
      10 # 删除无关内容
      11 :18,498d
      12 :21,25d
      13 :22,4862d
      14 # 取消显示行号
      15 :set nonu

      配置文件

       1 <configuration>
       2     <property>
       3         <name>javax.jdo.option.ConnectionURL</name>
       4         <value>jdbc:mysql://master:3306/hive</value>
       5     </property>
       6     <property>
       7         <name>javax.jdo.option.ConnectionDriverName</name>
       8         <value>com.mysql.jdbc.Driver</value>
       9     </property>
      10     <property>
      11         <name>javax.jdo.option.ConnectionUserName</name>
      12         <value>root</value>
      13     </property>
      14     <property>
      15         <name>javax.jdo.option.ConnectionPassword</name>
      16         <value>root</value>
      17     </property>
      18 </configuration>

       

  5. 初始化数据库

    1 schematool -dbType mysql -initSchema
    2 hive
    3 show tables;
    4 show functions;

     

  6. 操作Hive

    在Mysql中查看TBLS表来查看Hive是否建表 Hive中创建的表的字段信息会自动存入到MySQL的hive数据库COLUMNS_V2表中

远程模式安装

Hive Shell操作表和数据

  1. 上传数据到集群上

  2. 创建表

     1 create table stu_info(
     2 id int,
     3 name string,
     4 age int) row format delimited
     5 fields terminated by '\\\\t';
     6 # 指定数据库位置,目录会自动建立
     7 create table stu_info(
     8 id int,
     9 name string,
    10 age int) location '/pcc/stu_info';
    11 # 将另一个表的数据给一个新表
    12 create table stu_info row format delimited
    13 fields terminated by ',' AS select * from stu_info;
    14 # 建立分区表
    15 create table stu_info_0 (id int, name string) partitioned by(age int)
    16 row format delimited fields terminated by ',';

     

  3. 导入数据

    1 # 导入集群文件
    2 load data inpath '/pcc/file' overwrite into table stu_info;
    3 # 导入本地文件
    4 load data local inpath '/home/file' overwrite into table stu_info;
    5 # 向分区中导入数据
    6 insert overwrite table stu_info_0 patition(age=22)
    7 select id, name from stu_info where age=22;

     

  4. 导出数据

    1 create table t1 as select * from stu_info where gender = 'male';

     

posted @ 2020-12-17 21:08  小石小石摩西摩西  阅读(135)  评论(0编辑  收藏  举报