5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Hive0.13とPrestoで配列の順序関係を残して色々処理したい。

Last updated at Posted at 2015-03-20

クエリのちょっとした書き方。

元のテーブルには、カンマで区切られた文字列が格納している。

num ans
1,4 2,5

この各カラムには対応関係があり、
1 - 2
4 - 5
この対応関係を残して処理したい

つまり、下のようなテーブルが完成できればよい。

num ans
1 2
4 5

Hive 0.10だと難しいが、
Hive 0.13からposexplode関数がある。
これはpositionを保持したまま、配列を展開できる。

SELECT t1.col as num, t2.col as ans
FROM (
SELECT  seq, col  FROM table
LATERAL VIEW posexplode(split('1,4', ',')) rec as seq, col
) t1
JOIN (
SELECT  seq, col  FROM table
LATERAL VIEW posexplode(split('2,5', ',')) rec as seq, col
) t2
ON t1.seq = t2.seq

Prestoではregexp_extract_all関数とUNNESTを使う。

SELECT
 n as num, a as ans
FROM (
  -- record 生成用
  SELECT n1, n2
  FROM (
    VALUES
      ('1,4', '2,5')
  ) AS x (n1, n2)
) t1
CROSS JOIN UNNEST(regexp_extract_all(n1, '[^,]+'), regexp_extract_all(n2, '[^,]+')) AS t (n, a)

ちなみにTreasureDataのPrestoの自動構文チェックではUNNESTを使うとWarningがでるが、
無視して実行してよい。
presto.png

5
6
0

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
5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?