Hive 编程专题 六: 列引用之表值函数

环境:

Hive: 2.7.7
Oracle SQL Developer
Cloudera JDBC Driver

案例 - 1 : 生成多列



select  explode(deductions) as (type,vol)
from default.employee 


结果:

image

错误:

1 - 环境部署

create table account(accountid int, account array)

在行: 3 上开始执行命令时出错 -
create table account(accountid int, account array)
错误报告 -
[Cloudera][HiveJDBCDriver](500051) ERROR processing query/statement. Error Code: 40000, SQL state: TStatus(statusCode:ERROR_STATUS, infoMessages:[*org.apache.hive.service.cli.HiveSQLException:Error while compiling statement: FAILED: ParseException line 1:49 mismatched input ')' expecting < near 'array' in list type:17:16, org.apache.hive.service.cli.operation.Operation:toSQLException:Operation.java:380, org.apache.hive.service.cli.operation.SQLOperation:prepare:SQLOperation.java:206, org.apache.hive.service.cli.operation.SQLOperation:runInternal:SQLOperation.java:290, org.apache.hive.service.cli.operation.Operation:run:Operation.java:320, org.apache.hive.service.cli.session.HiveSessionImpl:executeStatementInternal:HiveSessionImpl.java:530, org.apache.hive.service.cli.session.HiveSessionImpl:executeStatementAsync:HiveSessionImpl.java:517, org.apache.hive.service.cli.CLIService:executeStatementAsync:CLIService.java:310, org.apache.hive.service.cli.thrift.ThriftCLIService:ExecuteStatement:ThriftCLIService.java:530, org.apache.hive.service.rpc.thrift.TCLIService$Processor$ExecuteStatement:getResult:TCLIService.java:1437, org.apache.hive.service.rpc.thrift.TCLIService$Processor$ExecuteStatement:getResult:TCLIService.java:1422, org.apache.thrift.ProcessFunction:process:ProcessFunction.java:39, org.apache.thrift.TBaseProcessor:process:TBaseProcessor.java:39, org.apache.hive.service.auth.TSetIpAddressProcessor:process:TSetIpAddressProcessor.java:56, org.apache.thrift.server.TThreadPoolServer$WorkerProcess:run:TThreadPoolServer.java:286, java.util.concurrent.ThreadPoolExecutor:runWorker:ThreadPoolExecutor.java:1142, java.util.concurrent.ThreadPoolExecutor$Worker:run:ThreadPoolExecutor.java:617, java.lang.Thread:run:Thread.java:745, *org.apache.hadoop.hive.ql.parse.ParseException:line 1:49 mismatched input ')'
expecting < near 'array' in list type:22:6, org.apache.hadoop.hive.ql.parse.ParseDriver:parse:ParseDriver.java:211, org.apache.hadoop.hive.ql.parse.ParseUtils:parse:ParseUtils.java:77, org.apache.hadoop.hive.ql.parse.ParseUtils:parse:ParseUtils.java:70, org.apache.hadoop.hive.ql.Driver:compile:Driver.java:468, org.apache.hadoop.hive.ql.Driver:compileInternal:Driver.java:1317, org.apache.hadoop.hive.ql.Driver:compileAndRespond:Driver.java:1295, org.apache.hive.service.cli.operation.SQLOperation:prepare:SQLOperation.java:204], sqlState:42000, errorCode:40000, errorMessage:Error while compiling statement: FAILED: ParseException line 1:49 mismatched input ')' expecting < near 'array' in list type), Query: create table account(accountid int, account array).

解决方法:

create table account(accountid int, account array<string>)

array, map, struct 都有自己的定义方法,而他们的调用方法基本都相同:

调用方法:

array('Ali','Tencent')
map('company','ali','city','hangzhou')
struct('ZheJiang','Hangzhou','Ali')

定义数据类型:


create table employee(
    [name] string,
    salary float,
    subordinates array<string>,
    deductions map<string,float>,
    address struct<street:string,city:string,state:string,zip:int> ) ;
    

异常:需要输入绑定

image

最终发现,执行脚本与执行语句,还是有些不一样的地方。

执行语句的时候 :var 是代表一个参数,所以需要输入参数值

2 - 生成测试数据

insert into account(accountid,account) values(2,array('Ali.com','Tencent'))


在行: 6 上开始执行命令时出错 -
insert into account(accountid,account) values(2,array('Ali.com','Tencent'))
错误位于命令行: 6 列: 1
错误报告 -
SQL 错误: [Cloudera][HiveJDBCDriver](500051) ERROR processing query/statement. Error Code: [Cloudera][JSQLEngine](12000) The column "account" could not be found., SQL state: HY000, Query: insert into account(accountid,account) values(2,array('Ali.com','Tencent')).

其实是 array() 不能用在 insert … values… 这种语句中,换成如下便可:

create table employee(
        name string
    ,   salary float  
    ,   subordinates array<string>
)    
    
insert into employee(name,salary,subordinates) 
select 'ali',320,array('ali','acai','ayun') 
from account 
limit 1

完整的 Array, Map, Struct 调用方法如下:

insert into default.employee (name,salary,subordinates,deductions,address)
select 'ali'
    ,320
    ,array('ali','acai','ayun') as sub
    , map('ali',1,'acai',2,'ayun',7) as ded
    ,named_struct('street',"zhejiang",  'city',"hangzhou",  'state',"hubin",  'zip',"201210")
from student 
limit 1

值得注意的地方,struct() 与 named_struct() 的不同:

struct("zhejiang","hangzhou","hubin","201210")

会被编译成这样的数据结构:

struct("col0":"zhejiang","col1":"hangzhou","col2":"hubin","col3":"201210")

但 named_struct 的节点就被定死了:

named_struct('street',"zhejiang",  'city',"hangzhou",  'state',"hubin",  'zip',"201210")

编译过后就是:

struct('street',"zhejiang",  'city',"hangzhou",  'state',"hubin",  'zip',"201210")

而这种结构正符合表中定义的数据类型。

案例 - 2 : 生成多行


select  explode(subordinates) as subs
from default.employee 

image

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dbLenis

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值