博客
关于我
【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/

你可能感兴趣的文章
NOPI读取Excel
查看>>
NoSQL&MongoDB
查看>>
NoSQL介绍
查看>>
NoSQL数据库概述
查看>>
Notadd —— 基于 nest.js 的微服务开发框架
查看>>
NOTE:rfc5766-turn-server
查看>>
Notepad ++ 安装与配置教程(非常详细)从零基础入门到精通,看完这一篇就够了
查看>>
Notepad++在线和离线安装JSON格式化插件
查看>>
notepad++最详情汇总
查看>>
notepad++正则表达式替换字符串详解
查看>>
notepad如何自动对齐_notepad++怎么自动排版
查看>>
Notes on Paul Irish's "Things I learned from the jQuery source" casts
查看>>
Notification 使用详解(很全
查看>>
NotImplementedError: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty()
查看>>
NotImplementedError: Could not run torchvision::nms
查看>>
nova基于ubs机制扩展scheduler-filter
查看>>
Now trying to drop the old temporary tablespace, the session hangs.
查看>>
nowcoder—Beauty of Trees
查看>>
np.arange()和np.linspace()绘制logistic回归图像时得到不同的结果?
查看>>
np.power的使用
查看>>