LoginSignup
6
4

More than 5 years have passed since last update.

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

Last updated at Posted at 2015-07-18

Java な embulk プラグインを作るにあたって、Timestamp オブジェクトの作り方とか、format の仕方とか parse の仕方とか。

JRuby の機能を使ったりしていて、けっこうややこしい(それ自体は %Y-%m-%d のような Ruby ライクな時間フォーマットを使えるようにするためなので仕方ないのだが)

Timestamp オブジェクトの作り方

cf. http://www.embulk.org/docs/javadoc/org/embulk/spi/time/Timestamp.html

ただ Timestamp オブジェクトを作るだけであれば、

Timestamp.ofEpochSecond(long epochSecond);
Timestamp.ofEpochSecond(long epochSecond, long nanoAdjustMent);
Timestamp.ofEpochMilli(long epochMilli);

あたりで生成できる。

TimestampParser

cf. http://www.embulk.org/docs/javadoc/org/embulk/spi/time/TimestampParser.html

YYYY-MM-DD のような文字列から Timestamp オブジェクトを作るのがけっこう面倒。

DateTimeZone は Joda の API なのでそちらを参照のこと cf. http://www.joda.org/joda-time/apidocs/org/joda/time/DateTimeZone.html

import org.embulk.config.Task;
import org.joda.time.DateTimeZone;
import org.embulk.spi.time.Timestamp;
import org.embulk.spi.time.TimestampParser;
import org.embulk.spi.time.TimestampParseException;
import com.google.common.base.Throwables;

public class SampleFilterPlugin implements FilterPlugin
{
    // TimestampParser.Task の extends が必要
    public interface PluginTask extends Task, TimestampParser.Task
    {
    }

    @Override
    public PageOutput open(TaskSource taskSource, Schema inputSchema,
            Schema outputSchema, PageOutput output)
    {
        PluginTask task = taskSource.loadTask(PluginTask.class);

        String time            = "2015-07-14";
        String format          = "%Y-%m-%d";
        DateTimeZone timezone  = DateTimeZone.forID("UTC");

        TimestampParser parser = new TimestampParser(task.getJRuby(), format, timezone);
        Timestamp timestamp;
        try {
            timestamp = parser.parse(time);
        } catch(TimestampParseException ex) {
            throw Throwables.propagate(ex);
        }
     }
}

要望: TimestampParser.Task の extends が必要とかなると単体テストしづらいので依存消したい。サンプルコードも大きくなるし。

TimestampFormatter

cf. http://www.embulk.org/docs/javadoc/org/embulk/spi/time/TimestampFormatter.html

Ruby でいう Time#strftime みたいなことをやる

import org.embulk.config.Task;
import org.embulk.spi.Exec;
import org.joda.time.DateTimeZone;
import org.embulk.spi.time.Timestamp;
import org.embulk.spi.time.TimestampFormatter;

public class SampleFilterPlugin implements FilterPlugin
{
    // TimestampFormatter.Task の extends が必要
    public interface PluginTask extends Task, TimestampFormatter.Task
    {
    }

    @Override
    public PageOutput open(TaskSource taskSource, Schema inputSchema,
            Schema outputSchema, PageOutput output)
    {
        PluginTask task = taskSource.loadTask(PluginTask.class);

        Timestamp timestamp    = Timestamp.ofEpochSecond(1437229271);
        String format          = "%Y-%m-%d";
        DateTimeZone timezone  = DateTimeZone.forID("UTC");

        TimestampFormatter formatter = new TimestampFormatter(task.getJRuby(), format, timezone);
        System.out.println(formatter.format(timestamp)); 
     }
}

Transaction 実行時間(現在時間に近いもの)を取得したい場合は Exec.session().getTransactionTime() で取れるらしい。

要望: TimestampFormatter.Task の extends が必要とかなると単体テストしづらいので依存消したい。サンプルコードも大きくなるし。

6
4
4

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
6
4