Hive的搭建和命令行

搭建

  1.安装MySql,参照上篇博文

  2.修改hive的配置文件

    a.{HIVE_HOME/conf}目录下,复制hive-env.sh.template

      $>cp hive-env.sh.template hive-env.sh
      $>gedit hive-env.sh
      添加:【 export HADOOP_HOME=/home/hyxy/soft/hadoop 】
    b.{HIVE_HOME/conf}目录下,复制hive-default.xml.sh.template
      $>cp hive-default.xml.template hive-site.xml
      $>gedit hive-site.xml
      <?xml version="1.0" encoding="UTF-8" standalone="no"?>
      <?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
      <configuration>
        <property>
          <name>hive.exec.scratchdir</name>
          <value>/tmp/hive</value>
        </property>

        <property>
          <name>hive.exec.local.scratchdir</name>
          <value>/home/hyxy/tmp/hive</value>
        </property>

        <property>
          <name>javax.jdo.option.ConnectionUserName</name>
          <value>hive</value>
        </property>
        <property>
          <name>javax.jdo.option.ConnectionPassword</name>
          <value>123456</value>
        </property>

        <property>
          <name>javax.jdo.option.ConnectionURL</name>
          <value>jdbc:mysql://master:3

306/hive?createDatabaseIfNotExist=true</value>
        </property>

        <property>
          <name>javax.jdo.option.ConnectionDriverName</name>
          <value>com.mysql.jdbc.Driver</value>
        </property>

        <property>
          <name>hive.metastore.warehouse.dir</name>
          <value>/user/hive/warehouse</value>
        </property>
      </configuration>
    c.将MySQL的连接驱动包拷贝至{HIVE_HOME/lib}下
      $>cd /home/hyxy/soft/hive/lib
      $>cp /mnt/hgfs/2.安装环境/download/mysql-connector-java-5.1.10.jar .
    d.开启hadoop集群;
    e.执行hive命令
      $>hive

命令行

  

1.定义变量:
  $>hive -d name=hyxy; //进入到客户端,并定义了变量为name,值为hyxy;作用域在hivevar下
  查看设置后的属性值:
    hive> set name;
    hive> set hivevar:name;
  查看系统默认的所有属性项:
    hive> set name=zhangyuejiu;
    hive> set -v;
  在shell命令行中,可以使用${}方式引用变量
    hive> create table ${name}(id int,${name} string);
    hive> show tables;
      tt
      zhangyuejiu
    hive> describe zhangyuejiu;
      id int
      zhangyuejiu string
  应用:显示当前所在的database:
    hive> set hive.cli.print.current.db;
    hive.cli.print.current.db=false
    hive> set hive.cli.print.current.db=true;
    hive (default)> show databases;
2.hive中“一次使用”命令
  $> hive -e "select * from tt";
  $> hive -S -e "select * from tt";
3.hive执行sql文件
  要求:一般情况下,将文件定义为*.q或*.hql;
    $>echo 'select * from tt' >> ~/text.hql
    $>hive -f ~/text.hql;
    hive> source ~/text.hql;
4.设置默认加载初始化文件:
  $>cd ~
  $>gedit .hiverc
  set hive.cli.print.current.db=true;
  set name=hyxy;
  $>hive;
  测试成功!
5.hive CLI自动补全。
6.默认情况下,在/home/hyxy目录下,可以查看.hivehistory文件,显示历史SQL的操作;
7.在hive Cli可以使用shell命令
  hive>!clear;
8.在hive Cli中可以使用hadoop的dfs命令。
  hive>dfs -lsr /user;
9.查找表时,显示字段名称;
  hive>set hive.cli.print.header=true;

原文地址:https://www.cnblogs.com/lyr999736/p/9468105.html