在HIVE表中上传JSON数据,给出NoViableAltException / PraseException

时间:2017-02-02 07:03:47

标签: json hadoop hive

我的Json在下面。我必须将此JSON加载到配置单元中,并且必须查询一些细节。

{
  "id": "1234",
  "pdid": "abcd",
  "summary": {
    "tripStartTimestamp": 1485263310528,
    "tripEndTimestamp": 0,
    "status": 10,
    "totalGPSDistanceMetres": 0,
    "avgGPSSpeed": 0,
    "maxGPSSpeed": 0,
    "avgInstMileage": 0,
    "totalHaltTimeSeconds": 0,
    "totalIdlingTimeSeconds": 0,
    "totalRunningTimeMins": 0,
    "startLocation": {
      "latitude": 13.022425,
      "longitude": 77.760587,
      "speed": 70,
      "ts": 1485263310528,
      "direction": 0
    },
    "endLocation": null,
    "driverBehaviorSummary": [
      {
        "driver": null,
        "noOfRapidAcceleration": 0,
        "noOfRapidDeceleration": 0,
        "noOfOverSpeed": 0,
        "noOfHarshBreak": 0
      }
    ]
  },
  "latLongs": [
    {
      "latitude": 13.022425,
      "longitude": 77.760587,
      "speed": 70,
      "ts": 1485263310528,
      "direction": 0
    }
  ],
  "halts": [],
  "idlings": []
}

我在下面写了针对HIVE的create table语句。我计算了JSON模式,并使用它来创建下面的结构。

CREATE TABLE TABLE_ABC_Test1(
  id string ,
  pdid string ,
  summary object<struct<
  tripStartTimestamp:int,
  tripEndTimestamp:int,
  status:int,
  totalGPSDistanceMetres:int,
  avgGPSSpeed:int,
  maxGPSSpeed:int,
  avgInstMileage:int,
  totalHaltTimeSeconds:int,
  totalIdlingTimeSeconds:int,
  totalRunningTimeMins:int,
  startLocation object<struct<
  latitude:int,
  longitude:int,
  speed:int,
  ts:int,
  direction:int>>
  endLocation:string,
  driverBehaviorSummary array<struct<object<struct<
  driver:string,
  noOfRapidAcceleration:int,
  noOfRapidDeceleration:int,
  noOfOverSpeed:int,
  noOfHarshBreak:int
  >>>>
  >>
  latLongs<array<struct<object<
  latitude:int,
  longitude:int,
  speed:int,
  ts:int,
  direction:int
  >>>>
  halts<array<struct<>>>
  idlings<array<struct<>>>
   )
ROW FORMAT SERDE 
'org.apache.hive.hcatalog.data.JsonSerDe' 
STORED AS TEXTFILE;

但错误已经来了:

NoViableAltException(26@[])
        .......some hive stack trace
FAILED: ParseException line 4:10 cannot recognize input near 'object' '<' 'struct' in column type

1 个答案:

答案 0 :(得分:1)

Hive没有object数据类型,对于某些字段分配,您还缺少:

create table语法为

CREATE TABLE TABLE_ABC_Test1(
  id string ,
  pdid string ,
  summary struct<
  tripStartTimestamp:int,
  tripEndTimestamp:int,
  status:int,
  totalGPSDistanceMetres:int,
  avgGPSSpeed:int,
  maxGPSSpeed:int,
  avgInstMileage:int,
  totalHaltTimeSeconds:int,
  totalIdlingTimeSeconds:int,
  totalRunningTimeMins:int,
  startLocation:struct<
  latitude:int,
  longitude:int,
  speed:int,
  ts:int,
  direction:int>,
  endLocation:string,
  driverBehaviorSummary:array<struct<
  driver:string,
  noOfRapidAcceleration:int,
  noOfRapidDeceleration:int,
  noOfOverSpeed:int,
  noOfHarshBreak:int
  >>
  >,
  latLongs array<struct<
  latitude:int,
  longitude:int,
  speed:int,
  ts:int,
  direction:int
  >>,
  halts array<string>,
  idlings array<string>
   )
ROW FORMAT SERDE 
'org.apache.hive.hcatalog.data.JsonSerDe' 
STORED AS TEXTFILE;