embulk-output-cassandra 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +13 -0
  3. data/LICENSE.txt +21 -0
  4. data/README.md +45 -0
  5. data/build.gradle +97 -0
  6. data/config/checkstyle/checkstyle.xml +128 -0
  7. data/config/checkstyle/default.xml +108 -0
  8. data/embulk-output-cassandra.iml +13 -0
  9. data/gradle/wrapper/gradle-wrapper.jar +0 -0
  10. data/gradle/wrapper/gradle-wrapper.properties +5 -0
  11. data/gradlew +172 -0
  12. data/gradlew.bat +84 -0
  13. data/lib/embulk/output/cassandra.rb +3 -0
  14. data/src/main/java/org/embulk/output/cassandra/CassandraOutputPlugin.java +283 -0
  15. data/src/main/java/org/embulk/output/cassandra/setter/BigintColumnSetter.java +41 -0
  16. data/src/main/java/org/embulk/output/cassandra/setter/BooleanColumnSetter.java +30 -0
  17. data/src/main/java/org/embulk/output/cassandra/setter/CassandraColumnSetter.java +51 -0
  18. data/src/main/java/org/embulk/output/cassandra/setter/CassandraColumnSetterFactory.java +60 -0
  19. data/src/main/java/org/embulk/output/cassandra/setter/ColumnSetterVisitor.java +96 -0
  20. data/src/main/java/org/embulk/output/cassandra/setter/DateColumnSetter.java +29 -0
  21. data/src/main/java/org/embulk/output/cassandra/setter/DecimalColumnSetter.java +43 -0
  22. data/src/main/java/org/embulk/output/cassandra/setter/DoubleColumnSetter.java +41 -0
  23. data/src/main/java/org/embulk/output/cassandra/setter/FloatColumnSetter.java +41 -0
  24. data/src/main/java/org/embulk/output/cassandra/setter/InetColumnSetter.java +26 -0
  25. data/src/main/java/org/embulk/output/cassandra/setter/IntColumnSetter.java +41 -0
  26. data/src/main/java/org/embulk/output/cassandra/setter/ListColumnSetter.java +22 -0
  27. data/src/main/java/org/embulk/output/cassandra/setter/MapColumnSetter.java +22 -0
  28. data/src/main/java/org/embulk/output/cassandra/setter/SetColumnSetter.java +24 -0
  29. data/src/main/java/org/embulk/output/cassandra/setter/SmallintColumnSetter.java +41 -0
  30. data/src/main/java/org/embulk/output/cassandra/setter/TextColumnSetter.java +50 -0
  31. data/src/main/java/org/embulk/output/cassandra/setter/TimeColumnSetter.java +46 -0
  32. data/src/main/java/org/embulk/output/cassandra/setter/TimestampColumnSetter.java +42 -0
  33. data/src/main/java/org/embulk/output/cassandra/setter/TimeuuidColumnSetter.java +18 -0
  34. data/src/main/java/org/embulk/output/cassandra/setter/TinyintColumnSetter.java +41 -0
  35. data/src/main/java/org/embulk/output/cassandra/setter/TupleColumnSetter.java +34 -0
  36. data/src/main/java/org/embulk/output/cassandra/setter/UuidColumnSetter.java +18 -0
  37. data/src/main/java/org/embulk/output/cassandra/setter/ValueConverter.java +63 -0
  38. data/src/main/java/org/embulk/output/cassandra/setter/VarintColumnSetter.java +43 -0
  39. data/src/test/java/org/embulk/output/cassandra/TestCassandraOutputPlugin.java +250 -0
  40. data/src/test/resources/org/embulk/output/cassandra/create_keyspace.cql +5 -0
  41. data/src/test/resources/org/embulk/output/cassandra/create_table_test_basic.cql +11 -0
  42. data/src/test/resources/org/embulk/output/cassandra/create_table_test_complex.cql +12 -0
  43. data/src/test/resources/org/embulk/output/cassandra/create_table_test_uuid.cql +8 -0
  44. data/src/test/resources/org/embulk/output/cassandra/test1.csv +4 -0
  45. data/src/test/resources/org/embulk/output/cassandra/test2.csv +2 -0
  46. data/src/test/resources/org/embulk/output/cassandra/test3.csv +2 -0
  47. data/src/test/resources/org/embulk/output/cassandra/test_basic.yaml +3 -0
  48. data/src/test/resources/org/embulk/output/cassandra/test_complex.yaml +3 -0
  49. data/src/test/resources/org/embulk/output/cassandra/test_uuid.yaml +3 -0
  50. metadata +141 -0
@@ -0,0 +1,41 @@
1
+ package org.embulk.output.cassandra.setter;
2
+
3
+ import com.datastax.driver.core.BoundStatement;
4
+ import com.datastax.driver.core.ColumnMetadata;
5
+
6
+ public class IntColumnSetter extends CassandraColumnSetter
7
+ {
8
+ public IntColumnSetter(ColumnMetadata cassandraColumn)
9
+ {
10
+ super(cassandraColumn);
11
+ }
12
+
13
+ @Override
14
+ public void setBooleanValue(Boolean value, BoundStatement statement)
15
+ {
16
+ if (value) {
17
+ statement.setInt(cassandraColumn.getName(), 1);
18
+ }
19
+ else {
20
+ statement.setInt(cassandraColumn.getName(), 0);
21
+ }
22
+ }
23
+
24
+ @Override
25
+ public void setLongValue(Long value, BoundStatement statement)
26
+ {
27
+ statement.setInt(cassandraColumn.getName(), value.intValue());
28
+ }
29
+
30
+ @Override
31
+ public void setDoubleValue(Double value, BoundStatement statement)
32
+ {
33
+ statement.setInt(cassandraColumn.getName(), value.intValue());
34
+ }
35
+
36
+ @Override
37
+ public void setStringValue(String value, BoundStatement statement)
38
+ {
39
+ statement.setInt(cassandraColumn.getName(), Integer.parseInt(value));
40
+ }
41
+ }
@@ -0,0 +1,22 @@
1
+ package org.embulk.output.cassandra.setter;
2
+
3
+ import com.datastax.driver.core.BoundStatement;
4
+ import com.datastax.driver.core.ColumnMetadata;
5
+ import org.msgpack.value.Value;
6
+
7
+ public class ListColumnSetter extends CassandraColumnSetter
8
+ {
9
+ public ListColumnSetter(ColumnMetadata cassandraColumn)
10
+ {
11
+ super(cassandraColumn);
12
+ }
13
+
14
+ @Override
15
+ public void setJsonValue(Value value, BoundStatement statement)
16
+ {
17
+ if (!value.isArrayValue()) {
18
+ throw new RuntimeException(value.toJson() + " is not array value");
19
+ }
20
+ statement.setList(cassandraColumn.getName(), ValueConverter.convertList(value.asArrayValue().list()));
21
+ }
22
+ }
@@ -0,0 +1,22 @@
1
+ package org.embulk.output.cassandra.setter;
2
+
3
+ import com.datastax.driver.core.BoundStatement;
4
+ import com.datastax.driver.core.ColumnMetadata;
5
+ import org.msgpack.value.Value;
6
+
7
+ public class MapColumnSetter extends CassandraColumnSetter
8
+ {
9
+ public MapColumnSetter(ColumnMetadata cassandraColumn)
10
+ {
11
+ super(cassandraColumn);
12
+ }
13
+
14
+ @Override
15
+ public void setJsonValue(Value value, BoundStatement statement)
16
+ {
17
+ if (!value.isMapValue()) {
18
+ throw new RuntimeException(value.toJson() + " is not map value");
19
+ }
20
+ statement.setMap(cassandraColumn.getName(), ValueConverter.convertMap(value.asMapValue().map()));
21
+ }
22
+ }
@@ -0,0 +1,24 @@
1
+ package org.embulk.output.cassandra.setter;
2
+
3
+ import com.datastax.driver.core.BoundStatement;
4
+ import com.datastax.driver.core.ColumnMetadata;
5
+ import org.msgpack.value.Value;
6
+
7
+ import java.util.LinkedHashSet;
8
+
9
+ public class SetColumnSetter extends CassandraColumnSetter
10
+ {
11
+ public SetColumnSetter(ColumnMetadata cassandraColumn)
12
+ {
13
+ super(cassandraColumn);
14
+ }
15
+
16
+ @Override
17
+ public void setJsonValue(Value value, BoundStatement statement)
18
+ {
19
+ if (!value.isArrayValue()) {
20
+ throw new RuntimeException(value.toJson() + " is not array value");
21
+ }
22
+ statement.setSet(cassandraColumn.getName(), new LinkedHashSet<>(ValueConverter.convertList(value.asArrayValue().list())));
23
+ }
24
+ }
@@ -0,0 +1,41 @@
1
+ package org.embulk.output.cassandra.setter;
2
+
3
+ import com.datastax.driver.core.BoundStatement;
4
+ import com.datastax.driver.core.ColumnMetadata;
5
+
6
+ public class SmallintColumnSetter extends CassandraColumnSetter
7
+ {
8
+ public SmallintColumnSetter(ColumnMetadata cassandraColumn)
9
+ {
10
+ super(cassandraColumn);
11
+ }
12
+
13
+ @Override
14
+ public void setBooleanValue(Boolean value, BoundStatement statement)
15
+ {
16
+ if (value) {
17
+ statement.setShort(cassandraColumn.getName(), (short) 1);
18
+ }
19
+ else {
20
+ statement.setShort(cassandraColumn.getName(), (short) 0);
21
+ }
22
+ }
23
+
24
+ @Override
25
+ public void setLongValue(Long value, BoundStatement statement)
26
+ {
27
+ statement.setShort(cassandraColumn.getName(), value.shortValue());
28
+ }
29
+
30
+ @Override
31
+ public void setDoubleValue(Double value, BoundStatement statement)
32
+ {
33
+ statement.setShort(cassandraColumn.getName(), value.shortValue());
34
+ }
35
+
36
+ @Override
37
+ public void setStringValue(String value, BoundStatement statement)
38
+ {
39
+ statement.setShort(cassandraColumn.getName(), Short.parseShort(value));
40
+ }
41
+ }
@@ -0,0 +1,50 @@
1
+ package org.embulk.output.cassandra.setter;
2
+
3
+ import com.datastax.driver.core.BoundStatement;
4
+ import com.datastax.driver.core.ColumnMetadata;
5
+ import org.embulk.spi.time.Timestamp;
6
+ import org.msgpack.value.Value;
7
+
8
+ public class TextColumnSetter extends CassandraColumnSetter
9
+ {
10
+ public TextColumnSetter(ColumnMetadata cassandraColumn)
11
+ {
12
+ super(cassandraColumn);
13
+ }
14
+
15
+ @Override
16
+ public void setBooleanValue(Boolean value, BoundStatement statement)
17
+ {
18
+ statement.setString(cassandraColumn.getName(), value.toString());
19
+ }
20
+
21
+ @Override
22
+ public void setLongValue(Long value, BoundStatement statement)
23
+ {
24
+ statement.setString(cassandraColumn.getName(), value.toString());
25
+ }
26
+
27
+ @Override
28
+ public void setDoubleValue(Double value, BoundStatement statement)
29
+ {
30
+ statement.setString(cassandraColumn.getName(), value.toString());
31
+ }
32
+
33
+ @Override
34
+ public void setStringValue(String value, BoundStatement statement)
35
+ {
36
+ statement.setString(cassandraColumn.getName(), value);
37
+ }
38
+
39
+ @Override
40
+ public void setTimestampValue(Timestamp value, BoundStatement statement)
41
+ {
42
+ statement.setString(cassandraColumn.getName(), value.toString());
43
+ }
44
+
45
+ @Override
46
+ public void setJsonValue(Value value, BoundStatement statement)
47
+ {
48
+ statement.setString(cassandraColumn.getName(), value.toJson());
49
+ }
50
+ }
@@ -0,0 +1,46 @@
1
+ package org.embulk.output.cassandra.setter;
2
+
3
+ import com.datastax.driver.core.BoundStatement;
4
+ import com.datastax.driver.core.ColumnMetadata;
5
+ import org.embulk.spi.time.Timestamp;
6
+
7
+ import java.time.Instant;
8
+ import java.time.LocalTime;
9
+ import java.time.ZoneId;
10
+ import java.time.ZonedDateTime;
11
+
12
+ public class TimeColumnSetter extends CassandraColumnSetter
13
+ {
14
+ public TimeColumnSetter(ColumnMetadata cassandraColumn)
15
+ {
16
+ super(cassandraColumn);
17
+ }
18
+
19
+ @Override
20
+ public void setLongValue(Long value, BoundStatement statement)
21
+ {
22
+ statement.setTime(cassandraColumn.getName(), value);
23
+ }
24
+
25
+ @Override
26
+ public void setDoubleValue(Double value, BoundStatement statement)
27
+ {
28
+ statement.setTime(cassandraColumn.getName(), value.longValue());
29
+ }
30
+
31
+ @Override
32
+ public void setStringValue(String value, BoundStatement statement)
33
+ {
34
+ LocalTime time = LocalTime.parse(value);
35
+ statement.setTime(cassandraColumn.getName(), time.toNanoOfDay());
36
+ }
37
+
38
+ @Override
39
+ public void setTimestampValue(Timestamp value, BoundStatement statement)
40
+ {
41
+ ZonedDateTime datetime = ZonedDateTime.ofInstant(Instant.ofEpochMilli(value.toEpochMilli()), ZoneId.of("UTC"));
42
+ long nsec = datetime.toLocalTime().toNanoOfDay();
43
+
44
+ statement.setTime(cassandraColumn.getName(), nsec);
45
+ }
46
+ }
@@ -0,0 +1,42 @@
1
+ package org.embulk.output.cassandra.setter;
2
+
3
+ import com.datastax.driver.core.BoundStatement;
4
+ import com.datastax.driver.core.ColumnMetadata;
5
+ import org.embulk.spi.time.Timestamp;
6
+
7
+ import java.util.Date;
8
+
9
+ public class TimestampColumnSetter extends CassandraColumnSetter
10
+ {
11
+ public TimestampColumnSetter(ColumnMetadata cassandraColumn)
12
+ {
13
+ super(cassandraColumn);
14
+ }
15
+
16
+ @Override
17
+ public void setLongValue(Long value, BoundStatement statement)
18
+ {
19
+ Date time = new Date(value * 1000);
20
+ statement.setTimestamp(cassandraColumn.getName(), time);
21
+ }
22
+
23
+ @Override
24
+ public void setDoubleValue(Double value, BoundStatement statement)
25
+ {
26
+ Date time = new Date(value.longValue() * 1000);
27
+ statement.setTimestamp(cassandraColumn.getName(), time);
28
+ }
29
+
30
+ @Override
31
+ public void setStringValue(String value, BoundStatement statement)
32
+ {
33
+ statement.setString(cassandraColumn.getName(), value);
34
+ }
35
+
36
+ @Override
37
+ public void setTimestampValue(Timestamp value, BoundStatement statement)
38
+ {
39
+ Date time = new Date(value.toEpochMilli());
40
+ statement.setTimestamp(cassandraColumn.getName(), time);
41
+ }
42
+ }
@@ -0,0 +1,18 @@
1
+ package org.embulk.output.cassandra.setter;
2
+
3
+ import com.datastax.driver.core.BoundStatement;
4
+ import com.datastax.driver.core.ColumnMetadata;
5
+ import com.datastax.driver.core.utils.UUIDs;
6
+
7
+ public class TimeuuidColumnSetter extends CassandraColumnSetter
8
+ {
9
+ public TimeuuidColumnSetter(ColumnMetadata cassandraColumn)
10
+ {
11
+ super(cassandraColumn);
12
+ }
13
+
14
+ public void setNullValue(BoundStatement statement)
15
+ {
16
+ statement.setUUID(cassandraColumn.getName(), UUIDs.timeBased());
17
+ }
18
+ }
@@ -0,0 +1,41 @@
1
+ package org.embulk.output.cassandra.setter;
2
+
3
+ import com.datastax.driver.core.BoundStatement;
4
+ import com.datastax.driver.core.ColumnMetadata;
5
+
6
+ public class TinyintColumnSetter extends CassandraColumnSetter
7
+ {
8
+ public TinyintColumnSetter(ColumnMetadata cassandraColumn)
9
+ {
10
+ super(cassandraColumn);
11
+ }
12
+
13
+ @Override
14
+ public void setBooleanValue(Boolean value, BoundStatement statement)
15
+ {
16
+ if (value) {
17
+ statement.setByte(cassandraColumn.getName(), (byte) 1);
18
+ }
19
+ else {
20
+ statement.setByte(cassandraColumn.getName(), (byte) 0);
21
+ }
22
+ }
23
+
24
+ @Override
25
+ public void setLongValue(Long value, BoundStatement statement)
26
+ {
27
+ statement.setByte(cassandraColumn.getName(), value.byteValue());
28
+ }
29
+
30
+ @Override
31
+ public void setDoubleValue(Double value, BoundStatement statement)
32
+ {
33
+ statement.setByte(cassandraColumn.getName(), value.byteValue());
34
+ }
35
+
36
+ @Override
37
+ public void setStringValue(String value, BoundStatement statement)
38
+ {
39
+ statement.setByte(cassandraColumn.getName(), Byte.parseByte(value));
40
+ }
41
+ }
@@ -0,0 +1,34 @@
1
+ package org.embulk.output.cassandra.setter;
2
+
3
+ import com.datastax.driver.core.BoundStatement;
4
+ import com.datastax.driver.core.Cluster;
5
+ import com.datastax.driver.core.ColumnMetadata;
6
+ import com.datastax.driver.core.TupleType;
7
+ import com.datastax.driver.core.TupleValue;
8
+ import org.msgpack.value.Value;
9
+
10
+ import java.util.List;
11
+
12
+ public class TupleColumnSetter extends CassandraColumnSetter
13
+ {
14
+ private final Cluster cluster;
15
+
16
+ public TupleColumnSetter(ColumnMetadata cassandraColumn, Cluster cluster)
17
+ {
18
+ super(cassandraColumn);
19
+ this.cluster = cluster;
20
+ }
21
+
22
+ @Override
23
+ public void setJsonValue(Value value, BoundStatement statement)
24
+ {
25
+ if (!value.isArrayValue()) {
26
+ throw new RuntimeException(value.toJson() + " is not array value");
27
+ }
28
+
29
+ TupleType tupleType = cluster.getMetadata().newTupleType(((TupleType) cassandraColumn.getType()).getComponentTypes());
30
+ List<Object> list = ValueConverter.convertList(value.asArrayValue().list());
31
+ TupleValue tupleValue = tupleType.newValue(list.toArray());
32
+ statement.setTupleValue(cassandraColumn.getName(), tupleValue);
33
+ }
34
+ }
@@ -0,0 +1,18 @@
1
+ package org.embulk.output.cassandra.setter;
2
+
3
+ import com.datastax.driver.core.BoundStatement;
4
+ import com.datastax.driver.core.ColumnMetadata;
5
+ import com.datastax.driver.core.utils.UUIDs;
6
+
7
+ public class UuidColumnSetter extends CassandraColumnSetter
8
+ {
9
+ public UuidColumnSetter(ColumnMetadata cassandraColumn)
10
+ {
11
+ super(cassandraColumn);
12
+ }
13
+
14
+ public void setNullValue(BoundStatement statement)
15
+ {
16
+ statement.setUUID(cassandraColumn.getName(), UUIDs.random());
17
+ }
18
+ }
@@ -0,0 +1,63 @@
1
+ package org.embulk.output.cassandra.setter;
2
+
3
+ import com.google.common.collect.Lists;
4
+ import org.msgpack.value.Value;
5
+
6
+ import java.util.List;
7
+ import java.util.Map;
8
+ import java.util.stream.Collectors;
9
+
10
+ public class ValueConverter
11
+ {
12
+ private ValueConverter()
13
+ {
14
+ }
15
+
16
+ public static List<Object> convertList(List<Value> list)
17
+ {
18
+ return Lists.transform(list, (val) -> convertValueToPlain(val));
19
+ }
20
+
21
+ public static Map<Object, Object> convertMap(Map<Value, Value> map)
22
+ {
23
+ return map.entrySet().stream().collect(Collectors.toMap(
24
+ (Map.Entry<Value, Value> entry) -> {
25
+ if (!entry.getKey().isStringValue()) {
26
+ throw new RuntimeException("map key is");
27
+ }
28
+ return entry.getKey().asStringValue().toString();
29
+ },
30
+ (Map.Entry<Value, Value> entry) -> {
31
+ Value val = entry.getValue();
32
+ return convertValueToPlain(val);
33
+ }
34
+ ));
35
+ }
36
+
37
+ private static Object convertValueToPlain(Value val)
38
+ {
39
+ if (val.isNilValue()) {
40
+ return null;
41
+ }
42
+ if (val.isStringValue()) {
43
+ return val.asStringValue().toString();
44
+ }
45
+ if (val.isBooleanValue()) {
46
+ return val.asBooleanValue().getBoolean();
47
+ }
48
+ if (val.isIntegerValue()) {
49
+ return val.asIntegerValue().asLong();
50
+ }
51
+ if (val.isFloatValue()) {
52
+ return val.asFloatValue().toDouble();
53
+ }
54
+ if (val.isArrayValue()) {
55
+ return convertList(val.asArrayValue().list());
56
+ }
57
+ if (val.isMapValue()) {
58
+ return convertMap(val.asMapValue().map());
59
+ }
60
+
61
+ return null;
62
+ }
63
+ }