内表数据加载创建表时加载
create table newtable as select col1,col2 from oldtable
hive> create table testNew as select name,addr from testtable;hive> select * from testNew;OKliguodongcdaobama lsjliguodongcdaobama lsj创建表时指定数据位置
create table tablename location ‘/**/**’
testNew2(name string comment ‘name value’,addr string comment ‘addr value’)row format delimited fields terminated by ‘\t’ lines terminated by ‘\n’ stored as textfilelocation ‘/liguodong/hivedata’;
本地数据加载
load data local inpath ‘localpath’ [overwrite] into table tablename
追加hive> load data local inpath ‘/liguodong/hivedata/datatest’ into table testNew2;覆盖load data local inpath ‘/liguodong/hivedata/datatest’ overwrite into table testNew2;
加载hdfs数据
load data inpath ‘hdfspath’ [overwrite] into table tablename 注:这个操作是移动数据,而不是复制数据。
testNew2;load data inpath ‘/liguodong/datatest’ overwrite into table testNew2;
使用Hadoop命令拷贝数据到指定位置(hive的shell中执行和Linux的shell执行)
由查询语句加载数据insert [overwrite | into] table tablenameselect col1,col2 from table where …from tableinsert [overwrite | into ]table tablenameselect col1,col2where …
注意:字段对应不同于一些关系型数据库。
外表数据加载创建表时指定数据位置
create external table tablename() location ”
testExtNew(name string,addr string)row format delimited fields terminated by ‘\t’ lines terminated by ‘\n’ stored as textfilelocation ‘/liguodong/exttable/’;select * from testExtNew;查询插入,同内表使用Hadoop命令拷贝数据到指定位置(hive的shell中执行和Linux的Shell执行),同内部表Hive分区表数据加载
内部分区表和外部分区表数据加载 内部分区表数据加载方式类似于内表 外部分区表数据加载方式类似于外表
注意: 数据存放的路径层次要和表的分区一致; 如果分区表没有新增分区,即使目标路径下己经有数据了,但依然查不到数据。
不同之处 加载数据指定目标表的同时,需要指定分区。
本地数据加载
Load data local inpath ‘localpath’ [overwrite] into table tablename partition(pn=”)
内部表
testPar(name string,addr string)partitioned by (dt string)row format delimited fields terminated by ‘\t’ lines terminated by ‘\n’ stored as textfile;testPar partition(dt=’20150717′);显示分区show partitions testPar;
外部表
testExtPar(name string,addr string)partitioned by (dt string)row format delimited fields terminated by ‘\t’ lines terminated by ‘\n’ stored as textfilelocation ‘/liguodong/exttable/’;select * from testExtPar;没有结果,不满足分区表相应的目录格式。dfs -copyFromLocal /liguodong/dataext /liguodong/exttable/dt=20150717select * from testExtPar;满足分区表相应的目录格式,仍然没有结果,因为,查不到分区相关信息。//修改表,添加相应的分区partitions testExtPar;select * from testExtPar;
加载hdfs数据 Load data inpath ‘hdfspath’ [overwrite] into table tablename partition(pn=”)
由查询语句加载数据
insert [overwrite] into table tablename partition(pn=”)…Hive数据加载注意问题
分隔符问题:分隔符默认只有单个字符。如果有多个字符,默认取第一个字符作为分隔符。 数据类型对应问题: Load数据数据,字段类型不能互相转化时,查询结果返回NULL。而实际的数据仍然存在。 Select查询插入,字段类型不能互相转化时,插入数据为NULL。而实际的数据也为NULL。 其他: Select查询插入数据,字段值顺序要与表中字段顺序一致,名称可不一致。 Hive在数据加载时不做检查,查询时检查。 外部分区表需要添加分区才能看到数据。
没有口水与汗水,就没有成功的泪水。