HIVE 基础(二)

16 篇文章 3 订阅
12 篇文章 2 订阅

目录

Hive数据类型 - 集合数据类型

Hive数据结构

创建静态表

 加载本地文件

加载HDFS文件

加载数据后覆盖原数据

创建分区表

分区表插入数据

查看分区表信息

多字段分区

插入数据

数据表

内部表(管理表)

外部表(External Tables)

创建外部


Hive数据类型 - 集合数据类型

ARRAY:存储的数据为相同类型

MAP:具有相同类型的键值对

STRUCT:封装了一组字段

类型格式定义示例

ARRAY

['Apple','Orange','Mongo']

ARRAY<string>

a[0] = 'Apple'

MAP

{'A':'Apple','O':'Orange'}

MAP<string,string>

b['A'] = 'Apple'

STRUCT

{'Apple',2}

STRUCT<fruit:string,weight:int>

c.weight = 2

Hive数据结构

数据结构描述逻辑关系物理存储(HFDS)

Database

数据库

表的集合

文件夹

Table

行数据的集合

文件夹

Partition

分区

用于分割数据

文件夹

Buckets

分桶

用于分布数据

文件

Row

行记录

文件中的行

Columns

列记录

每行中指定的位置

Views

视图

逻辑概念,可跨越多张表

不存储数据

Index

索引

记录统计数据信息

文件夹

在opt下面创建一个 employee.txt 文件,把数据加到txt文件中

数据内容:

Michael|Montreal,Toronto|Male,30|DB:80|Product:Developer Lead
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

创建静态表

create table if not exists employee(
    name string,
    work_place array<string>,
    gender_age struct<gender:string,age:int>,
    skills_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';

row format delimited   含义是:分隔符设置开始语句

fields terminated by '|'   含义是:设置字段与字段之间的分隔符为“|”
collection items terminated by ','  含义是:设置一个复杂类型(array,struct)字段的各个item之间的分隔符为 “,”
map keys terminated by ':' 含义是:设置一个复杂类型(Map)字段的key value之间的分隔符为 “:”
lines terminated by '\n';  含义是:设置行与行之间的分隔符为 “\n”

 加载本地文件

0: jdbc:hive2://192.168.152.192:10000> load data local inpath '/opt/employee.txt' into table employee;

加载HDFS文件

这步需要先把txt文件上传到hdfs上面

0: jdbc:hive2://192.168.152.192:10000> load data inpath '/employee.txt' into table employee;

加载数据后覆盖原数据

0: jdbc:hive2://192.168.152.192:10000> load data inpath '/employee.txt' overwrite  into table employee;

创建分区表

create table employee2(
    name string,
    work_place array<string>,
    gender_age struct<gender:string,age:int>,
    skills_score map<string,int>,
    depart_title map<string,string>
)
partitioned by (age int)
row format delimited 
fields terminated by '|'
collection items terminated by ','
map keys terminated by ':'
lines terminated by '\n';

partitioned by (age int) 含义是:创建分区 以age分区

分区表插入数据

0: jdbc:hive2://192.168.152.192:10000> load data local inpath '/opt/employee.txt' into table employee2 partition(age=20);


0: jdbc:hive2://192.168.152.192:10000> load data local inpath '/opt/employee.txt' into table employee2 partition(age=30);

查看分区表信息:

show partitions employee2;

多字段分区

create table employee3(
    name string,
    work_place array<string>,
    gender_age struct<gender:string,age:int>,
    skills_score map<string,int>,
    depart_title map<string,string>
)
partitioned by (age int , gender string)
row format delimited 
fields terminated by '|'
collection items terminated by ','
map keys terminated by ':'
lines terminated by '\n';

插入数据

0: jdbc:hive2://192.168.152.192:10000> load data local inpath '/opt/employee.txt' into table employee3 partition(age=20,gender='0');


0: jdbc:hive2://192.168.152.192:10000> load data local inpath '/opt/employee.txt' into table employee3 partition(age=20,gender='1');

数据表

数据表分为内部表和外部表

内部表(管理表)

  • HDFS中为所属数据库目录下的子文件夹
  • 数据完全由Hive管理,删除表(元数据)会删除数据

外部表(External Tables)

  • 数据保存在指定位置的HDFS路径中
  • Hive不完全管理数据,删除表(元数据)不会删除数据

上传数据内容

hdfs dfs -put ./employee.txt /tmp/hivedata/employee/

创建外部

create external table if not exists employee(
    name string,
    work_place array<string>,
    gender_age struct<gender:string,age:int>,
    skills_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
location '/tmp/hivedata/employee';

创建外部表要在create后面加上一个 external    

location '/tmp/hivedata/employee'; 含义是:指定数据存储路径(HDFS)     

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
⼤数据java操作hive ⼤数据 ⼤数据 java操作 操作hive 版权声明: 本⽂为博主学习整理原创⽂章,如有不正之处请多多指教。 未经博主允许不得转载。 虚拟机上操作,保证 虚拟机上操作,保证hive数据库能正常连接进⼊。 数据库能正常连接进⼊。 如因退出没有⽤到命令(quit;),第⼆次进⼊则会报错。解决⽅法,切换到 [root@xcl ~]# cd apache-hive-2.1.1-bin/conf/ [root@xcl conf]# vi hive-site.xml 最后进⾏初始化命令为:schematool -initSchema -dbType mysql 在任意⽬录下输⼊hive,则ok。 创建数据库、在数据库中创建数据表,在本地 创建数据库、在数据库中创建数据表,在本地"造数据 造数据" Create databases text; 例: CREATE TABLE t4(name String ,age int,likes ARRAY<String>,relation MAP<String,String>,location struct<country:String,city:String ,doornum:int>) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' COLLECTION ITEMS TERMINATED BY '^' MAP KEYS TERMINATED BY ':'; "造的数据"已经分好类 # 字段分割符是逗号 # 数组类型分隔符是^号 # map数据类型分割符是冒号 tom,18,lol^篮球^⾳乐,father:tom1^mother:lili,US^LOS^16 ⽤load上传本地数据到表中 load data local inpath '/usr/local/apps/test1.txt' into table t4; eclipse操作步骤(要配好 操作步骤(要配好maven创建 创建maven项⽬) 项⽬) maven项⽬需要的包,在 项⽬需要的包,在"pom.xml"中添加 中添加 <dependency> <groupId>org.apache.hive </groupId> <artifactId>hive-jdbc </artifactId> <version>2.1.1</version> </dependency> java连接 连接hive代码: 代码: //加载驱动 Class.forName("org.apache.hive.jdbc.HiveDriver"); //获取连接 String url="jdbc:hive2://192.168.1.90:10000/test"; Connection connection= DriverManager.getConnection(url,"root",""); //执⾏sql PreparedStatement ps=connection.prepareStatement("SELECT * FROM table01 "); //获取结果 ResultSet rs=ps.executeQuery(); while (rs.next()){ System.out.println("测试数据输出结果是:"+rs.getString(1)); } //关闭连接 rs.close(); ps.close(); connection.close(); 在代码中添加 注意:引的包全是sql包 引完后会报异常,抛出异常即可 如出现这个问题 解决⽅法: <dependency> <groupId>jdk.tools</groupId> <artifactId>jdk.tools</artifactId> <version>1.7</version> <scope>system</scope> <systemPath>${JAVA_HOME}/lib/tools.jar</systemPath> </dependency>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值