博客
关于我
【Hive】---- Hive 数据类型
阅读量:336 次
发布时间:2019-03-04

本文共 1573 字,大约阅读时间需要 5 分钟。

一、基本数据类型

Hive中的基本数据类型类似于数据库中的基本数据类型,支持整数、浮点数、字符串等多种类型。以下是几种常用的基本数据类型的特点:

  • Hive的String类型与数据库的varchar类似,支持存储可变长度的字符串,理论上最多可存储2GB字符。
  • Integer类型用于存储整数值,支持自动类型转换,但需注意转换规则。
  • Float和Double用于存储浮点数值,支持隐式转换,但精度需注意。
  • Boolean类型用于存储布尔值,值为true或false。

二、集合数据类型

Hive支持三种复杂数据类型:ARRAY、MAP和STRUCT。这些数据类型允许数据的嵌套和分层,适用于存储结构化数据:

  • ARRAY类似于Java中的Array,用于存储一维以上的数组数据。
  • MAP类似于Java中的HashMap,用于存储键值对数据。
  • STRUCT类似于C语言中的结构体,用于存储具有命名字段的数据组合。

案例精讲

以下是一个复杂数据结构的示例:

{    "name": "songsong",    "friends": ["bingbing", "lili"],    "children": {      "xiao song": 18,      "xiaoxiao song": 19    },    "address": {      "street": "hui long guan",      "city": "beijing"    }  }

在Hive中访问上述数据结构的方式如下:

{    "name": "songsong",    "friends": ["bingbing", "lili"],    "children": {      "xiao song": 18,      "xiaoxiao song": 19    },    "address": {      "street": "hui long guan",      "city": "beijing"    }  }

创建表格和导入数据

创建对应表格的SQL语句如下:

create table test(    name string,    friends array,    children map,    address struct  ) row format delimited fields terminated by ','    collection items terminated by '_'    map keys terminated by ':'    lines terminated by '\n';

导入数据的命令为:

load data local inpath ‘/opt/module/datas/test.txt’ into table test;

数据访问示例

访问集合类型数据的方式如下:

select friends[1], children['xiao song'], address.city from test where name="songsong";

返回结果为:

lili  18  beijing

三、类型转换

Hive支持隐式和显式类型转换,以下是类型转换的规则:

  • 隐式类型转换:
    • 整数类型间可以相互转换,例如TINYINT到INT会自动转换,但INT到TINYINT不会。
    • 所有整数类型、浮点数和字符串类型都可以转换为DOUBLE。
    • TINYINT、SMALLINT、INT可以转换为FLOAT。
    • BOOLEAN类型无法转换为其他类型。
  • 显式类型转换可通过CAST函数实现,例如:
    • CAST('1' AS INT)
    • CAST('X' AS INT) 返回NULL

转载地址:http://lzeq.baihongyu.com/

你可能感兴趣的文章
NMAP网络扫描工具的安装与使用
查看>>
NMF(非负矩阵分解)
查看>>
nmon_x86_64_centos7工具如何使用
查看>>
NN&DL4.1 Deep L-layer neural network简介
查看>>
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.7 Parameters vs Hyperparameters
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named cv2
查看>>