hive使用load加载数据1.0

安装hive
直接操作hive
create table if not exists L_EMPLOYEE (eid int,name String,salary String,destination String)
COMMENT 'Employee details' 
ROW FORMAT delimited
fields terminated by '\t'
lines terminated by '\n'
stored as textfile;


加载数据到数据表中,
load data local inpath '/data/app/lijianzhen/hive/l_employee.txt' overwrite into table L_EMPLOYEE;
在这里load时候出现FAILED: SemanticException Line 1:23 Invalid path '"/data/app/lijianzhen/hive/l_employee.txt"': No files matching path file:/data/app/lijianzhen/hive/l_employee.txt
这时你用下边的命令看看你是不是可以读的数据
less /data/app/lijianzhen/hive/l_employee.txt

文件中的内容为,这里注意创建表的时候fields terminated by '\t'所以在txt中列注意要用'\t'隔开
1202    Manisha 45000    Proof reader
1203    Masthanvali  40000   Technical writer
1204    Kiran 40000   Hr Admin
1205    Kranthi 30000  Op Admin

导入后select * from l_employee就会看家你要看到的数据

hive修改表
DROP TABLE IF EXISTS l_employee;
alter table l_employee rename l_emlpoyee1;
ALTER TABLE employee CHANGE name ename String;
ALTER TABLE employee CHANGE salary salary Double;


添加表的分区
create table employee (id int, name String, dept String, yoj Stirng)
COMMENT 'Employee details' 
ROW FORMAT delimited
fields terminated by ','
lines terminated by '\n'
partition by 'yoj'
stored as textfile;

加载的数据
id, name, dept, yoj
1, gopal, TP, 2012
2, kiran, HR, 2012
3, kaleel,SC, 2013
4, Prasanth, SC, 2013

这里我们将上边的数据放入文件/tmp/employee/file1.txt
load数据,如果我们没有加
partition by 'yoj'
会出现:ValidationFailureSemanticException table is not partitioned but partition spec exists:
这是由于我们在创建表的时候没有创建分区。由于在新建表的时候,所以只有在存在分区列的表上执行增加分区的操作,才会成功。

装载数据总结
上边我们已经从本地装载了数据,我们可以试试其他的相关的命令试试
load data inpath '/app/hive/aaa' into table employee;

这条命令将HDFS的/app/hive/aaa文件下的所有文件追加到表employee中,如果需要覆盖test已有的记录则需要加上overwrite关键字。
load data inpath '/app/hive/aaa' overwrite into table employee;

如果table是一个分区表,则在hql中必须指定分区。
load data inpath '/app/hive/aaa' overwrite into table employee partition(part="3");

如果加上location,hive会将本地文件复制一份上传到指定目录,如果不加local关键字,hive只是将hdfs上的数据移动到指定的目录。
hive加载数据时发现不会对数据格式进行任何的校验,需要用户自己保证数据格式与定义的格式一致。
















猜你喜欢

转载自janle.iteye.com/blog/2327870