Hive的 改
https://www.cppentry.com/bencandy.php?fid=117&id=194663
修改数据库
用户可以使用ALTER DATABASE命令为某个数据库的dbproperties设置键-值对属性值,来描述这个数据库的属性信息。数据库的其他元数据信息都是不可更改的,包括数据库名和数据库所在的目录位置。
alter database test_6_14 set dbproperties('createtime'='20170830'); #test_6_14是数据库的名称
修改表: 重命名表,ALTER TABLE table_name RENAME TO new_table_name
内部表与外部表的相互转换。
MANAGED_TABLE 是内部表的意思
EXTERNAL_TABLE 是外部表的意思
先查看一下表的结构数据,desc formatted 表名字;
Table Type: MANAGED_TABLE 一看就是内部表
改成外部表:hive (test_6_14)> alter table test set tblproperties('EXTERNAL'='TRUE');
如果想改回内部表的话 hive (test_6_14)> alter table test set tblproperties('EXTERNAL'='TRUE');
注意:('EXTERNAL'='TRUE')和('EXTERNAL'='FALSE')为固定写法,区分大小写!区分大小写!区分大小写!
Hive的 删
删除数据库
> drop database wu666;
OK
Time taken: 0.089 seconds
如果数据库不为空,可以采用cascade命令,强制删除
删除名字为wu666的数据库
drop database wu666 cascade ;
删除表 drop table 表名;
Hive的 增
在表中增加一列:alter table dept_partition add columns(deptdesc string);
修改字段名字(更新列): alter table dept_partition change column deptdesc id int;
这里指将deptdesc字段改为id字段 id字段为int类型。 alter table 表名 change column 原字段 新字段 字段类型;
替换列:alter table dept_partition replace columns (deptno string,dname string,loc string);
这个更新列是只将整个表的字段进行修改,你()里面写的多少表的字段就是多少,不是对某一字段进行修改,是对整个表的字段进行修改。
创建表
create [external] table [if not exists] table_name
[(col_name data_type [COMMENT col_comment], ...)]
[comment table_comment]
[partitioned by (col_name data_type [comment col_comment], ...)]
[clustered by (col_name, col_name, ...)
[sorted bt (col_name [ASC|DESC], ...)] into num_buckets buckets]
[row format row_format]
[stored as file_format]
[location hdfs_path]
字段解释说明:
CREATE TABLE 创建表语句。
external 可选语句,是用来创建一个外部表,在建表的同时指定一个指向实际数据的路径(location)
comment 为表和列添加注释。
partitioned by 创建分区表
clustered by 创建分桶表
row format delimited [fields terminated by '(字符)'] 指的是每行用什么字符作为分隔符。
stored as 指定存储文件类型
常用的存储文件类型:SEQUENCEFILE(二进制序列文件)、TEXTFILE(文本)、RCFILE(列式存储格式文件)
如果文件数据是纯文本,可以使用STORED AS TEXTFILE。如果数据需要压缩,使用 STORED AS SEQUENCEFILE。
location :指定表在HDFS上的存储位置。
like允许用户复制现有的表结构,但是不复制数据。
创建一个外部表的例子:
hive (test_6_14)> create external table if not exists dept(
> deptno int,
> dname string,
> loc int)
> row format delimited fields terminated by '\t';
hive (test_6_14)> create external table if not exists emp(
> empon int,
> ename string,
> job string,
> mgr int,
> hiredate string,
> sal double,
> comm double,
> deptno int)
> row format delimited fields terminated by'\t';
创建分区表
创建一个一级的分区表
> create table dept_partition(
> deptno int,dname string,loc string)
> partitioned by (month string)
> row format delimited fields terminated by '\t';
这个一级的分区表用 一个名字为month 为string类型的作为添加数据分区的依据。
在分区表中导入数据:
hive (test_6_14)> load data local inpath '/opt/storage/dept.txt' into table de
pt_partition partition(month='1');
这样添加进去的内容在HDFS路径显示中的表文件夹内还会有一个分组的文件夹名字叫 month=1
查询分区表的内容:用where子句来进行筛选。例如:hive (test_6_14)> select * from dept_partition where month='1';
Hive的查
desc formatted 表名: 查看该表的结构化数据,但并不列出表中的数据
导入数据
从本地导入到hive
hive (test_6_14)> load data local inpath '/opt/storage/dept.txt' into table dept;
代码不加 local就是从HDFS中的路径导入到hive中去。
===================================================