将虚拟机本地csv文件导入hive

1、首先,先将windows系统下的csv文件上传到虚拟机目录中。我的是在/my_dbdata/下
在这里插入图片描述
2、启动hive,我提前创建了一个名为toutiao的hive业务数据库。创建时使用的命令:

create database if not exists toutiao comment "user,news information of 136 mysql" location '/user/hive/warehouse/toutiao.db/';

之后再数据库中创建table

hive (default)> use toutiao;

开始使用的和mysql中同样的语句(当然一定会报错了,,但我也贴出错误的代码了)

create table user_profile(userid VARCHAR(100) NOT NULL,mobile_type VARCHAR(30),mobile_system VARCHAR(30),start_telecom VARCHAR(20) character set utf8,network_type VARCHAR(20) character set utf8,start_address VARCHAR(120) character set utf8);//错误,不可取)

以下正确:

create table user_profile(userid VARCHAR(100),mobile_type VARCHAR(30),mobile_system VARCHAR(30),start_telecom VARCHAR(20),network_type VARCHAR(20),start_address VARCHAR(120)) row format delimited fields terminated by ',';

其中在这里插入图片描述
红框标出的语句是为表指定格式,为了保证导入数据时不出错。其中’,'时自己设定的,因为我的时csv文件,默认分隔符是“逗号”,所以选择的逗号。这个可以根据自己的实际情况来。如果忘了设定格式,,可以将表删掉重新建立。。drop table user_profile;
之后,就可以导入数据了

`hive (toutiao)> load data local inpath '/my_dbdata/db_user_info.csv' overwrite into table user_profile;`

之后又创建了另一个表,表中出现了新的数据类型,时间和文本。

Hive 中,可以用String、Date和Timestamp表示日期时间,String 用 yyyy-MM-dd 的形式表示,Date 用yyyy-MM-dd 的形式表示,Timestamp 用 yyyy-MM-dd hh:mm:ss 的形式表示

我使用的Timestamp,文本使用string。

hive (toutiao)> create table news_article_content(newsid VARCHAR(100) ,title string,content string,create_time Timestamp,resource string,content_type string) row format delimited fields terminated by ',';

之后也可以往这个table中导数据了

hive (toutiao)> load data local inpath '/my_dbdata/db_news_info.csv' overwrite into table news_article_content;

在这里插入图片描述
可见成功导入。
创建第三个表:

hive (toutiao)> create table u_news_interaction(userid varchar(100),newsid VARCHAR(100) ,event_type varchar(10),click_time Timestamp) row format delimited fields terminated by ',';

继续开心导数据:

hive (toutiao)> load data local inpath '/my_dbdata/u_news_interaction.csv' overwrite into table u_news_interaction;

撒花

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值