【问题标题】:Error Creating External Table in Hive to save JSON data在 Hive 中创建外部表以保存 JSON 数据时出错
【发布时间】:2017-02-11 04:58:30
【问题描述】:

我正在尝试在 Hive 中创建一个外部表,用于获取 json 数据作为 Web 服务的响应。

下面给出了我创建外部表架构的json文件数据:

{
    "location": {
        "name": "Paris",
        "region": "Ile-de-France",
        "country": "France",
        "lat": 48.87,
        "lon": 2.33,
        "tz_id": "Europe/Paris",
        "localtime_epoch": 1486792043,
        "localtime": "2017-02-11 5:47"
    },
    "current": {
        "last_updated_epoch": 1486792043,
        "last_updated": "2017-02-11 05:47",
        "temp_c": 0.0,
        "temp_f": 32.0,
        "is_day": 0,
        "condition": {
            "text": "Mist",
            "icon": "//cdn.apixu.com/weather/64x64/night/143.png",
            "code": 1030
        },
        "wind_mph": 8.1,
        "wind_kph": 13.0,
        "wind_degree": 330,
        "wind_dir": "NNW",
        "pressure_mb": 1019.0,
        "pressure_in": 30.6,
        "precip_mm": 0.0,
        "precip_in": 0.0,
        "humidity": 74,
        "cloud": 0,
        "feelslike_c": -4.0,
        "feelslike_f": 24.8
    }
}

创建外部表命令如下:

CREATE EXTERNAL TABLE weatherdata (
   location STRUCT<
      name:STRING, 
      region:STRING, 
      country:STRING, 
      lat:FLOAT,
      lon:FLOAT, 
      tz_id:STRING, 
      localtime:STRING>,
   current STRUCT<
      last_updated_epoch:BIGINT, 
      last_updated:STRING, 
      temp_c:FLOAT,
      temp_f:FLOAT, 
      is_day:INT,
      condition:STRUCT<text:STRING, icon:STRING, code:INT>,
      wind_mph:FLOAT, wind_kph:FLOAT, 
      wind_degree:INT, 
      wind_dir:STRING,
      pressure_mb:FLOAT, 
      pressure_in:FLOAT, 
      precip_mm:FLOAT, 
      precip_in:FLOAT,
      humidity:INT, 
      cloud:INT, 
      feelslike_c:FLOAT, 
      feelslike_f:FLOAT>
)
ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe';

我收到以下错误:

FAILED: ParseException line 9:3 cannot recognize input near 'current' 'STRUCT' '<' in column specification

我尝试使用字段“current”作为“curr”创建这个外部表,只是为了检查并成功创建了表。很明显,将上面给出的“json”数据加载到此表中时,只有“位置”数据加载成功,“当前”数据显示为空。 “当前”是蜂巢中的关键字吗?什么问题?

【问题讨论】:

  • 你有condition:STRUCT,那么为什么locationcurrent 上没有:
  • @cricket_007 ':' 仅用于内部结构变量。

标签: json hadoop hive hdfs hiveql


【解决方案1】:

是的,“CURRENT”是 Hive 中的 Reserved Keyword。您可以将它们用作标识符,方法是使用反引号 (`) 字符将它们括起来。请参阅此documentation 在列名中引用标识符。

在这里,create 语句将是

....      
       tz_id:STRING, 
          localtime:STRING>,
       `current` STRUCT<
          last_updated_epoch:BIGINT, 
          last_updated:STRING, 
          temp_c:FLOAT,
....

也在select:

select `current` from weatherdata;

【讨论】:

    【解决方案2】:

    Current 是保留的关键字。因此使用 DDL 如下:- 使用反引号

    CREATE EXTERNAL TABLE weatherdata (
       `location` STRUCT<
          name:STRING, 
          region:STRING, 
          country:STRING, 
          lat:FLOAT,
          lon:FLOAT, 
          tz_id:STRING, 
          localtime:STRING>,
       `current` STRUCT<
          last_updated_epoch:BIGINT, 
          last_updated:STRING, 
          temp_c:FLOAT,
          temp_f:FLOAT, 
          is_day:INT,
          condition:STRUCT<text:STRING, icon:STRING, code:INT>,
          wind_mph:FLOAT, wind_kph:FLOAT, 
          wind_degree:INT, 
          wind_dir:STRING,
          pressure_mb:FLOAT, 
          pressure_in:FLOAT, 
          precip_mm:FLOAT, 
          precip_in:FLOAT,
          humidity:INT, 
          cloud:INT, 
          feelslike_c:FLOAT, 
          feelslike_f:FLOAT>
    )
    ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe';
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-28
      相关资源
      最近更新 更多