LoginSignup
28

More than 5 years have passed since last update.

Hiveで配列で保存されたカラムを展開して集計する

Last updated at Posted at 2013-03-08

LATERAL VIEW を使うと配列を展開しそれぞれの要素が結合されるのでそれを利用する。

ex)
users テーブル内の attributes にユーザの属性が登録されていて女性ユーザの人数を取得する場合。

  • users table
name type
user_id string
user_attributes array
  • attributes array

["female", "28", "orange"]
["male", "18", "car"]
["female", "32", "art"]

SELECT count(*) FROM tracks 
LATERAL VIEW explode(attributes) exploded_attributes AS attribute
  WHERE
    attribute = 'female'

下記の様に結合されるので条件を設定することで件数を集計できる。

user_id attribute
1 female
1 28
1 orange
2 male
2 18
2 car
3 female
3 32
3 art

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
28