embulk-filter-copy 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (31) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +6 -1
  3. data/README.md +5 -1
  4. data/build.gradle +6 -5
  5. data/example/config.yml +1 -0
  6. data/example/data.tsv +5 -5
  7. data/gradle/wrapper/gradle-wrapper.jar +0 -0
  8. data/gradle/wrapper/gradle-wrapper.properties +1 -2
  9. data/gradlew +13 -10
  10. data/src/main/java/org/embulk/filter/copy/CopyFilterPlugin.java +26 -37
  11. data/src/main/java/org/embulk/filter/copy/forward/{ForwardBaseTask.java → ForwardParentTask.java} +1 -1
  12. data/src/main/java/org/embulk/filter/copy/forward/InForwardEventReader.java +67 -62
  13. data/src/main/java/org/embulk/filter/copy/forward/InForwardService.java +102 -35
  14. data/src/main/java/org/embulk/filter/copy/forward/OutForwardEventBuilder.java +66 -54
  15. data/src/main/java/org/embulk/filter/copy/forward/OutForwardService.java +2 -2
  16. data/src/main/java/org/embulk/filter/copy/plugin/InternalForwardInputPlugin.java +11 -13
  17. data/src/main/java/org/embulk/filter/copy/runner/AsyncEmbulkRunnerService.java +32 -0
  18. data/src/main/java/org/embulk/filter/copy/runner/EmbulkRunner.java +124 -0
  19. data/src/main/java/org/embulk/filter/copy/spi/DataBuilder.java +41 -0
  20. data/src/main/java/org/embulk/filter/copy/spi/DataReader.java +41 -0
  21. data/src/main/java/org/embulk/filter/copy/spi/ElapsedTime.java +81 -0
  22. data/src/main/java/org/embulk/filter/copy/spi/PageBuilder.java +16 -0
  23. data/src/main/java/org/embulk/filter/copy/spi/PageReader.java +13 -0
  24. data/src/main/java/org/embulk/filter/copy/{util → spi}/StandardColumnVisitor.java +6 -6
  25. data/src/test/java/org/embulk/filter/copy/TestCopyFilterPlugin.java +0 -11
  26. metadata +24 -16
  27. data/src/main/java/org/embulk/filter/copy/executor/EmbulkExecutor.java +0 -105
  28. data/src/main/java/org/embulk/filter/copy/executor/LocalThreadExecutor.java +0 -96
  29. data/src/main/java/org/embulk/filter/copy/forward/InForwardVisitor.java +0 -63
  30. data/src/main/java/org/embulk/filter/copy/forward/OutForwardVisitor.java +0 -63
  31. data/src/main/java/org/embulk/filter/copy/util/ElapsedTime.java +0 -165
@@ -1,105 +0,0 @@
1
- package org.embulk.filter.copy.executor;
2
-
3
- import com.fasterxml.jackson.annotation.JsonCreator;
4
- import com.fasterxml.jackson.annotation.JsonValue;
5
- import com.google.common.base.Joiner;
6
- import com.google.inject.Injector;
7
- import org.embulk.EmbulkEmbed;
8
- import org.embulk.config.Config;
9
- import org.embulk.config.ConfigDefault;
10
- import org.embulk.config.ConfigException;
11
- import org.embulk.config.ConfigSource;
12
- import org.embulk.guice.LifeCycleInjector;
13
- import org.embulk.spi.Exec;
14
- import org.slf4j.Logger;
15
-
16
- import java.lang.reflect.Constructor;
17
- import java.lang.reflect.InvocationTargetException;
18
- import java.util.Locale;
19
-
20
- public abstract class EmbulkExecutor
21
- {
22
- static final Logger logger = Exec.getLogger(EmbulkExecutor.class);
23
-
24
- public interface ExecutorTask
25
- extends org.embulk.config.Task
26
- {
27
- @Config("\"type\"")
28
- @ConfigDefault("\"local_thread\"")
29
- ExecutorType getType();
30
-
31
- enum ExecutorType
32
- {
33
- LOCAL_THREAD;
34
-
35
- @JsonValue
36
- @Override
37
- public String toString()
38
- {
39
- return name().toLowerCase(Locale.ENGLISH);
40
- }
41
-
42
- @JsonCreator
43
- public static ExecutorType fromString(String value)
44
- {
45
- switch (value) {
46
- case "local_thread":
47
- return LOCAL_THREAD;
48
- default:
49
- String values = Joiner.on(", ").join(values()).toLowerCase(Locale.ENGLISH);
50
- throw new ConfigException(String.format("Unknown executor type '%s'. Supported executor types are %s", value, values));
51
- }
52
- }
53
- }
54
-
55
- }
56
-
57
- public interface Task
58
- extends org.embulk.config.Task
59
- {
60
- @Config("executor")
61
- @ConfigDefault("{}")
62
- ExecutorTask getExecutorTask();
63
- }
64
-
65
- public static EmbulkExecutor buildExecutor(Task task)
66
- {
67
- switch (task.getExecutorTask().getType()) {
68
- case LOCAL_THREAD:
69
- return new LocalThreadExecutor(task.getExecutorTask());
70
- default:
71
- String values = Joiner.on(", ").join(ExecutorTask.ExecutorType.values()).toLowerCase(Locale.ENGLISH);
72
- throw new ConfigException(String.format("Unknown executor type '%s'. Supported executor types are %s", task.getExecutorTask().getType(), values));
73
- }
74
- }
75
-
76
- final ExecutorTask task;
77
- final Injector injector;
78
-
79
- EmbulkExecutor(ExecutorTask task)
80
- {
81
- this.task = task;
82
- this.injector = Exec.getInjector();
83
- }
84
-
85
- public abstract void setup();
86
-
87
- public abstract void executeAsync(ConfigSource config);
88
-
89
- public abstract void waitUntilExecutionFinished();
90
-
91
- public abstract void shutdown();
92
-
93
- EmbulkEmbed newEmbulkEmbed()
94
- {
95
- try {
96
- Constructor<EmbulkEmbed> constructor = EmbulkEmbed.class
97
- .getDeclaredConstructor(ConfigSource.class, LifeCycleInjector.class);
98
- constructor.setAccessible(true);
99
- return constructor.newInstance(null, injector);
100
- }
101
- catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
102
- throw new ConfigException(e);
103
- }
104
- }
105
- }
@@ -1,96 +0,0 @@
1
- package org.embulk.filter.copy.executor;
2
-
3
- import com.google.common.util.concurrent.FutureCallback;
4
- import com.google.common.util.concurrent.Futures;
5
- import com.google.common.util.concurrent.ListenableFuture;
6
- import com.google.common.util.concurrent.ListeningExecutorService;
7
- import com.google.common.util.concurrent.MoreExecutors;
8
- import org.embulk.config.ConfigSource;
9
- import org.embulk.exec.ExecutionResult;
10
- import org.embulk.filter.copy.util.ElapsedTime;
11
-
12
- import javax.annotation.Nullable;
13
-
14
- import java.util.concurrent.Callable;
15
- import java.util.concurrent.Executors;
16
-
17
- public class LocalThreadExecutor
18
- extends EmbulkExecutor
19
- {
20
- private static final String THREAD_NAME = "embulk executor service";
21
- private static final int NUM_THREADS = 1;
22
- private final ListeningExecutorService es;
23
- private ListenableFuture<ExecutionResult> future;
24
-
25
- LocalThreadExecutor(ExecutorTask task)
26
- {
27
- super(task);
28
- this.es = MoreExecutors.listeningDecorator(
29
- Executors.newFixedThreadPool(
30
- NUM_THREADS,
31
- r -> new Thread(r, THREAD_NAME)
32
- ));
33
- }
34
-
35
- @Override
36
- public void setup()
37
- {
38
- }
39
-
40
- @Override
41
- public void executeAsync(ConfigSource config)
42
- {
43
- logger.debug("execute with this config: {}", config);
44
- if (future != null) {
45
- throw new IllegalStateException("executeAsync is already called.");
46
- }
47
- future = es.submit(embulkRun(config));
48
- Futures.addCallback(future, resultFutureCallback());
49
- }
50
-
51
- @Override
52
- public void waitUntilExecutionFinished()
53
- {
54
- if (future == null) {
55
- throw new NullPointerException();
56
- }
57
- ElapsedTime.debugUntil(() -> future.isDone() || future.isCancelled(),
58
- logger, "embulk executor", 3000L);
59
- }
60
-
61
- @Override
62
- public void shutdown()
63
- {
64
- ElapsedTime.info(
65
- logger,
66
- "embulk executor service shutdown",
67
- es::shutdown);
68
- }
69
-
70
- private Callable<ExecutionResult> embulkRun(ConfigSource config)
71
- {
72
- return () -> newEmbulkEmbed().run(config);
73
- }
74
-
75
- private FutureCallback<ExecutionResult> resultFutureCallback()
76
- {
77
- return new FutureCallback<ExecutionResult>()
78
- {
79
- @Override
80
- public void onSuccess(@Nullable ExecutionResult result)
81
- {
82
- for (Throwable throwable : result.getIgnoredExceptions()) {
83
- logger.warn("Ignored error ", throwable);
84
- }
85
- logger.info("Config diff: {}", result.getConfigDiff());
86
- logger.debug("ExecutionResult: {}", result);
87
- }
88
-
89
- @Override
90
- public void onFailure(Throwable t)
91
- {
92
- throw new RuntimeException(t);
93
- }
94
- };
95
- }
96
- }
@@ -1,63 +0,0 @@
1
- package org.embulk.filter.copy.forward;
2
-
3
- import org.embulk.spi.Column;
4
- import org.embulk.spi.ColumnVisitor;
5
- import org.embulk.spi.PageBuilder;
6
-
7
- public class InForwardVisitor
8
- implements ColumnVisitor
9
- {
10
- private final InForwardEventReader reader;
11
- private final PageBuilder builder;
12
-
13
- public InForwardVisitor(InForwardEventReader reader, PageBuilder builder)
14
- {
15
- this.reader = reader;
16
- this.builder = builder;
17
- }
18
-
19
- private void nullOr(Column column, Runnable r)
20
- {
21
- if (reader.isNull(column)) {
22
- builder.setNull(column);
23
- return;
24
- }
25
- r.run();
26
- }
27
-
28
- @Override
29
- public void booleanColumn(Column column)
30
- {
31
- nullOr(column, () -> builder.setBoolean(column, reader.getBoolean(column)));
32
- }
33
-
34
- @Override
35
- public void longColumn(Column column)
36
- {
37
- nullOr(column, () -> builder.setLong(column, reader.getLong(column)));
38
- }
39
-
40
- @Override
41
- public void doubleColumn(Column column)
42
- {
43
- nullOr(column, () -> builder.setDouble(column, reader.getDouble(column)));
44
- }
45
-
46
- @Override
47
- public void stringColumn(Column column)
48
- {
49
- nullOr(column, () -> builder.setString(column, reader.getString(column)));
50
- }
51
-
52
- @Override
53
- public void timestampColumn(Column column)
54
- {
55
- nullOr(column, () -> builder.setTimestamp(column, reader.getTimestamp(column)));
56
- }
57
-
58
- @Override
59
- public void jsonColumn(Column column)
60
- {
61
- nullOr(column, () -> builder.setJson(column, reader.getJson(column)));
62
- }
63
- }
@@ -1,63 +0,0 @@
1
- package org.embulk.filter.copy.forward;
2
-
3
- import org.embulk.spi.Column;
4
- import org.embulk.spi.ColumnVisitor;
5
- import org.embulk.spi.PageReader;
6
-
7
- public class OutForwardVisitor
8
- implements ColumnVisitor
9
- {
10
- private final PageReader reader;
11
- private final OutForwardEventBuilder builder;
12
-
13
- public OutForwardVisitor(PageReader reader, OutForwardEventBuilder builder)
14
- {
15
- this.reader = reader;
16
- this.builder = builder;
17
- }
18
-
19
- private void nullOr(Column column, Runnable r)
20
- {
21
- if (reader.isNull(column)) {
22
- builder.setNull(column);
23
- return;
24
- }
25
- r.run();
26
- }
27
-
28
- @Override
29
- public void booleanColumn(Column column)
30
- {
31
- nullOr(column, () -> builder.setBoolean(column, reader.getBoolean(column)));
32
- }
33
-
34
- @Override
35
- public void longColumn(Column column)
36
- {
37
- nullOr(column, () -> builder.setLong(column, reader.getLong(column)));
38
- }
39
-
40
- @Override
41
- public void doubleColumn(Column column)
42
- {
43
- nullOr(column, () -> builder.setDouble(column, reader.getDouble(column)));
44
- }
45
-
46
- @Override
47
- public void stringColumn(Column column)
48
- {
49
- nullOr(column, () -> builder.setString(column, reader.getString(column)));
50
- }
51
-
52
- @Override
53
- public void timestampColumn(Column column)
54
- {
55
- nullOr(column, () -> builder.setTimestamp(column, reader.getTimestamp(column)));
56
- }
57
-
58
- @Override
59
- public void jsonColumn(Column column)
60
- {
61
- nullOr(column, () -> builder.setJson(column, reader.getJson(column)));
62
- }
63
- }
@@ -1,165 +0,0 @@
1
- package org.embulk.filter.copy.util;
2
-
3
- import org.slf4j.Logger;
4
-
5
- import java.util.function.Supplier;
6
-
7
- public class ElapsedTime
8
- {
9
- private ElapsedTime()
10
- {
11
- }
12
-
13
- private final static String TEMPLATE = " ({} ms)";
14
- private enum LogLevel {TRACE, DEBUG, INFO, WARN, ERROR}
15
- private enum Status {START, RUNNING, FINISHED}
16
-
17
- private static void log(Logger logger, LogLevel level, String message, long elapsed)
18
- {
19
- String msgWithTmpl = message + TEMPLATE;
20
- switch (level)
21
- {
22
- case TRACE:
23
- logger.trace(msgWithTmpl, elapsed);
24
- break;
25
- case DEBUG:
26
- logger.debug(msgWithTmpl, elapsed);
27
- break;
28
- case INFO:
29
- logger.info(msgWithTmpl, elapsed);
30
- break;
31
- case WARN:
32
- logger.warn(msgWithTmpl, elapsed);
33
- break;
34
- case ERROR:
35
- logger.error(msgWithTmpl, elapsed);
36
- break;
37
- }
38
- }
39
-
40
- private static <T>T run(Logger logger, LogLevel level, String message, Supplier<T> proc)
41
- {
42
- long start = System.currentTimeMillis();
43
- try {
44
- return proc.get();
45
- }
46
- finally {
47
- log(logger, level, message, System.currentTimeMillis() - start);
48
- }
49
- }
50
-
51
- private static void run(Logger logger, LogLevel level, String message, Runnable proc)
52
- {
53
- long start = System.currentTimeMillis();
54
- try {
55
- proc.run();
56
- }
57
- finally {
58
- log(logger, level, message, System.currentTimeMillis() - start);
59
- }
60
- }
61
-
62
- public static <T>T trace(Logger logger, String message, Supplier<T> proc)
63
- {
64
- return run(logger, LogLevel.TRACE, message, proc);
65
- }
66
-
67
- public static <T>T debug(Logger logger, String message, Supplier<T> proc)
68
- {
69
- return run(logger, LogLevel.DEBUG, message, proc);
70
- }
71
-
72
- public static <T>T info(Logger logger, String message, Supplier<T> proc)
73
- {
74
- return run(logger, LogLevel.INFO, message, proc);
75
- }
76
-
77
- public static <T>T warn(Logger logger, String message, Supplier<T> proc)
78
- {
79
- return run(logger, LogLevel.WARN, message, proc);
80
- }
81
-
82
- public static <T>T error(Logger logger, String message, Supplier<T> proc)
83
- {
84
- return run(logger, LogLevel.ERROR, message, proc);
85
- }
86
-
87
- public static void trace(Logger logger, String message, Runnable proc)
88
- {
89
- run(logger, LogLevel.TRACE, message, proc);
90
- }
91
-
92
- public static void debug(Logger logger, String message, Runnable proc)
93
- {
94
- run(logger, LogLevel.DEBUG, message, proc);
95
- }
96
-
97
- public static void info(Logger logger, String message, Runnable proc)
98
- {
99
- run(logger, LogLevel.INFO, message, proc);
100
- }
101
-
102
- public static void warn(Logger logger, String message, Runnable proc)
103
- {
104
- run(logger, LogLevel.WARN, message, proc);
105
- }
106
-
107
- public static void error(Logger logger, String message, Runnable proc)
108
- {
109
- run(logger, LogLevel.ERROR, message, proc);
110
- }
111
-
112
- private static void logUntil(Supplier<Boolean> condition,
113
- Logger logger, LogLevel level, String message, long sleep)
114
- {
115
- long start = System.currentTimeMillis();
116
- log(logger, level, String.format("(%s) %s", Status.START, message),
117
- System.currentTimeMillis() - start);
118
- while (true) {
119
- if (condition.get()) {
120
- log(logger, level, String.format("(%s) %s", Status.FINISHED, message),
121
- System.currentTimeMillis() - start);
122
- break;
123
- }
124
- try {
125
- Thread.sleep(sleep);
126
- }
127
- catch (InterruptedException e) {
128
- logger.warn(e.getMessage(), e);
129
- }
130
-
131
- log(logger, level, String.format("(%s) %s", Status.RUNNING, message),
132
- System.currentTimeMillis() - start);
133
- }
134
- }
135
-
136
- public static void traceUntil(Supplier<Boolean> condition,
137
- Logger logger, String message, long sleep)
138
- {
139
- logUntil(condition, logger, LogLevel.TRACE, message, sleep);
140
- }
141
-
142
- public static void debugUntil(Supplier<Boolean> condition,
143
- Logger logger, String message, long sleep)
144
- {
145
- logUntil(condition, logger, LogLevel.DEBUG, message, sleep);
146
- }
147
-
148
- public static void infoUntil(Supplier<Boolean> condition,
149
- Logger logger, String message, long sleep)
150
- {
151
- logUntil(condition, logger, LogLevel.INFO, message, sleep);
152
- }
153
-
154
- public static void warnUntil(Supplier<Boolean> condition,
155
- Logger logger, String message, long sleep)
156
- {
157
- logUntil(condition, logger, LogLevel.WARN, message, sleep);
158
- }
159
-
160
- public static void errorUntil(Supplier<Boolean> condition,
161
- Logger logger, String message, long sleep)
162
- {
163
- logUntil(condition, logger, LogLevel.ERROR, message, sleep);
164
- }
165
- }