大数据(Hive的MetaStore切换及其Hive的语法细节)

 


MetaStore
1. MetaStore在Hive中是非常重要的一个概念,通过MetaStoreHive存储HDFS与表的对应关系,MetaStore通过RDB进行数据的存储.
2. MetaStore默认是通过Derby数据库,进行元数据存储的.
3. 如果用Derby充当默认的MetaStore,Hive只能以一个Client进行访问

Hive中MetaStore的切换

把默认的Derby切换成其他的RDB(MySQL)
  1. 切换的步骤

    1. 安装MySQL数据库
       yum install -y mysql-server
       service mysqld start #启动mysql服务
       chkconfig mysqld on  #自启动
       修改root用户密码
       /usr/bin/mysqladmin -u root password 'new-password'
    2. 打开mysql远端访问权限
       use mysql
       grant all privileges  on *.* to root@'%' identified by "1234456";
       flush privileges;
       delete from user where host != '%'
       
       service mysqld restart
    3. 修改hive的配置 hive-site.xml
       <property>
          <name>javax.jdo.option.ConnectionURL</name>
          <value>jdbc:mysql://hadoop4:3306/metastore?createDatabaseIfNotExist=true</value>    
        </property>
        <property>
               <name>javax.jdo.option.ConnectionDriverName</name>
               <value>com.mysql.jdbc.Driver</value>
        </property>
        <property>
              <name>javax.jdo.option.ConnectionUserName</name>
              <value>root</value>
        </property>
        <property>
              <name>javax.jdo.option.ConnectionPassword</name>
              <value>123456</value>
        </property>
    4. 上传mysql的驱动jar 到 hive_home/lib
    5.测试
        启动hive,建库:hive>  create database if not exists mydb;
                hive> use mydb;
            mydb下建表:
            hive> create table if not exists t_user(
                > id int,
                > name string)row format delimited fields terminated by '	';
            OK
            本地导入数据
            hive> load data local inpath '/root/test' into table t_user;
            查询:
            hive> select * from t_user;
    OK
    1   lhc
    2   aaa
    3   bbb
    4   ccc
    Time taken: 10.678 seconds, Fetched: 4 row(s)
    网页也可查看对应存储路径:http://hadoop1:50070/explorer.html#/user/hive/warehouse/mydb.db
Hive的语法细节
  • Hive相关的配置文件

    hive-default.xml
    ​
    hive-site.xml
    <!--显示数据库名-->
    <property>
          <name>hive.cli.print.current.db</name>
          <value>true</value>
     </property>
     <!--显示查询表的列名-->
     <property>
          <name>hive.cli.print.header</name>
          <value>true</value>
    </property>
    也可以通过shell或者hive命令行修改
    bin/hive --hiveconf hive.cli.print.header false
    ​
    hive>set hive.cli.print.header
    hive>set hive.cli.print.header=true
  • Hive的启动参数

    #启动
    1. bin/hive
    #启动hive时,修改hive的配置项
    2. bin/hive --hiveconf hive.cli.print.header false
    #查看帮助文档
    3. bin/hive -help 查看hive的帮助信息
    #启动hive 同时指定数据库
    4. bin/hive --database lhc_140
    #启动hive 同时执行命令
    5. bin/hive -e 'show databases'
       bin/hive --database lhc_140 -e 'show tables'
       >:覆盖原来文件             >>:追加到源文件
       bin/hive --database lhc_140 -e 'select * from t_user'  >> /root/result
    #启动hive,同时执行命令文件(sql命令放在sql文件里)
    6. bin/hive -f /opt/datas/hive.sql    
       bin/hive --database lhc_140 -f /root/hive.sql   >> /root/result
  • 数据库

  • 导入数据

  • 导出数据

  • SQL语句

原文地址:https://www.cnblogs.com/lhc-hhh/p/10324503.html