自己投資としてチャレンジしている内容を Blog で公開しています。今回は Apache Hive 用にサンプルデータの一括作成方法について紹介します。
———————————–
▼1. Apache Hive を利用するメリット
———————————–
Apache Hive のメリットは Hadoop の分散ストレージにあるデータに対し、Java や python などコードを書かなくても、SQL のようなクエリで分散処理が実行できることです。この SQL のクエリは Hive Query Language (HiveQL もしくは HQL) と呼ばれています。
———————————–
▼2. サンプルデータ作成の準備
———————————–
Apache Hive の環境を準備します。
(参考) Apache Hive インストール 3 ノードの環境 No.57 –2022/08
———————————–
▼3. サンプルデータの一括作成
———————————–
3-1. 事前準備
Hive コンソールで、データベースおよびテーブルを作成します。
hive> create database sampleDB;
OK
Time taken: 1.907 seconds
hive> use sampleDB;
OK
Time taken: 0.111 seconds
hive> create table sampletable(num int,cdatetime timestamp,note string);
OK
Time taken: 1.068 secondsこの HiveQL ではデータベース sampleDB を作成し、その DB の中で sampletable を作成しています。sampletable のスキマは以下のようになっています。
hive> describe sampletable;
OK
num int
cdatetime timestamp
note string
Time taken: 0.15 seconds, Fetched: 3 row(s)3-2. サンプルデータを作成するために、python で insert クエリ “generatehql.hql” を作成します。
この例では num の数は 10 ですが、この値を増やしサンプルレコードを増やすことができます。
import random,string,datetime
dtnow=datetime.datetime.now()
def randomword(length):
letters=string.ascii_lowercase
return ''.join(random.choice(letters) for i in range(length))
with open("generatehql.hql","w") as f:
f.write("INSERT INTO TABLE sampleDB.sampletable values(0,CAST('2022-10-21 12:33:36.009302' as TIMESTAMP),'init')")
for num in range(1,10):
f.write(",(%d,CAST('%s' as TIMESTAMP),'%s')" % (num,dtnow,randomword(num)))
f.write(";")(参考) Python のコードの実行方法はこちらを参照ください。
Python – Visual Studio Code の利用 No.34 – 2021/07
3-3. generatehql.hql に出力された insert 文を hive コンソールで実行します。
(例)
hive> INSERT INTO TABLE sampleDB.sampletable values(0,CAST('2022-10-21 12:33:36.009302' as TIMESTAMP),'init'),(1,CAST('2022-10-21 23:57:22.816816' as TIMESTAMP),'c'),(2,CAST('2022-10-21 23:57:22.816816' as TIMESTAMP),'lh'),(3,CAST('2022-10-21 23:57:22.816816' as TIMESTAMP),'mhp'),(4,CAST('2022-10-21 23:57:22.816816' as TIMESTAMP),'cchu'),(5,CAST('2022-10-21 23:57:22.816816' as TIMESTAMP),'tdeoe'),(6,CAST('2022-10-21 23:57:22.816816' as TIMESTAMP),'venfcr'),(7,CAST('2022-10-21 23:57:22.816816' as TIMESTAMP),'ltrlbzl'),(8,CAST('2022-10-21 23:57:22.816816' as TIMESTAMP),'bwtfvxpz'),(9,CAST('2022-10-21 23:57:22.816816' as TIMESTAMP),'nvddvgmuo');
Query ID = hadoop_20221021235806_c62e9cdf-dd19-4dd3-9c99-3bc3263df746
Total jobs = 3
Launching Job 1 out of 3
Number of reduce tasks determined at compile time: 1
In order to change the average load for a reducer (in bytes):
set hive.exec.reducers.bytes.per.reducer=<number>
In order to limit the maximum number of reducers:
set hive.exec.reducers.max=<number>
In order to set a constant number of reducers:
set mapreduce.job.reduces=<number>
Starting Job = job_1666363555433_0001, Tracking URL = http://maste
xxx 省略 xxx
Total MapReduce CPU Time Spent: 4 seconds 160 msec
OK
Time taken: 45.697 seconds3-4. Insert されたデータを確認します。
hive> select * from sampletable;
OK
0 2022-10-21 12:33:36.009302 init
1 2022-10-21 23:57:22.816816 c
2 2022-10-21 23:57:22.816816 lh
3 2022-10-21 23:57:22.816816 mhp
4 2022-10-21 23:57:22.816816 cchu
5 2022-10-21 23:57:22.816816 tdeoe
6 2022-10-21 23:57:22.816816 venfcr
7 2022-10-21 23:57:22.816816 ltrlbzl
8 2022-10-21 23:57:22.816816 bwtfvxpz
9 2022-10-21 23:57:22.816816 nvddvgmuo
Time taken: 0.448 seconds, Fetched: 10 row(s)3-5. おまけ. explain を使って、select のクエリプランを見てみます。
hive> explain select * from sampletable;
OK
STAGE DEPENDENCIES:
Stage-0 is a root stage
STAGE PLANS:
Stage: Stage-0
Fetch Operator
limit: -1
Processor Tree:
TableScan
alias: sampletable
Statistics: Num rows: 10 Data size: 339 Basic stats: COMPLETE Column stats: NONE
Select Operator
expressions: num (type: int), cdatetime (type: timestamp), note (type: string)
outputColumnNames: _col0, _col1, _col2
Statistics: Num rows: 10 Data size: 339 Basic stats: COMPLETE Column stats: NONE
ListSink
Time taken: 0.353 seconds, Fetched: 17 row(s)———————————–
▼4. 参考情報
———————————–
(1) Apache Hive インストール 3 ノードの環境 No.57 –2022/08
(2) Python – Visual Studio Code の利用 No.34 – 2021/07
以上です。参考になりましたら幸いです。