首页 技术 正文
技术 2022年11月9日
0 收藏 741 点赞 4,017 浏览 2031 个字

0. 说明

  Hive 插入数据的方法 && Hive 插入数据的顺序 && 插入复杂数据的方法 && load 命令详解


1. Hive 插入数据的方法

  Hive 插入数据不是使用 insert,而是 load


2. Hive 插入数据的顺序

  2.1 先定义好表结构

create table employee(name string,
work_place array<string>,
sex_age struct<sex:string, age:int>,
score map<string, int>,
depart_title map<string, string>)
row format delimited
fields terminated by '|'
collection items terminated by ','
map keys terminated by ':'
lines terminated by '\n'
stored as textfile;

  2.2 准备数据。数据格式要和表结构对应 employee.txt

Michael|Montreal,Toronto|Male,30|DB:80|Product:Developer
Will|Montreal|Male,35|Perl:85|Product:Lead,Test:Lead
Shelley|New York|Female,27|Python:80|Test:Lead,COE:Architect
Lucy|Vancouver|Female,57|Sales:89,HR:94|Sales:Lead

  2.3 空表中使用 load 命令加载数据

load data local inpath '/home/centos/files/employee.txt' into table employee;

[Hive_4] Hive 插入数据

  2.4 取出所有的成员

# array获取
select name ,work_place[] from employee;
# 结构体获取
select name ,sex_age.sex from employee;
# map成员获取
select name, score['Python'] from employee;

3. 插入复杂类型数据 insert

  3.0 设置显示表头

  临时修改命令如下,永久修改需要修改配置文件 hive-site.xml

set hive.cli.print.header=true;

  3.1 插入复杂类型使用转储

insert xxx select xxx

  通过 select 语句构造出 array 类型

# 通过 select 语句构造出 array 类型
select array('tom','tomas','tomson') ;# 转储 array 类型数据
insert into employee(name,work_place) select 'tom',array('beijing','shanghai','guangzhou');

  通过 select 语句构造出 map 类型

# 通过 select 语句构造出 map 类型
select map('bigdata',100);# 转储 map 类型数据
insert into employee(name,score) select 'tomas', map('bigdata',100);

  通过 select 语句构造出 struct 类型

# 通过 select 语句构造出 struct 类型
select struct('male',10);
select named_struct('sex','male','age',10);# 转储 struct 类型数据
insert into employee(name,sex_age) select 'tomson',named_struct('sex','male','age',10);

4. load命令详解

  4.0 前提:先建表

create table duowan(id int, name string, pass string, mail string, nickname string)
row format delimited
fields terminated by '\t'
lines terminated by '\n'
stored as textfile;

  4.1 使用 load

# load 本地数据,相当于上传或者复制,源文件不变
load data local inpath '/home/centos/files/employee.txt' into table employee;# load hdfs 数据,相当于移动
load data inpath '/duowan_user.txt' into table duowan;# load 本地数据 + 覆盖原始数据
load data local inpath '/home/centos/files/employee.txt' overwrite into table employee;# load hdfs 数据 + 覆盖原始数据
load data inpath '/duowan_user.txt' overwrite into table duowan;

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,074
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,551
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,399
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,811
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,892