当前位置: 首页 > 工具软件 > BStruct > 使用案例 >

hive 如何将数组转成字符串_Hive Struct to String转换(Hive Struct to String conversion)

萧无尘
2023-12-01

Hive Struct to String转换(Hive Struct to String conversion)

我有一个包含结构的表 - 让我们说:

create external table table1 (

a int,

b STRUCT ,

e string

)

我在这张桌子上执行选择,得到类似的东西 -

1100 {“c”:12.3,“d”:45.6} str

但是当我将这些数据插入另一个表格时 -

create external table table2 (

a string,

b string,

c string

)

insert overwrite table table2

select a,b,c

from table1;

我得到以下奇怪的行为,表明在hive中struct和string之间的转换不能按预期工作

select * from table2;

会导致 -

1100 12.345.6 str

结果是结构中的一种奇怪的值连接,甚至在处理更复杂的结构时也会发生奇怪的事情

有没有办法阻止这种自动转换? 在这种情况下让hive抛出错误?

有没有一种干净的方法来改变这种自动转换以不同的方式工作?

I have a table that has a struct in it - let's say:

create external table table1 (

a int,

b STRUCT ,

e string

)

I perform select on this table and get something like -

1100 {"c":12.3,"d":45.6} str

But then when I insert this data to another table -

create external table table2 (

a string,

b string,

c string

)

insert overwrite table table2

select a,b,c

from table1;

I get the following strange behavior indicating that the conversion between struct and string in hive doesn't work as expected

select * from table2;

would result -

1100 12.345.6 str

the result is a kind of strange concatenation of values in the struct and even stranger things happen when dealing with more complex structs

Is there a way to prevent this auto conversion? Make hive throw an error in such cases?

Is there a clean way to change this auto conversion to work differently?

原文:https://stackoverflow.com/questions/40395868

更新时间:2020-03-05 15:03

最满意答案

我们无法阻止自动转换时直接insert overwrite table table2 select a,b,c from table1; 调用。 背后发生的事情是它只是从struct所有值。

更快捷的方式:

如果你的目的是从struct获取价值并将其存储为原始值,那么尝试如下,

create external table table2 (

a string,

b_c string,

b_d string,

c string

)

insert overwrite table table2

select a,b.c,b.e,c

from table1;

如果这有帮助,请告诉我。

We cannot prevent the auto conversion when directly insert overwrite table table2 select a,b,c from table1; called. Whats happening behind is it just concat all the values from struct.

Quicker way:

if your intention is to get value out of struct and store it as a primitive value then try like below,

create external table table2 (

a string,

b_c string,

b_d string,

c string

)

insert overwrite table table2

select a,b.c,b.e,c

from table1;

Let me know if this helps.

2016-11-03

相关问答

你只需要爆炸一次(连同横向视图)。 在爆炸之后,你可以使用一个新的列(在我的例子中称为prod_and_ts),它将是结构类型的。 然后,您可以解析此新结构列的product_id和timestamps成员以检索所需的结果。 SELECT

user_id,

prod_and_ts.product_id as product_id,

prod_and_ts.timestamps as timestamps

FROM

SampleTable

LATERAL VIEW

...

找到我的问题的一个解决方案: select

seq,

split(split(results,",")[0],':')[1] as offerId,

split(split(results,",")[1],':')[1] as businessName,

split(split(results,",")[2],':')[1] as businessGroup,

split(split(resul

...

select * from hbweather where key.usaf=400010 and key.wban=99999 and key.`date`='199906280000' limit 10;

select * from hbweather where key.usaf=400010 and key.wban=99999 and key.`date`='199906280000' limit 10;

我找到了一个解决方案: 使用Hive爆炸UDTF来爆炸结构数组,即为数组“features”中的每个结构创建第二个(临时)表,并为其创建第二个(临时)表。 CREATE TABLE tbl_exploded as

select main_id,

f.name as f_name,

f.value as f_value

from tbl

LATERAL VIEW explode(features) exploded_table as f

-- optionally filter here inst

...

select t.name

,count (e.col.n_nationkey) as count

,sum (e.col.n_nationkey) as sum

,count (distinct e.col.n_nationkey) as distinct_val

from temp t lateral view explode

...

map keys terminated by ',' create external table temp

(

regionkey smallint

,name string

,comment string

,nations array>

)

row format delimited

fields terminat

...

我终于找到了答案。 看来这是用于序列化/反序列化JSON的JAR的问题。 默认的(Apache)无法对我拥有的数据执行良好的工作。 我尝试了所有这些典型的JAR(在括号中,'ROW FORMAT SERDE'的类): hive-json-serde-0.2.jar(org.apache.hadoop.hive.contrib.serde2.JsonSerde) hive-serdes-1.0-SNAPSHOT.jar(com.cloudera.hive.serde.JSONSerDe) hive-

...

您忘记了deviceID:调用DeviceData.init(deviceID:type:attributes:)命名参数,并且您还忘记了Meta.init(currentPage:nextPage:deviceID)的currentPage和nextPage命名参数Meta.init(currentPage:nextPage:deviceID) 。 这是一个编译的示例: var exampleData = Device(

data: DeviceData(

deviceI

...

在Hive中,Map列的键必须是基元(即不是Struct)。 https://cwiki.apache.org/confluence/display/Hive/LanguageManual+Types#LanguageManualTypes-ComplexTypes 我强烈建议你不要将关键字作为结构。 在您的示例中,如果我不知道开始或结束,如何访问Map的值? 用户需要知道确切的开始和结束,并对表中的每一行进行更改吗? In Hive the key for a Map column must b

...

我们无法阻止自动转换时直接insert overwrite table table2 select a,b,c from table1; 调用。 背后发生的事情是它只是从struct所有值。 您可以编写通用UDF来使用struct ref: http : //www.dataiku.com/blog/2013/05/01/a-complete-guide-to-writing-hive-udf.html 更快捷的方式: 如果你的目的是从struct获取价值并将其存储为原始值,那么尝试如下, cre

...

 类似资料: