Apache Drill查询列名称中包含空格的JSON

时间:2016-10-28 06:07:53

标签: sql json apache-drill

我的数据集格式如下:

    {  
   "business_id":"5UmKMjUEUNdYWqANhGckJw",
   "full_address":"4734 Lebanon Church Rd\nDravosburg, PA 15034",
   "hours":{  
      "Friday":{  
         "close":"21:00",
         "open":"11:00"
      },
      "Tuesday":{  
         "close":"21:00",
         "open":"11:00"
      },
      "Thursday":{  
         "close":"21:00",
         "open":"11:00"
      },
      "Wednesday":{  
         "close":"21:00",
         "open":"11:00"
      },
      "Monday":{  
         "close":"21:00",
         "open":"11:00"
      }
   },
   "open":true,
   "categories":[  
      "Fast Food",
      "Restaurants"
   ],
   "city":"Dravosburg",
   "review_count":4,
   "name":"Mr Hoagie",
   "neighborhoods":[  

   ],
   "longitude":-79.9007057,
   "state":"PA",
   "stars":4.5,
   "latitude":40.3543266,
   "attributes":{  
      "Take-out":true,
      "Drive-Thru":false,
      "Good For":{  
         "dessert":false,
         "latenight":false,
         "lunch":false,
         "dinner":false,
         "brunch":false,
         "breakfast":false
      },
      "Caters":false,
      "Noise Level":"average",
      "Takes Reservations":false,
      "Delivery":false,
      "Ambience":{  
         "romantic":false,
         "intimate":false,
         "classy":false,
         "hipster":false,
         "divey":false,
         "touristy":false,
         "trendy":false,
         "upscale":false,
         "casual":false
      },
      "Parking":{  
         "garage":false,
         "street":false,
         "validated":false,
         "lot":false,
         "valet":false
      },
      "Has TV":false,
      "Outdoor Seating":false,
      "Attire":"casual",
      "Alcohol":"none",
      "Waiter Service":false,
      "Accepts Credit Cards":true,
      "Good for Kids":true,
      "Good For Groups":true,
      "Price Range":1
   },
   "type":"business"
}

我想查询数据集的属性。但是,许多属性名称中都有空格。如何用空格引用属性名称?示例 - 我想找到拉斯维加斯餐厅的平均“价格范围”。我尝试使用以下方法引用它:

select avg(`t.attributes.Price Range`) from `mongo.274_BI`.`yelp_dataset`t where t.city = 'Las Vegas';

返回null。由于价格和范围之间的空间存在问题。我毫无疑问地询问了“停车”字段。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

使用以下查询:

select avg(t.attributes.`Price Range`) from `mongo.274_BI`.`yelp_dataset`t where t.city = 'Las Vegas';

此外,您可能需要 CAST attributes.Price Range为int或double。

示例:

select avg(cast(t.attributes.`Price Range` as double)) from `mongo.274_BI`.`yelp_dataset`t where t.city = 'Las Vegas';