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

你可能感兴趣的文章
npm install CERT_HAS_EXPIRED解决方法
查看>>
npm install digital envelope routines::unsupported解决方法
查看>>
npm install 卡着不动的解决方法
查看>>
npm install 报错 EEXIST File exists 的解决方法
查看>>
npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
查看>>
npm install 报错 Failed to connect to github.com port 443 的解决方法
查看>>
npm install 报错 fatal: unable to connect to github.com 的解决方法
查看>>
npm install 报错 no such file or directory 的解决方法
查看>>
npm install 权限问题
查看>>
npm install报错,证书验证失败unable to get local issuer certificate
查看>>
npm install无法生成node_modules的解决方法
查看>>
npm install的--save和--save-dev使用说明
查看>>
npm node pm2相关问题
查看>>
npm run build 失败Compiler server unexpectedly exited with code: null and signal: SIGBUS
查看>>
npm run build报Cannot find module错误的解决方法
查看>>
npm run build部署到云服务器中的Nginx(图文配置)
查看>>
npm run dev 和npm dev、npm run start和npm start、npm run serve和npm serve等的区别
查看>>
npm run dev 报错PS ‘vite‘ 不是内部或外部命令,也不是可运行的程序或批处理文件。
查看>>
npm scripts 使用指南
查看>>
npm should be run outside of the node repl, in your normal shell
查看>>