使用datagrip连接hive
-- 切换数据库
use testdb;
create database testdb;
-- 创建表
create table t_user(
id int,
name varchar(100),
age int
);
create table t_user3(
id int,
name varchar(100),
age int
)
row format delimited fields terminated by ','
STORED AS TEXTFILE;
-- 创建表指定分区key
create table testdb.mytable1( id int, name varchar(100), age int ) partitioned by (partition_key int) ;
-- 创建user_info表
CREATE TABLE `test.user_info`(
`id` bigint,
`user_uuid` string,
`user_name` string,
`user_cardid` bigint,
`user_phone` bigint,
`user_age` bigint,
`user_sex` string,
`user_address` string,
`user_birthday` timestamp,
`salary` double,
`user_occ` string,
`time` string,
`user_description` string) ;
-- 插入数据
可以通过insert和updatesql语句来操作,不过执行效率很慢,推荐通过文件的方式来写入数据
-- 这里需要结合hdfs来操作
-- 新建文本文件
[hdfs@master hive-server2]$ vim ~/user.txt
1,zhangsan,18
2,lisi,19
3,wangwu,17
4,zhaoliu,30
-- 将文件放到库中
hadoop fs -put ~/user.txt /warehouse/tablespace/managed/hive/testdb.db/t_user3
-- 查看建表语句,可以通过这个语句看到存储文件地址
show create table t_user;
-- 'hdfs://master:8020/warehouse/tablespace/managed/hive/testdb.db/t_user'
--删除表
drop table t_user;
truncate table testdb.mytable;
--查询表
select * from t_user3;
-- 查询数据条数
SELECT COUNT(*) FROM test.user_info;
-- 显示创建的表
show tables;
-- 查看表的字段信息
DESCRIBE testdb.my_table;
问题处理
java.io.IOException: java.lang.RuntimeException: ORC split generation failed with exception: org.apache.orc.FileFormatException: Malformed ORC file hdfs://master:8020/warehouse/tablespace/managed/hive/testdb.db/t_user2/user.txt. Invalid postscript. org.apache.orc.FileFormatException:Malformed ORC file hdfs://master:8020/warehouse/tablespace/managed/hive/testdb.db/t_user2/user.txt. Invalid postscript.
这种报错是上传的是txt文件,而表默认使用orc的方式读取文件,导致读取失败,这里可以通过建表语句指定为txt格式
STORED AS TEXTFILE;
也可以使用标准orc文件来解决这种问题
-- insert overwrite 语法
create temporary table t4 ( id int, name string, age int );
insert into t4(id,name,age) values(3,'aaa', 18);
insert overwrite table t4 select * from t2;