4
3

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.

hiveの使い方メモ

Last updated at Posted at 2019-06-11

#Table

CREATE TABLE

###table2と同じ構造のtable1を作る

CREATE TABLE table1 LIKE table2;

##ALTER TABLE
###テーブル名の変更
table_nameをnew_table_nameに変更する

ALTER TABLE table_name RENAME TO new_table_name;

#SELECT

##文字列操作
###文字の結合
区切り文字を入れない場合の文字結合(CONCAT)。
引数にNULLが1つでも来ると、結合した文字列もNULLになる。

SELECT CONCAT(string_a, string_b, ...) FROM table_name;

###SPLIT
指定のデリミタで、カラムを配列に変換する。index_numberは0始まり。

SELECT SPLIT(column_name, 'delimiter')[index_number] FROM table_name;

##条件式(WHERE句)
###LIKE
search_wordでワイルドカードを使いたい場合は「%」を使う

SELECT * FROM table_name WHERE column_name LIKE 'search_word';

WITH

WITHを使うとサブクエリを利用せずにSELECTの結果をテーブルの様に扱える。同じSQL内で何度もSELECT結果を利用する場合に便利。

WITH tmp_table_1 as (
  SELECT
    *
  FROM table_name
)

SELECT * FROM tmp_table_1

WITHで複数のテーブルを定義することもできる。

WITH tmp_table_1 as (
  SELECT
    *
  FROM table_name
),
tmp_table_2 as (
  SELECT
    *
  FROM table_name
)

SELECT *
FROM tmp_table_1 a
JOIN tmp_table_2 b
ON a.col_name == b.col_name

#INSERT

###SELECTした結果を保存する

INSERT OVERWRITE TABLE insert_table_name
SELECT [column_name ...] FROM select_table_name

#LOAD
###ファイルのデータを、テーブルに上書き保存する

LOAD DATA LOCAL INPATH 'file_path' OVERWRITE INTO TABLE table_name;

SQL内の変数(hivevar)

SET hivevar:begin_date=2019-01-01;
SET hivevar:end_date=2019-01-31;

select * from table_name
where col_name between ${hivevar:begin_date} and ${hivevar:end_date} 
4
3
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
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?