欢迎投稿

今日深度:

hive 建表,

hive 建表,


hive建表是学习hive的第一步,建表很容易,但建个适合自己的就不是那么容易了,建表语句也有很大的学问。

1.建表语句:

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 by (col_name)[asc|desc],...] into num_buckets buckets] [row format row_format] [stored as file_format] [localtion hdfs_pathh]

稍微解释一下:

create  table  创建一个指定名字的表,如果相同名字的表已经存在,则抛出异常;用户可以用if not exists 选项来忽略这个异常。external关键字可以让用户创建一个外部表,在建表的同时指定一个指向实际数据的路径(location),hive 创建内部表时,会将数据移动到数据仓库指向的路径;若创建外部表,仅记录数据所在位置,不对数据的位置坐任何改变,在删除表的时候,内部表的元数据和数据会一起被删除,而外部表只删除元数据,不 删除数据,如果文件数据是纯文本,可以使用stored as textfile ,如果数据需要压缩,使用stored as sequence ,有分区的表可以在创建的时候使用partitioned by 语句,一个表可以拥有一个或者多个分区,每一个分区单独存在一个目录下,而且,表和分区都可以对某个列进行clustered by 操作,将若干个列放入一个桶(bucket)中,也可以利用sort by 对数据进行排序,这样可以为特定应用提高性能。

创建普通的表:

create table test_table(id int,name string,no int) row format delimited fields terminated by ',' stored as textfile

//指定了字段的分隔符为逗号,所以load数据的时候,load的文本也要为逗号,否则加载后为null,hive只支持单个字符的分隔符,hive默认的分隔符是 \001

创建带有partition的表:

create table test_part(id int,name string,no int) partitioned by (dt string) row format delimited fields terminated by '\t' stored as textfile;

创建表用\t做分隔符的表,dt为分区字段,

加载如下:

load data local inpath '/home/zhangxin/hive/test_hive.txt' overwrite into table test_part partition(dt='2012-03-05');

//local是本地文件,注意不是你电脑上的文件,是hadoop所在的 本地文件

//如果是在hdfs 里的文件,则不需要local,overwrite into 是覆盖表分区,仅仅是这个分区的数据内容,如果是追加,则不需要overwrite

创建external 表(外部表):

create external table test_external(id int,name string,no int) row format delimited fields terminated by ',' localtion '/home/zhangxin/hive/test_hive.txt';

//用逗号分隔的表,且无分区,location后是外部表数据的存放路径

创建与已知表相同结构的表like:只是复制表的结构,而不是复制表的内容,create table test_like_table like test_bucket;

摘自:http://jingyan.baidu.com/article/a378c96092cf56b328283006.html



www.htsjk.Com true http://www.htsjk.com/hive/39562.html NewsArticle hive 建表, hive建表是学习hive的第一步,建表很容易,但建个适合自己的就不是那么容易了,建表语句也有很大的学问。 1.建表语句: create [external] table [if not exists ] table_name[(col_name d...
相关文章
    暂无相关文章
评论暂时关闭