hive使用load加载数据1.0

Hive数据操作指南
本文详细介绍了如何使用Hive进行表的创建、数据加载及修改等操作,并提供了具体实例,帮助读者掌握Hive的基本使用技巧。
安装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'隔开
[table]
|1202 | Manisha |45000| Proof reader|
|1203 | Masthanvali | 40000 | Technical writer|
|1204 | Kiran |40000 | Hr Admin|
|1205 | Kranthi |30000 | Op Admin|
[/table]
导入后select * from l_employee就会看家你要看到的数据

[b]hive修改表[/b]

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;


[b]添加表的分区[/b]
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:
这是由于我们在创建表的时候没有创建分区。由于在新建表的时候,所以只有在存在分区列的表上执行增加分区的操作,才会成功。

[b]装载数据总结[/b]
上边我们已经从本地装载了数据,我们可以试试其他的相关的命令试试
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加载数据时发现不会对数据格式进行任何的校验,需要用户自己保证数据格式与定义的格式一致。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值