首页 > 解决方案 > 无法运行 HIVE 查询

问题描述

table在 Cloudera VM 中使用 HIVE 查询创建了一个,下面是我DDL创建名为incremental_tweets.

CREATE EXTERNAL TABLE incremental_tweets (
id BIGINT,
created_at STRING,
source STRING,
favorited BOOLEAN,
retweet_count INT,
retweeted_status STRUCT<
  text:STRING,
  user:STRUCT<screen_name:STRING,name:STRING>>,
entities STRUCT<
  urls:ARRAY<STRUCT<expanded_url:STRING>>,
  user_mentions:ARRAY<STRUCT<screen_name:STRING,name:STRING>>,
  hashtags:ARRAY<STRUCT<text:STRING>>>,
text STRING,
user STRUCT<
  screen_name:STRING,
  name:STRING,
  friends_count:INT,
  followers_count:INT,
  statuses_count:INT,
  verified:BOOLEAN,
  utc_offset:INT,
  time_zone:STRING>,
in_reply_to_screen_name STRING
)
ROW FORMAT SERDE 'com.cloudera.hive.serde.JSONSerDe'
LOCATION '/twitteranalytics/incremental/';

在成功创建表上执行此操作后HUE HIVE Editor,现在的问题是我无法执行SELECT引发以下错误的语句。

SELECT Statement

 Select id, entities.user_mentions.name FROM incremental_tweets;

ERROR

  Error while processing statement: FAILED: Execution Error, return code 2 
  from org.apache.hadoop.hive.ql.exec.mr.MapRedTask

此外,由于HUE editor提供了自动完成功能,下面是它给出的语句和错误。

Statement

Select id, entities.`,user_mentions`.name FROM incremental_tweets;

ERROR

Error while compiling statement: FAILED: RuntimeException cannot find field 
,user_mentions(lowercase form: ,user_mentions) in [urls, user_mentions, 
hashtags]   

什么是正确的SELECT statement?我错过了任何语法吗?

标签: arraysstructhivehiveqlcloudera-quickstart-vm

解决方案


user_mentions是一个结构数组。您只能通过指定数组索引来处理内部结构元素:

entities.user_mentions[0].name --get name from first array element

或者如果要选择所有数组元素,请使用explode()+ :lateral view

select id, user_mention.name
from incremental_tweets 
     lateral view outer explode(entities.user_mentions) s as user_mention
;

推荐阅读