LoginSignup
20
16

More than 5 years have passed since last update.

巷で知ったEmbulkプラグイン開発ノウハウ(Java編)

Last updated at Posted at 2015-07-17

ちなみに当人、Javaは普段書きませんので、簡単なことでも教えていただけると非常にありがたいです。

右側の目次でやりたいことを探してください。

カラム

カラムを絞る方法

元ネタ

List<String> columnNames = task.getColumns();
ImmutableList.Builder<Column> builder = ImmutableList.builder();
int i = 0;
for (String columnName : columnNames) {
    for (Column inputColumn: inputSchema.getColumns()) {
        if (inputColumn.getName().equals(columnName)) {
            Column outputColumn = new Column(i++, columnName, inputColumn.getType());
            builder.add(outputColumn);
            break;
        }
    }
}
Schema outputSchema = new Schema(builder.build());

時刻

時刻パースの仕方(Embulk 0.6.14以降)

sonotsさんが詳しく書いてくださいました。

Embulk の Timestamp クラスまわりのメモ (Java)

TimestampParser.Task をPluginTaskに追記します。

public interface PluginTask
        extends Task, LineDecoder.DecoderTask, TimestampParser.Task

クラスのインスタンス化

final TimestampParser time_parser = new TimestampParser(task.getJRuby(),"%d/%b/%Y:%T %z",task.getDefaultTimeZone());

パース

try {
    pageBuilder.setTimestamp(3,time_parser.parse(accessLogEntryMatcher.group(4)));
} catch(TimestampParseException ex) {
  throw Throwables.propagate(ex);
}

実装例

時刻出力時のフォーマット指定

元ネタ

sonotsさんが詳しく書いてくださいました。

Embulk の Timestamp クラスまわりのメモ (Java)

テスト

gemインストールせずに開発中のプラグインをテストする

embulk のプラグイン開発時にお試し実行するには

travisでテスト

embulk のプラグインを travis でテストする

LocalFile

gradle

javaのオプション追記方法(Deprecation確認)

build.gradleに下記のエントリを追加します。

//
// for deprecation check
//
allprojects {
    tasks.withType(JavaCompile) {
        options.compilerArgs << "-Xlint:deprecation"
    }
}

タスクの依存関係を表示する。

gradlew tasks --allとするとタスクの依存関係が出力されます。--allを省略するとトップレベルのタスクだけ出力されます。

/gradlew tasks --all
:tasks

------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------

Build tasks
-----------
assemble - Assembles the outputs of this project. [jar]
build - Assembles and tests this project. [assemble, check]
buildDependents - Assembles and tests this project and all projects that depend on it.                                                                          [build]
buildNeeded - Assembles and tests this project and all projects it depends on. [build]
classes - Assembles classes 'main'.
    compileJava - Compiles Java source 'main:java'.
    processResources - Processes JVM resources 'main:resources'.
...

プロジェクトの依存関係を出力

./gradlew dependencies
:dependencies

------------------------------------------------------------
Root project
------------------------------------------------------------

archives - Configuration for archive artifacts.
No dependencies

checkstyle - The Checkstyle libraries to be used for this project.
\--- com.puppycrawl.tools:checkstyle:6.7
     +--- org.apache.commons:commons-lang3:3.4
     +--- antlr:antlr:2.7.7
     +--- org.antlr:antlr4-runtime:4.5
     +--- commons-beanutils:commons-beanutils-core:1.8.3
     +--- commons-cli:commons-cli:1.3
     \--- com.google.guava:guava:18.0

compile - Compile classpath for source set 'main'.
+--- org.embulk:embulk-core:0.7.0
|    +--- com.google.guava:guava:18.0
|    +--- com.google.inject:guice:4.0
|    |    +--- javax.inject:javax.inject:1
|    |    +--- aopalliance:aopalliance:1.0
|    |    \--- com.google.guava:guava:16.0.1 -> 18.0
|    +--- com.google.inject.extensions:guice-multibindings:4.0
|    |    \--- com.google.inject:guice:4.0 (*)
|    +--- javax.inject:javax.inject:1

一時ファイル

一時ファイルの作り方

Embulkのソースコード整形

Embulkのソースコードスタイルチェック方法 (Java編)を参照

DynamicPageBuilder

String→long/timestamp…の型変換を自動でやってくれて、ColumnVisitorが不要になるDynamicPageBuilderという実験的実装

EmbulkEmbed

ソースの変更を検出して自動でコンパイルしてくれるgradleオプション

./gradlew gem -t 

gradleがフォアグランドで起動します。コンパイルが終わってもコマンドは終了せず次のようなメッセ〜ジが表示されます。

Waiting for changes to input files of tasks... (ctrl-d to exit)

ソースを修正すると変更を自動的に検出し次のようなメッセージを出力してコンパイルを継続します。

Change detected, executing build...

embulk migrateの注意点

build.gradleorg.embulk:embulk-core:0.7.+のように+を利用している場合にembulk migrateを実行すると最新のバージョン番号に置き換わります。(Emblk 0.8.0から)

これは仕様です。参考

build.gradleの変更前と変更後の値は次のようになります。

diff --git a/build.gradle b/build.gradle
index c3985ed..478f544 100644
--- a/build.gradle
+++ b/build.gradle
@@ -21,13 +21,13 @@ sourceCompatibility = 1.7
 targetCompatibility = 1.7

 dependencies {
-    compile  "org.embulk:embulk-core:0.7.+"
-    provided "org.embulk:embulk-core:0.7.+"
+    compile  "org.embulk:embulk-core:0.8.1"
+    provided "org.embulk:embulk-core:0.8.1"
     // compile "YOUR_JAR_DEPENDENCY_GROUP:YOUR_JAR_DEPENDENCY_MODULE:YOUR_JAR_DEPENDENCY_VERSION"
     compile 'org.apache.hadoop:hadoop-client:2.6.0'
     testCompile "junit:junit:4.+"
-    testCompile "org.embulk:embulk-core:0.7.+:tests"
-    testCompile "org.embulk:embulk-standards:0.7.+"
+    testCompile "org.embulk:embulk-core:0.8.1:tests"
+    testCompile "org.embulk:embulk-standards:0.8.1"
 }

最後に一度だけ実行

SimpleDateFormatは使わないほうが良いらしい。

ImmutableでスレッドセーフになったJavaの新しい日時APIの基礎知識 (1/5)

SimpleDateFormatはスレッドセーフでないらしく、誤ったデータを返すことがあるようだ。Joda-Timeを使うのが良い模様。
(パーサーは良いかもしれない(自信なし)が、フィルタでは使わないほうがよさそう)

JavaでGuessを実装する

embulk-parser-grokを参考にしましょう。

Mapreduce-executor.

リトライ

追補

trunsationとrunでのパラメータの共有

その他

20
16
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
20
16