<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Kazuhiro Sasaki on Medium]]></title>
        <description><![CDATA[Stories by Kazuhiro Sasaki on Medium]]></description>
        <link>https://medium.com/@kazuhirosasaki?source=rss-69ff656d465------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*c06LvIluIBW3on_x.</url>
            <title>Stories by Kazuhiro Sasaki on Medium</title>
            <link>https://medium.com/@kazuhirosasaki?source=rss-69ff656d465------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 00:38:40 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kazuhirosasaki/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[embulk-mysql-output を高速化する]]></title>
            <link>https://medium.com/@kazuhirosasaki/embulk-mysql-output-%E3%82%92%E9%AB%98%E9%80%9F%E5%8C%96%E3%81%99%E3%82%8B-cc4179dea4af?source=rss-69ff656d465------2</link>
            <guid isPermaLink="false">https://medium.com/p/cc4179dea4af</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[mysql]]></category>
            <dc:creator><![CDATA[Kazuhiro Sasaki]]></dc:creator>
            <pubDate>Mon, 16 Jan 2017 15:49:29 GMT</pubDate>
            <atom:updated>2017-01-16T15:49:29.818Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="http://www.embulk.org/">embulk</a> は様々なデータソースに対応した高速なデータロードツールです。</p><p>特にRDBMSとの入出力ができるembulk-output-jdbcおよびembulk-input-jdbcの派生プラグインは製品ごとの差異を意識せずにデータのやりとりを行える非常に便利なプラグインです。</p><h3>embulk-output-jdbc のパフォーマンス</h3><p>便利なプラグインたちですが、私には不満に思っていることがあります。 それはembulk-output-jdbcの派生プラグインの中には各製品がサポートする高速なデータロード機能を使うものと、そうでないものがあることです。</p><p><strong>データロード専用機能を使うもの</strong></p><ul><li>embulk-output-postgres</li><li>embulk-output-vertica</li><li>embulk-output-redshift</li></ul><p><strong>データロード専用機能を使わないもの(BULK INSERT)</strong></p><ul><li>embulk-output-sqlserver</li><li>embulk-output-mysql</li></ul><p>mysqlにはLOAD DATAという高速ロードができるコマンドがあるのにそれを使っていないではないですか... これはちょっと見過ごせないということで、プラグインを改造してLOAD DATA を使ったロードを試して見ましょう</p><h3>実装</h3><h3>load data APIは存在するのか?</h3><p>とはいえ、jdbc経由でLOAD DATAは使えるのでしょうか？ 調べてみるといくつかの記事が見つかります。</p><ul><li><a href="http://stackoverflow.com/questions/17313921/does-any-jdbc-driver-supports-load-data-infile-sql-command">Does any JDBC driver supports LOAD DATA INFILE sql command?</a></li><li><a href="http://d.hatena.ne.jp/sh2/20090528">MySQL Connector/Jにおける大量INSERTのチューニング</a></li></ul><p>１つ目の記事は期待はずれでしたが、２つ目の記事で紹介されているcom.mysql.jdbc.Statement#setLocalInfileInputStreamはまさに私の求めるものでした。 サンプルにあるように各行のデータをデリミタで結合した文字列をByteArrayInputStreamに変換してsetLocalInfileInputStreamに渡せば良さそうです。</p><p>１つ目の記事は期待はずれでしたが、２つ目の記事で紹介されているcom.mysql.jdbc.Statement#setLocalInfileInputStreamはまさに私の求めるものでした。 サンプルにあるように各行のデータをデリミタで結合した文字列をByteArrayInputStreamに変換してsetLocalInfileInputStreamに渡せば良さそうです。</p><p>念のため<a href="http://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-implementation-notes.html">リファレンス</a>も確認します.</p><pre>Connector/J 5.1.3 and later include two additional methods: setLocalInfileInputStream() sets an InputStream instance that will be used to send data to the MySQL server for a LOAD DATA LOCAL INFILE statement rather than a FileInputStream or URLInputStream that represents the path given as an argument to the statement. This stream will be read to completion upon execution of a LOAD DATA LOCAL INFILE statement, and will automatically be closed by the driver, so it needs to be reset before each call to execute*() that would cause the MySQL server to request data to fulfill the request for LOAD DATA LOCAL INFILE. If this value is set to NULL, the driver will revert to using a FileInputStream or URLInputStream as required.</pre><p>まぁサンプルコードの通りで問題ないでしょう…</p><h3>コード</h3><p>ゼロからプラグインを作るのはつらいのでembulk-output-mysqlをフォークしました。</p><p>コードはこちら <a href="https://github.com/kazup0n/embulk-output-jdbc/commit/2fd323e4ba43ad977c04ce0557c21c367688e710">kazup0n/embulk-output-jdbc</a></p><h3>テスト</h3><p>実装は適当に手を抜いてさっさと終わらせたので、次は動作確認とパフォーマンス計測です。 テストに必要なのは次の２つです。</p><ul><li>テストデータ（少なすぎると差異がわからないのでたくさん必要です)</li><li>テストマシン（しょぼいとプラグインの実装以外がボトルネックになってしまうので早いのが必要です)</li></ul><p>それぞれどうしたのかはこれから紹介します。</p><h3>テストマシン</h3><p>先日ハンズオン用に試用アカウントを作ったばかりのGoogle Cloud PlatformのCompute Engineを使いました。 仮想マシンの作成が早く、SSHなどの設定も簡単なのでこういう用途にはぴったりです。 ちなみに安価なIaaSには他に<a href="https://www.digitalocean.com/">Digital Ocean</a>や<a href="https://www.linode.com/">Linode</a>があります。 使ったマシンのスペックは下記の通りです。</p><pre>n1-standard-4（vCPU x 4、メモリ 15 GB） CPU プラットフォーム: Intel Haswell ストレージ: SSD 100GB ゾーン: us-central1-c</pre><h3>テストデータ</h3><p>テストデータは<a href="http://osdldbt.sourceforge.net/">dbt3</a>というデータベース用のベンチマークテストのものを使いました。 ベンチマークには他にTPCやdbtの他のテストスイートがありますが、ググって最初に出てきたのでこれにしました。</p><h4>dbt3の中身</h4><p>ダウンロードして適当に展開します</p><p>DDL</p><p>RDBMSごとにテーブルの初期化用のシェルスクリプトがあります。</p><p>mysqlは dbt3-1.10/scripts/mysql/dbt3-mysql-create-tables にあります。</p><p>シェルスクリプトを読むとこんなのが見つかります(これはデータ生成で使うlineitemというデータに対応するDDLです)</p><pre>CREATE TABLE lineitem ( l_orderkey INTEGER, l_partkey INTEGER, l_suppkey INTEGER, l_linenumber INTEGER, l_quantity DECIMAL(10,2), l_extendedprice DECIMAL(10,2), l_discount DECIMAL(10,2), l_tax DECIMAL(10,2), l_returnflag CHAR(1), l_linestatus CHAR(1), l_shipDATE DATE, l_commitDATE DATE, l_receiptDATE DATE, l_shipinstruct CHAR(25), l_shipmode CHAR(10), l_comment VARCHAR(44) );</pre><p>データ</p><p>データはプログラムで生成します。そしてプログラムは自分でビルドしなきゃいけませんがいつもの感じで大丈夫でした。</p><pre>$ cd dbt3-1.10 $ ./configure $ cd src/dbgen $ make</pre><p>ただしmacだと#include &lt;malloc.h&gt;という行でエラーになるので、下記のように書き換えます。</p><pre>- #include &lt;malloc.h&gt; + #include &lt;malloc/malloc.h&gt;</pre><p>さて dbgen を実行してみると下記のようなファイルができます。</p><pre>$ make $ ./dbgen <br>$ ls -l *.tbl <br>% ll *.tbl <br>-rw-r--r-- 1 kazup0n staff 23M 12 13 22:36 customer.tbl <br>-rw-r--r-- 1 kazup0n staff 725M 12 13 22:36 lineitem.tbl <br>-rw-r--r-- 1 kazup0n staff 2.2K 12 13 22:36 nation.tbl <br>-rw-r--r-- 1 kazup0n staff 164M 12 13 22:36 orders.tbl <br>-rw-r--r-- 1 kazup0n staff 23M 12 13 22:36 part.tbl <br>-rw-r--r-- 1 kazup0n staff 113M 12 13 22:36 partsupp.tbl <br>-rw-r--r-- 1 kazup0n staff 389B 12 13 22:36 region.tbl <br>-rw-r--r-- 1 kazup0n staff 1.3M 12 13 22:36 supplier.tbl</pre><p>lineitem.tbl はサイズが大きくていい感じです。 ただしサイズが大きすぎるかもしれないし、テストするならいくつかの件数で試したいところです。 件数を確かめてから適当なサブセットに切り出します。</p><pre>$ wc -l lineitem.tbl 6001215 (行) $ mv lineitem.tbl lineitem.tbl.orig $ head -n 500000 lineitem.tbl.orig &gt; lineitem.500000.tbl $ head -n 1500000 lineitem.tbl.orig &gt; lineitem.1000000.tbl $ head -n 1500000 lineitem.tbl.orig &gt; lineitem.1500000.tbl</pre><h3>実施</h3><p>作成したテストデータ３種類を使ってロードに必要な時間を調べました。 計測方法は下記の通り</p><ul><li>時間の計測は time embulk run config.yml の出力結果のrealを採用する</li><li>各パターン１回づつ計測する（最初は３回づつ実施してみたがほとんど変わらないので１回にした）</li></ul><h3>結果</h3><p><a href="https://public.tableau.com/views/embulk-output-mysql-performance-comprison/sheet0?:embed=y&amp;:display_count=yes">実行結果はここ</a></p><p>適当に作った割には早くなっているようです😺</p><h3>まとめ</h3><p>embulk-output-mysqlのコードを参考にしてLOAD DATA APIを使ってデータロードする実装を作り、パフォーマンスをテストしました。結果として(一応)パフォーマンスは改善されました。</p><p>しかし実用するには下記の点がまだまだ不足しています。</p><ul><li>面倒だったのでスキップした日時/タイムスタンプ系のカラムをサポートする必要があります</li><li>フィールド区切り、行区切りに使う文字がデータ中に含まれることを考慮する必要があります</li><li>囲み文字とエスケープ文字を考慮する必要があります</li></ul><p>さて、今回使ったツールやサービスをもう一度紹介します。ほとんどが無料で使えるものなのでプログラミングやデータ処理、分析に使ってみてください。</p><ul><li>IntelliJ Idea （IDE 統合開発環境)</li><li>embulk (ETLツール)</li><li>github.com (コラボレーションツール)</li><li>tableau public （データビジュアライズツール）</li><li>dbt (データベースのベンチマークスイート)</li><li>StackOverflow (QAサイト)</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cc4179dea4af" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[My Favorite spots in Otaru]]></title>
            <link>https://medium.com/@kazuhirosasaki/my-favorite-spots-in-otaru-9b9aa4817999?source=rss-69ff656d465------2</link>
            <guid isPermaLink="false">https://medium.com/p/9b9aa4817999</guid>
            <category><![CDATA[photography]]></category>
            <category><![CDATA[otaru]]></category>
            <dc:creator><![CDATA[Kazuhiro Sasaki]]></dc:creator>
            <pubDate>Sun, 26 Jun 2016 02:08:35 GMT</pubDate>
            <atom:updated>2016-06-26T02:14:18.895Z</atom:updated>
            <content:encoded><![CDATA[<p>Let me introduce my favorite spots in Otaru.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*b4ujhfSH_lIZeWVVhxGVlg.jpeg" /></figure><p>北運河エリア</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*O1epMQpXk43Z-Hg8xRHiHw.jpeg" /></figure><ul><li><a href="https://www.flickr.com/photos/7257211@N03/12718926915/in/dateposted-public/">R0019402</a></li><li><a href="https://www.flickr.com/photos/7257211@N03/12719380154/in/dateposted-public/">R0019379</a></li></ul><p>南小樽/南樽市場</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JFntakTM7SBaEAbO5utfMQ.jpeg" /></figure><ul><li><a href="https://www.flickr.com/photos/7257211@N03/7782098820/in/dateposted-public/">DSC_0046</a></li><li><a href="https://www.flickr.com/photos/7257211@N03/14121529034/in/dateposted-public/">R0019918</a></li><li><a href="https://www.flickr.com/photos/7257211@N03/27295468603/in/dateposted-public/">2016-06-26 10.32.12</a></li></ul><p>旧手宮線趾</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rHi9-BWFrqbl533i2DaeDw.jpeg" /></figure><iframe src="https://cdn.embedly.com/widgets/media.html?type=text%2Fhtml&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;schema=instagram&amp;url=https%3A//www.instagram.com/p/KkioO3i8ny/&amp;image=https%3A//i.embed.ly/1/image%3Furl%3Dhttps%253A%252F%252Fscontent.cdninstagram.com%252Ft51.2885-15%252Fe15%252F11203182_1581360268808802_1555098045_n.jpg%253Fig_cache_key%253DMTkwNDI5MzgyNTgxMTQ0MDUw.2%26key%3D4fce0568f2ce49e8b54624ef71a8a5bd" width="612" height="612" frameborder="0" scrolling="no"><a href="https://medium.com/media/e19d255d5fb5b63b6dbd5df4cf9e03e4/href">https://medium.com/media/e19d255d5fb5b63b6dbd5df4cf9e03e4/href</a></iframe><p>花園町</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6WEfJd2jBWnS58AyejpYUg.jpeg" /></figure><p><a href="https://www.flickr.com/photos/7257211@N03/27873381396/in/dateposted-public/">2016-06-26 10.33.05</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9b9aa4817999" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>