How do I select from an array of struct

VISQL picture VISQL · Jul 12, 2014 · Viewed 7.9k times · Source

I'm using HIVE for Hadoop. I want to select the price value from the price field in a struct. But the struct is part of an array of struct.

uid     string
timestamp       int
eventdate       string
products    array<struct
    <internalId:int,
    category:int,
    alternateId:string,
    price:double,
    quantity:int,
    externalId:string>>

I will be querying using an exectued .HQL file, not through the terminal window.

(i.e. hive -f blah.hql > blah.txt)

How do I select the price out of the product array of struct?

-- don't know which of these, if any work. getting weird or null output.

Select  
  table.products.price,  
  table.products[0].price,  
  table.products['price']
from table

Answer

VISQL picture VISQL · Jul 12, 2014

Answer

produts[0].price

Since the struct is the ONLY element in the array, for a given row in the table. While this can allow for future elements in the products array, it would have nice to make that column JUST a struct.