hive将元数据存储在metastore上,将数据存储在hdfs上;二者怎么关联呢?
其实就是:我们在hive客户端建立一个table,当该table的路径与hdfs上的存储数据的路径一致时,二者便可以联系起来。
准备基础
1.Hadoop安装,hdfs集群启动
2. hive安装
3. 用jps命令查看各客户端的启动情况;如下:
数据准备
我们想要上传到hdfs上的
数据内容
如下:
先创建一个 testdata.txt
touch testdata.txt
{
"name": "songsong",
"friends": ["bingbing" , "lili"] ,
"children": {
"xiao song": 18 ,
"xiaoxiao song": 19
}
"address": {
"street": "hui long guan",
"city": "beijing"
}
}
元数据创建
1.先验知识:
hive基本数据类型:
hive | java |
---|---|
TINYINT | byte |
SMALINT | short |
INT | int |
BIGINT | long |
BOOLEAN | boolean |
FLOAT | float |
DOUBLE | double |
STRING | string |
TIMESTAMP | – |
BINARY | – |
hive集合数据类型
数据类型 | 描述 |
---|---|
STRUCT | 就类似c语言的结构体 |
MAP | 键值对 key-value,相当于字典:{“grad”: 3} |
ARRAY | 类似数组:[“ss”, “ss”] |
2.创建元数据信息
启动hive
bin/hive
结合testdata内容创建元数据表
在 hive> 里输入如下命令:
create table test(
name string,
friends array<string>,
children map<string, int>,
address struct<street:string, city:string> )
row format delimited fields terminated by ','
collection items terminated by '_'
map keys terminated by ':'
lines terminated by '\n';
命令解释如下:
创建一个叫做
test
的table;
2-5行写上:
表头名称
以及
对应的数据结构
6-9是对数据进行说明:
row format delimited fields terminated by ','
collection items terminated by '_'
map keys terminated by ':'
lines terminated by '\n';
# 用”,“标志每一个row_id数据结束
# 用”_“来连接每一个row_id里面的数据
# 用":" 来映射map结构下的数据键值对
# 用回车符号表示数据填写结束
由于hive-site.xml配置里,在hdfs上的存储目录设置如下:
因此我们在hdfs上可以查看到 /user/hive/warehouse/
test
目录的创建
根据hive描述的那样,在testdata.txt里面的文件应该如下填写,才能被hive识别出来;
songsong,bingbing_lili,xiao song:18_xiaoxiao song:19,hui long
guan_beijing
至此,我们在hive上创建好了 test 的元数据表映射,也准备好了数据testdata.txt;接下来该上传了!
3.数据上传到hdfs:
因为hive创建的数据表table(test)是在下/user/hive/warehouse/
test
下,所以我们把创建的数据 testdata.txt 也上传到该目录,以便hive可以查询到:
hadoop fs -put testdata.txt /user/hive/warehouse/test
刷新hdfs页面查看:
在hive上查询