如何在配置单元

时间:2016-05-17 17:20:29

标签: hive hive-udf

第1步: 我写了一个UDF,它将构成2个或更多的Struct列,如汽车,自行车,公共汽车。此外,UDF从其他视图中获取了一些名为' details'。

的信息
cars struct form is: ARRAY<STRUCT<name:string, mfg:string, year:int>>
bikes struct form is: ARRAY<STRUCT<name: string, mfg:string, year: int, price: double>>
buses struct form is: ARRAY<STRUCT<name: string, mfg:string, year: int, price: double>>

我正在创建一个视图&#39; vehicle&#39;使用此UDF如下

ADD JAR s3://test/StructFV-0.1.jar;
CREATE TEMPORARY FUNCTION TEST_STRUCT AS "com.test.TestStruct";

CREATE DATABASE IF NOT EXISTS ranjith;
USE ranjith;
DROP VIEW IF EXISTS vehicles;
CREATE VIEW vehicles AS 
SELECT t.cars, t.bikes, t.buses
FROM details d LATERAL VIEW TEST_STRUCT(d.data) t AS
cars, bikes, buses;

第2步: 我想将每个struct列分解为另一个视图。 当我尝试下面的查询时,我得到的错误就像&#34; AS子句中提供的别名数量与UDTF预期输出的列数不匹配&#34;

USE ranjith;
DROP VIEW IF EXISTS cars;
CREATE VIEW cars AS 
SELECT c.name as name, c.mfg as mfg, c.year as year 
FROM vehicles v LATERAL VIEW EXPLODE (v.cars) exploded_table as c;

注意:如果我有只有汽车结构的UDF,工作正常。仅当UDF包含多个STRUCT时才面临问题。

任何帮助?

3 个答案:

答案 0 :(得分:0)

下面部分不完整的答案,因为您没有包含足够的信息,例如UDF名称,或者您要查询的表格的结构/名称,但它应该足以让您前进。

您可以创建一个使用EXPLODE的视图:

CREATE VIEW productview as 
  SELECT EXPLODE( myudf(.. ).product ) 
      FROM mytable

EXPLODE将获取一个数组并将其“爆炸”到表中。 EXPLODE实际上是一个UDTF,用户定义表生成函数。见https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF#LanguageManualUDF-explode

答案 1 :(得分:0)

USE ranjith;
DROP VIEW IF EXISTS cars;
CREATE VIEW cars AS 
SELECT c.name as name, c.mfg as mfg, c.year, b.name AS bike_name, bb.name AS             bus_name 
FROM vehicles v LATERAL VIEW EXPLODE (v.cars) exploded_table as c LATERAL VIEW     EXPLODE (v.bikes) exploded_table2 AS b LATERAL VIEW EXPLODE (v.buses) exploded_table3 AS bb;

答案 2 :(得分:0)

问题在于您的观点

CREATE VIEW vehicles AS 
SELECT t.cars, t.bikes, t.buses
FROM details d LATERAL VIEW TEST_STRUCT(d.data) t AS
cars, bikes, buses;

您的数据类型是STRUCT的ARRAY,因此您的LATERAL VIEW为 对于返回的STRUCT列,TEST_STRUCT(d.data)只需要一个别名。

e.g。

CREATE VIEW vehicles AS 
SELECT columnAlias.cars, columnAlias.bikes, columnAlias.buses
FROM details d LATERAL VIEW TEST_STRUCT(d.data) tableAlias AS columnAlias;