LoginSignup
1
0

More than 1 year has passed since last update.

Amazon Athena どうしても日本語カラムを使ったテーブルをcliでCREATEしたい場合

Last updated at Posted at 2022-03-31

この記事の目的

 本記事では、Amazon Athenaのcliで日本語カラムがあるテーブルを作成する方法を記述します。備忘録です。
 Amazon Athenaに限らずDBテーブルで日本語カラムを設定するケースは殆どありませんが、運悪くそのような場面(SESで案件の規定がそうなっていた、など)に出会った時に活用していただければありがたいです。

使用する環境

vagrant(amazonlinux2) amazon athenaを使用できる環境構築は完了しているものとする(awscliのインストールなど)。

結論

日本語カラムはバックスラッシュ + バッククォーテーションで囲みます。
その他注意点として、WITH SerDePropertiesのカッコ内はダブルクォーテーションではなくシングルクォーテーションを使うほうが望ましいです(エスケープしようとするとぐちゃぐちゃになるため)。

下記は日本語カラムを用いたテーブル作成クエリです。s3に保存されたcsvをデータとして使用します。

aws athena start-query-execution --query-string "
CREATE EXTERNAL TABLE test1.user (
\`ⅠD\` int,
\`苗字\` string,
\`名前\`  string
) 
ROW FORMAT SerDe 'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe' 
WITH SerDeProperties ('field.delim' = ',', 'escapeChar'='\\\',  'quoteChar'='\"')
STORED AS TEXTFILE
LOCATION 's3://athenatest20220331/input/';
" --result-configuration OutputLocation=s3://athenatest20220331/output/

使用例

下記のcsvをs3://athenatest20220331/input/へ保存します。

1,山田,太郎
2,鈴木,華子

Amazon athenaのデータベースをcliで作成します。

aws athena start-query-execution --query-string "
CREATE DATABASE test1;
" --result-configuration OutputLocation=s3://athenatest20220331/output/

前述したテーブル作成cliを実行します(前述したcliの再掲です)。

aws athena start-query-execution --query-string "
CREATE EXTERNAL TABLE test1.user (
\`ⅠD\` int,
\`苗字\` string,
\`名前\`  string
) 
ROW FORMAT SerDe 'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe' 
WITH SerDeProperties ('field.delim' = ',', 'escapeChar'='\\\',  'quoteChar'='\"')
STORED AS TEXTFILE
LOCATION 's3://athenatest20220331/input/';
" --result-configuration OutputLocation=s3://athenatest20220331/output/

データをSELECT文で取得します。

aws athena start-query-execution --query-string "
SELECT * FROM test1.user;
" --result-configuration OutputLocation=s3://athenatest20220331/output/

取得したデータはs3://athenatest20220331/output/に保存されたファイルから閲覧できます。

"ⅰd","苗字","名前"
"1","山田","太郎"
"2","鈴木","華子"

以上

1
0
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
1
0