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

你可能感兴趣的文章
Orderer节点启动报错解决方案:Not bootstrapping because of 3 existing channels
查看>>
org.apache.axis2.AxisFault: org.apache.axis2.databinding.ADBException: Unexpected subelement profile
查看>>
sql查询中 查询字段数据类型 int 与 String 出现问题
查看>>
org.apache.commons.beanutils.BasicDynaBean cannot be cast to ...
查看>>
org.apache.dubbo.common.serialize.SerializationException: com.alibaba.fastjson2.JSONException: not s
查看>>
sqlserver学习笔记(三)—— 为数据库添加新的用户
查看>>
org.apache.http.conn.HttpHostConnectException: Connection to refused
查看>>
org.apache.ibatis.binding.BindingException: Invalid bound statement错误一例
查看>>
org.apache.ibatis.exceptions.PersistenceException:
查看>>
org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned
查看>>
org.apache.ibatis.type.TypeException: Could not resolve type alias 'xxxx'异常
查看>>
org.apache.poi.hssf.util.Region
查看>>
org.apache.xmlbeans.XmlOptions.setEntityExpansionLimit(I)Lorg/apache/xmlbeans/XmlOptions;
查看>>
org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /
查看>>
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:processDebugManifest'
查看>>
org.hibernate.HibernateException: Unable to get the default Bean Validation factory
查看>>
org.hibernate.ObjectNotFoundException: No row with the given identifier exists:
查看>>
org.springframework.amqp.AmqpConnectException:java.net.ConnectException:Connection timed out:connect
查看>>
org.springframework.beans.factory.BeanDefinitionStoreException
查看>>
org.springframework.boot.context.properties.ConfigurationBeanFactoryMetadata
查看>>