# 启动Hive客户端
cd /opt/apache_hadoop/apache-hive-1.2.1
bin/hive
# 创建数据库
create database [if not exists] dbname;
# 显示数据库列表
show databases;
# 显示数据库表列表
show tables;
# 显示Hive的自带函数
show functions;
查看某函数信息
desc function func_name
例子:desc function sum;
# 删除数据库
drop database [if exists] dbname cascade;
# 选择数据库
use dbname;
# 创建数据库表
create table [if not exists] tbname(id int,name string)
row format delimited fields terminated by '\t';
# 读取数据写入表中
读取本地文件-->将本地文件数据复制到Hive表目录下
load data local inpath '/opt/data/student.txt' into table tbname;
读取HDFS目录下的文件-->将HDFS的文件移动到Hive表目录下
load data inpath '/data/student.txt' into table tbname;
# 查询表数据
select * from tbname
select * from tbname where 条件表达式
# 查询表结构
desc tbname;
desc formatted tbname;
# 删除表,注意:删除表是把表的元数据信息删除,同时HDFS上表目录和目录下的数据一起删除
drop table [if exists] tbname;
# 清空表数据,只删除表内数据,表还在,表的元数据信息不会删除。
truncate table tbname;
# 修改表名
alter table tbname rename to new_tbname;
# 表内添加列
alter table tbname add columns(col,col_type,...);
例子:alter table tbname add columns(addr string, phone int);
# 修改表内列名及类型
alter table tbname change col_name new_col_name new_col_type;
# 替换列,注意:列不能删除,但可以修改或替换,会删除原来的所有列,新增替换的列。
alter table tbname replace columns(col col_type,...)
Hive的Linux Shell参数使用讲解
[root@aibigdata apache-hive-1.2.1]# bin/hive -help
usage: hive
-d,--define <key=value> Variable subsitution to apply to hive
commands. e.g. -d A=B or --define A=B
--database <databasename> Specify the database to use
-e <quoted-query-string> SQL from command line
-f <filename> SQL from files
-H,--help Print help information
--hiveconf <property=value> Use value for given property
--hivevar <key=value> Variable subsitution to apply to hive
commands. e.g. --hivevar A=B
-i <filename> Initialization SQL file
-S,--silent