LoginSignup
2

More than 5 years have passed since last update.

DynamoDB から EMR の hive を使って S3 に CSV 出力してみた

Last updated at Posted at 2017-11-30

1. S3 に CSV を出力するバケットとフォルダを作成

今回はこんな感じにした。

emr-dynamodb-export-test-2017-11-30/2130

  • バケット:emr-dynamodb-export-test-2017-11-30
  • フォルダ:2130

2. EMR でクラスタ起動

  • クラスターを作成 をクリック

image.png

  • クラスター名 を入力する。
  • EC2 キーペア を指定する。

image.png

  • 他はデフォルトのまま クラスターを作成 をクリック。(もちろん変えても良し)

image.png

しばらく待つ。

image.png

待機中になったら次へ。

3. マスタに SSH接続する

  • マスターパブリック DNS に、 SSH リンクがあるので、クリック。
    • ssh コマンドのサンプルが載っている。ユーザは hadoop
    • pem ファイルは、自身のパスへ変更する。
ssh -i ~/hoge.pem hadoop@ec2-1x-1xx-2xx-7x.ap-northeast-1.compute.amazonaws.com

※ 理由はわからないけど、セキュリティグループのインバウンドに 22 が無くて、手動で追加したことがあった。

ダーン!!

image.png

hive 実行

  • hive コマンドを実行
hive 
  • DynamoDB に格納されたデータを参照する Hive テーブルを作成する。(この記事 と同じ DynamoDB のテーブルを使用) − testtable01 が DynamoDB のテーブル名。
create external table hive_Table (
  hv_timestamp bigint,
  hv_user      string,
  hv_id        string,
  hv_jst       string,
  hv_value     bigint
)
stored by 'org.apache.hadoop.hive.dynamodb.DynamoDBStorageHandler' 
tblproperties (
  "dynamodb.table.name" = "testtable01",
  "dynamodb.column.mapping"="hv_timestamp:timestamp,hv_user:user,hv_id:id,hv_jst:jst,hv_value:value"
)
;
  • Amazon S3 に EXTERNAL テーブルを作成する。
create external table s3_Table (
  hv_timestamp bigint,
  hv_user      string,
  hv_id        string,
  hv_jst       string,
  hv_value     bigint
)
row format serde 'org.apache.hadoop.hive.serde2.OpenCSVSerde'
location 's3://emr-dynamodb-export-test-2017-11-30/2130/'
; 
  • DynamoDB のデータを Amazon S3 に書き込む。
insert overwrite table s3_Table 
select * 
FROM hive_Table
;
  • こんなの出力されて
Query ID = hadoop_20171130135920_255a0f5b-47ad-4a9a-b00c-99cd0394ef00
Total jobs = 1
Launching Job 1 out of 1
Status: Running (Executing on YARN cluster with App id application_1512049485217_0001)

----------------------------------------------------------------------------------------------
        VERTICES      MODE        STATUS  TOTAL  COMPLETED  RUNNING  PENDING  FAILED  KILLED
----------------------------------------------------------------------------------------------
Map 1 .......... container     SUCCEEDED      1          1        0        0       0       0
Reducer 2 ...... container     SUCCEEDED      1          1        0        0       0       0
----------------------------------------------------------------------------------------------
VERTICES: 02/02  [==========================>>] 100%  ELAPSED TIME: 36.58 s
----------------------------------------------------------------------------------------------
Loading data to table default.s3tablenamek
OK
Time taken: 43.527 seconds
  • こんなファイルが S3 に出来る。

image.png

  • ダウンロードする。

(終わったら、クラスタを削除する)

以上

4. 次は...

  • where 書ける?そりゃできると思うけど。ワクワク
  • 出力ファイル名は指定できる?

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
2