Hive常用命令

hive命令行

  • hive --help

    --config       hive配置文件路径
    --service      hive 服务
    --auxpath      添加jar包(多个jar包用:分割)
    
  • hive --service help

    Service List: beeline cli help hiveburninclient hiveserver2 hiveserver hwi jar lineage metastore metatool orcfiledump rcfilecat schemaTool version
    Parameters parsed:
      --auxpath : Auxillary jars
      --config : Hive configuration directory
      --service : Starts specific service/component. cli is default
    Parameters used:
      HADOOP_HOME or HADOOP_PREFIX : Hadoop install directory
      HIVE_OPT : Hive options
    For help on a particular service:
      ./hive --service serviceName --help
    Debug help:  ./hive --debug --help
    
  • hive --service cli(相当于直接执行hive) 具体服务

    hive --service cli --help (相当于直接执行hive -h)
        -e      直接执行hql语句 
        -v      输出hql语句
        -S      屏蔽执行过程
        -f      执行hql文件
        -i      执行一个文件
        -d      用户自定义变量
        --hivevar   用户自定义变量
    
  • hive cli更多功能

    • 直接执行shell 命令
      hive> ! pwd;
      
    • 直接执行hsdf 命令
      hive> df -ls;
      
    • 显示数据库
      hive> set hive.cli.print.current.db=true;
      
    • 显示字段名
      hive> set hive.cli.print.header=true;
      
  • 启动hive服务

    启动web GUI
    nohup hive --service hwi > /dev/null 2>&1 &		
    http://master:9999/hwi
    
    启动元数据
    nohup  hive --service metastore > /dev/null 2>&1 &		
    
     启动远程服务接口
    nohup hive --service hiveserver2 > /dev/null 2>&1 &    
    
    启动hcatlog
    先关闭matastore
    nohup hcat_server.sh start > /dev/null 2>&1 &
    
    启动webHcat
    webhcat_server.sh start &
    http://192.168.28.161:50111/templeton/v1/status
    
  • beeline使用

    /home/hadoop/soft/spark2/bin/beeline  
    beeline> !connect jdbc:hive://artemis-02:10000 hadoop hadoop
    
    /home/hadoop/soft/spark2/bin/beeline -u jdbc:hive://artemis-02:10000 -n hadoop -p hadoop --silent=true -e "show databases"
    
    
    /home/hadoop/soft/spark2/bin/beeline -u jdbc:hive://artemis-02:10000 -n hadoop -p hadoop --silent=true  --showHeader=false -e "show databases"
    
    
    /home/hadoop/soft/spark2/bin/beeline -u jdbc:hive://artemis-02:10000 -n hadoop -p hadoop --silent=true  --showHeader=false  --outputformat=csv2  -e "select * from lvxw.manage_partition"
    

数据库操作

  • 查看数据库

    show databases;
    show databases like 'lv*'
    set hive.metastore.warehouse.dir;       查看数据库所在hdfs根目录
    describe database lvxw;                 查看数据库信息
    describe database EXTENDED lvxw;        查看数据库详细信息
    
  • 创建数据库

    create database lvxw;
    create database if not exists lvxw;
    create database lvxw LOCATION 'newpath'; 创建数据库,并指定新的HDFS目录
    create database lvxw2 comment 'this is my database named lvxw2';
    create database lvxw with DBPROPERTIES('created-by'='lvxw','carete-date'='2018-09-10');
    
  • 删除数据库

    drop database lvxw;
    drop database if exists lvxw;   
    drop database if exists lvxw CASCADE;   删除数据库和其中的表
    
  • 修改数据库

    alter database lvxw set DBPROPERTIES('edited-by'='lvxw');
    
  • 使用指定数据库

    use lvxw;
    

表操作

  • 创建表(内部表)

    create table if not exists test(
        id INT comment '编号',
        component STRING comment '组件'
    )
    comment '这只一张用于测试的表'
    row format delimited fields
    terminated by ','
    LOCATION 'hdfs://artemis-02:9000/tmp/lvxw/hive'
    TBLPROPERTIES('carete-date'='2018-09-10');
    
  • 创建内部分区表

    create table if not exists manage_partition(
        id INT comment '编号',
        component STRING comment '组件'
    )
    comment '这只一张内部分区表'
    PARTITIONED BY (
      year string,
      month string,
      day string
    )
    row format delimited fields
    terminated by ','
    LOCATION 'hdfs://artemis-02:9000/tmp/lvxw/hive_manage'
    TBLPROPERTIES('carete-date'='2018-09-10');
    
    alter table manage_partition add if not exists partition(year="2018", month="01", day="01") LOCATION "/tmp/lvxw/hive_manage/2018/01/01";
    
  • 查看分区

    show partitions manage_partition
    show partitions manage_partition partition(year=2018,month=01)
    
  • 创建表(拷贝其他表结构,创建新表)

     create table if not exists test2 like test;
    
  • 创建外部表

    create external table if not exists test2(
        id INT comment '编号',
        component STRING comment '组件'
    )
    comment '这只一张用于测试的表'
    row format delimited fields
    terminated by ','
    LOCATION 'hdfs://artemis-02:9000/tmp/lvxw/hive'
    TBLPROPERTIES('carete-date'='2018-09-10');
    
  • 创建外部分区表

    create external table if not exists external_partition(
        id INT comment '编号',
        component STRING comment '组件'
    )
    comment '这只一张外部分区表'
    PARTITIONED BY (
      year string,
      month string,
      day string
    )
    row format delimited fields
    terminated by ','
    LOCATION 'hdfs://artemis-02:9000/tmp/lvxw/hive_external'
    TBLPROPERTIES('carete-date'='2018-09-10');
    
    alter table external_partition add if not exists partition(year="2018", month="01", day="01") LOCATION "/tmp/lvxw/hive_external/2018/01/01";
    
  • 查看表

    show tables;
    show tables in test;
    show tables 'te*'
    show create table test;
    show TBLPROPERTIES test;     查看表的属性信息
    describe test;
    describe extended test;
    describe formatted test;
    describe extended test.id;      具体某一列
    describe formatted test.id;     具体某一列
    
  • 删除表

    drop table if exists test;
    
  • 修改表

    alter table test rename to test_test; 修改表明
    
    alter table external_partition add if not exists partition(year="2018", month="01", day="02") LOCATION "/tmp/lvxw/hive_external/2018/01/02" partition(year="2018", month="01", day="03") LOCATION "/tmp/lvxw/hive_external/2018/01/03"; 增加分区
    
    alter table external_partition drop if exists partition(year="2018", month="01", day="02"); 删除分区
    
    alter table external_partition CHANGE COLUMN id ids INT AFTER component;
    alter table external_partition CHANGE COLUMN ids id INT;
    alter table external_partition CHANGE COLUMN component component STRING AFTER id;   修改列名、位置
    
    alter table external_partition ADD COLUMNS(other STRING); 增加字段
    
    alter table external_partition REPLACE COLUMNS(
        ids INT comment '编号',
        component STRING comment '组件'
    );      修改或删除字段
    
     alter table external_partition SET TBLPROPERTIES('edit-date'='2018-09-10'); 修改、增加表属性
    

线上如何创建分区表

    use temp_tasks;
    CREATE EXTERNAL TABLE `monitor_tip`(
        `ip` string,
        `ad` string,
        `playtime` string,
        `imp_chance_id` string,
        `miaozhen_ip` string,
        `admaster_ip` string
    )
    PARTITIONED BY (
      `year` string,
      `month` string,
      `day` string
      )
    ROW FORMAT SERDE
      'com.bizo.hive.serde.csv.CSVSerde'
    STORED AS INPUTFORMAT
      'org.apache.hadoop.mapred.TextInputFormat'
    OUTPUTFORMAT
      'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
    LOCATION
      'hdfs://hdfsnewcluster/tmp/ad_tmp'
    
    
    
    alter table monitor_tip add if not exists partition(year="2018", month="07", day="09") LOCATION "/tmp/ad_tmp/2018/07/09";

你可能感兴趣的:(bigdata)