You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@storm.apache.org by ka...@apache.org on 2017/08/20 22:42:01 UTC

[1/2] storm git commit: STORM-2492: Adding Cassandra Object Mapper statement builder.

Repository: storm
Updated Branches:
  refs/heads/master 0ffbe88e5 -> b87dcc0f7


STORM-2492: Adding Cassandra Object Mapper statement builder.

Allow bolts to emit object annotated with `@Table` and generate CQL for them automatically using the Cassandra object mapper.


Project: http://git-wip-us.apache.org/repos/asf/storm/repo
Commit: http://git-wip-us.apache.org/repos/asf/storm/commit/d16c8212
Tree: http://git-wip-us.apache.org/repos/asf/storm/tree/d16c8212
Diff: http://git-wip-us.apache.org/repos/asf/storm/diff/d16c8212

Branch: refs/heads/master
Commit: d16c82126cecbbe95ff2f8bd377ba8842828fc9c
Parents: 36e2e99
Author: Mads Tandrup <ma...@maetzke-tandrup.dk>
Authored: Mon Apr 24 14:13:59 2017 +0200
Committer: Mads Tandrup <ma...@maetzke-tandrup.dk>
Committed: Sat Jul 22 07:54:20 2017 +0200

----------------------------------------------------------------------
 external/storm-cassandra/README.md              |  34 +++++
 external/storm-cassandra/pom.xml                |   7 +
 .../cassandra/query/ObjectMapperOperation.java  |  36 +++++
 .../ObjectMapperCqlStatementMapperBuilder.java  |  94 +++++++++++++
 .../impl/ObjectMapperCqlStatementMapper.java    | 138 +++++++++++++++++++
 .../storm/lambda/SerializableCallable.java      |  25 ++++
 6 files changed, 334 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/storm/blob/d16c8212/external/storm-cassandra/README.md
----------------------------------------------------------------------
diff --git a/external/storm-cassandra/README.md b/external/storm-cassandra/README.md
index 2837063..ce485fa 100644
--- a/external/storm-cassandra/README.md
+++ b/external/storm-cassandra/README.md
@@ -151,6 +151,40 @@ import static org.apache.storm.cassandra.DynamicStatementBuilder.*
     );
 ```
 
+#### Writing using Cassandra Object Mapper
+
+Instead of defining CQL statements by hand, it is possible to define CQL using cassandra object mapper.
+
+In the topology we need to define what fields in the tuple will hold the operation (INSERT/DELETE) and the actual value:
+
+```java
+    new CassandraWriterBolt(new ObjectMapperCQLStatementMapperBuilder("operation", "model"))
+```
+
+Define some class using object mapper:
+
+```java
+@Table(keyspace = "my_keyspace", name = "my_table")
+public class ValueObject {
+    ...
+}
+```
+
+And in the bolt that emits to the cassandra bolt:
+
+```java
+    collector.emit(new Values(ObjectMapperOperation.SAVE, new ValueObject("foo", "bar")));
+
+```
+##### Custom codecs
+
+To add custom type codes to the mapping you need to define a lambda expression to work around TypeCodecs not being serializable:
+
+```java
+    new ObjectMapperCQLStatementMapperBuilder("operation", "model")
+            .withCodecs(Arrays.asList(() -> new EnumNameCodec<>(MyEnum.class)));
+```
+                                       
 ### How to handle query execution results
 
 The interface *ExecutionResultHandler* can be used to custom how an execution result should be handle.

http://git-wip-us.apache.org/repos/asf/storm/blob/d16c8212/external/storm-cassandra/pom.xml
----------------------------------------------------------------------
diff --git a/external/storm-cassandra/pom.xml b/external/storm-cassandra/pom.xml
index 2e728d4..5d2ba81 100644
--- a/external/storm-cassandra/pom.xml
+++ b/external/storm-cassandra/pom.xml
@@ -71,6 +71,13 @@
         </dependency>
 
         <dependency>
+            <groupId>com.datastax.cassandra</groupId>
+            <artifactId>cassandra-driver-mapping</artifactId>
+            <version>${cassandra.driver.core.version}</version>
+            <scope>provided</scope>
+        </dependency>
+
+        <dependency>
             <groupId>org.apache.cassandra</groupId>
             <artifactId>cassandra-all</artifactId>
             <version>${cassandra.version}</version>

http://git-wip-us.apache.org/repos/asf/storm/blob/d16c8212/external/storm-cassandra/src/main/java/org/apache/storm/cassandra/query/ObjectMapperOperation.java
----------------------------------------------------------------------
diff --git a/external/storm-cassandra/src/main/java/org/apache/storm/cassandra/query/ObjectMapperOperation.java b/external/storm-cassandra/src/main/java/org/apache/storm/cassandra/query/ObjectMapperOperation.java
new file mode 100644
index 0000000..29d7ce9
--- /dev/null
+++ b/external/storm-cassandra/src/main/java/org/apache/storm/cassandra/query/ObjectMapperOperation.java
@@ -0,0 +1,36 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.storm.cassandra.query;
+
+public enum ObjectMapperOperation {
+    /**
+     * Save all fields from the object in the value field.
+     */
+    SAVE,
+    /**
+     * Save all non-null fields from the object in the value field.
+     */
+    SAVE_IGNORE_NULLS,
+    /**
+     * Delete the object in the value field.
+     * Only the primary key fields needs to be set.
+     */
+    DELETE
+}

http://git-wip-us.apache.org/repos/asf/storm/blob/d16c8212/external/storm-cassandra/src/main/java/org/apache/storm/cassandra/query/builder/ObjectMapperCqlStatementMapperBuilder.java
----------------------------------------------------------------------
diff --git a/external/storm-cassandra/src/main/java/org/apache/storm/cassandra/query/builder/ObjectMapperCqlStatementMapperBuilder.java b/external/storm-cassandra/src/main/java/org/apache/storm/cassandra/query/builder/ObjectMapperCqlStatementMapperBuilder.java
new file mode 100644
index 0000000..e418468
--- /dev/null
+++ b/external/storm-cassandra/src/main/java/org/apache/storm/cassandra/query/builder/ObjectMapperCqlStatementMapperBuilder.java
@@ -0,0 +1,94 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.storm.cassandra.query.builder;
+
+import com.datastax.driver.core.TypeCodec;
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.stream.Collectors;
+import org.apache.storm.cassandra.query.CQLStatementBuilder;
+import org.apache.storm.cassandra.query.impl.ObjectMapperCqlStatementMapper;
+import org.apache.storm.lambda.SerializableCallable;
+
+/**
+ * Builds a tuple mapper that is able to map objects annotated with {@link com.datastax.driver.mapping.annotations.Table} to CQL statements.
+ *
+ * <p>
+ *     Needs at least <pre>operationField</pre> and <pre>valueField</pre>.
+ *     Writetime, TTL and consistency level can be specified in optional tuple fields.
+ * </p>
+ */
+public class ObjectMapperCqlStatementMapperBuilder implements CQLStatementBuilder<ObjectMapperCqlStatementMapper>, Serializable {
+    private final String operationField;
+    private final String valueField;
+    private final Collection<Class<?>> udtClasses = new ArrayList<>();
+    private final Collection<SerializableCallable<TypeCodec<?>>> codecProducers = new ArrayList<>();
+    private String timestampField;
+    private String ttlField;
+    private String consistencyLevelField;
+
+    public ObjectMapperCqlStatementMapperBuilder(String operationField, String valueField) {
+        this.operationField = operationField;
+        this.valueField = valueField;
+    }
+
+    /**
+     * Builds an ObjectMapperCqlStatementMapper.
+     */
+    public ObjectMapperCqlStatementMapper build() {
+        List<TypeCodec<?>> codecs = codecProducers.stream().map(codecProducer -> {
+            try {
+                return codecProducer.call();
+            } catch (Exception e) {
+                throw new RuntimeException(e);
+            }
+        }).collect(Collectors.toList());
+        return new ObjectMapperCqlStatementMapper(operationField, valueField, timestampField, ttlField, consistencyLevelField,
+                                                  codecs, udtClasses);
+    }
+
+    public ObjectMapperCqlStatementMapperBuilder withCodecs(List<SerializableCallable<TypeCodec<?>>> codecProducer) {
+        this.codecProducers.addAll(codecProducer);
+        return this;
+    }
+
+    public ObjectMapperCqlStatementMapperBuilder withUdtCodecs(List<Class<?>> udtClass) {
+        this.udtClasses.addAll(udtClass);
+        return this;
+    }
+
+    public ObjectMapperCqlStatementMapperBuilder withTimestampField(String timestampField) {
+        this.timestampField = timestampField;
+        return this;
+    }
+
+    public ObjectMapperCqlStatementMapperBuilder withTtlField(String ttlField) {
+        this.ttlField = ttlField;
+        return this;
+    }
+
+    public ObjectMapperCqlStatementMapperBuilder withConsistencyLevelField(String consistencyLevelField) {
+        this.consistencyLevelField = consistencyLevelField;
+        return this;
+    }
+}
+

http://git-wip-us.apache.org/repos/asf/storm/blob/d16c8212/external/storm-cassandra/src/main/java/org/apache/storm/cassandra/query/impl/ObjectMapperCqlStatementMapper.java
----------------------------------------------------------------------
diff --git a/external/storm-cassandra/src/main/java/org/apache/storm/cassandra/query/impl/ObjectMapperCqlStatementMapper.java b/external/storm-cassandra/src/main/java/org/apache/storm/cassandra/query/impl/ObjectMapperCqlStatementMapper.java
new file mode 100644
index 0000000..d3841fb
--- /dev/null
+++ b/external/storm-cassandra/src/main/java/org/apache/storm/cassandra/query/impl/ObjectMapperCqlStatementMapper.java
@@ -0,0 +1,138 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.storm.cassandra.query.impl;
+
+import com.datastax.driver.core.CodecRegistry;
+import com.datastax.driver.core.ConsistencyLevel;
+import com.datastax.driver.core.Session;
+import com.datastax.driver.core.Statement;
+import com.datastax.driver.core.TypeCodec;
+import com.datastax.driver.mapping.Mapper;
+import com.datastax.driver.mapping.Mapper.Option;
+import com.datastax.driver.mapping.MappingManager;
+import com.google.common.base.Preconditions;
+import java.time.Duration;
+import java.time.Instant;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+import java.util.WeakHashMap;
+import org.apache.storm.cassandra.query.CQLStatementTupleMapper;
+import org.apache.storm.cassandra.query.ObjectMapperOperation;
+import org.apache.storm.tuple.ITuple;
+
+/**
+ * Tuple mapper that is able to map objects annotated with {@link com.datastax.driver.mapping.annotations.Table} to CQL statements
+ */
+public class ObjectMapperCqlStatementMapper implements CQLStatementTupleMapper {
+    private static final Map<Session, MappingManager> mappingManagers = new WeakHashMap<>();
+
+    private final String operationField;
+    private final String valueField;
+    private final String timestampField;
+    private final String ttlField;
+    private final String consistencyLevelField;
+    private final Collection<TypeCodec<?>> codecs;
+    private final Collection<Class<?>> udtClasses;
+
+    public ObjectMapperCqlStatementMapper(String operationField, String valueField, String timestampField, String ttlField, String consistencyLevelField, Collection<TypeCodec<?>> codecs, Collection<Class<?>> udtClasses) {
+        Preconditions.checkNotNull(operationField, "Operation field must not be null");
+        Preconditions.checkNotNull(valueField, "Value field should not be null");
+        this.operationField = operationField;
+        this.valueField = valueField;
+        this.timestampField = timestampField;
+        this.ttlField = ttlField;
+        this.consistencyLevelField = consistencyLevelField;
+        this.codecs = codecs;
+        this.udtClasses = udtClasses;
+    }
+
+    @Override
+    public List<Statement> map(Map map, Session session, ITuple tuple) {
+        final ObjectMapperOperation operation = (ObjectMapperOperation)tuple.getValueByField(operationField);
+
+        Preconditions.checkNotNull(operation, "Operation must not be null");
+
+        final Object value = tuple.getValueByField(valueField);
+        final Object timestampObject = timestampField != null ? tuple.getValueByField(timestampField) : null;
+        final Object ttlObject = ttlField != null ? tuple.getValueByField(ttlField) : null;
+        final ConsistencyLevel consistencyLevel = consistencyLevelField != null ? (ConsistencyLevel) tuple.getValueByField(consistencyLevelField) : null;
+
+        final Class<?> valueClass = value.getClass();
+
+        final Mapper mapper = getMappingManager(session).mapper(valueClass);
+
+        Collection<Option> options = new ArrayList<>();
+
+        if (timestampObject != null) {
+            if (timestampObject instanceof Number) {
+                options.add(Option.timestamp(((Number) timestampObject).longValue()));
+            } else if (timestampObject instanceof Instant) {
+                Instant timestamp = (Instant) timestampObject;
+                options.add(Option.timestamp(timestamp.getEpochSecond() * 1000_0000l + timestamp.getNano() / 1000l));
+            }
+        }
+
+        if (ttlObject != null) {
+            if (ttlObject instanceof Number) {
+                options.add(Option.ttl(((Number) ttlObject).intValue()));
+            } else if (ttlObject instanceof Duration) {
+                Duration ttl = (Duration) ttlObject;
+                options.add(Option.ttl((int) ttl.getSeconds()));
+            }
+        }
+
+        if (consistencyLevel != null) {
+            options.add(Option.consistencyLevel(consistencyLevel));
+        }
+
+        if (operation == ObjectMapperOperation.SAVE) {
+            options.add(Option.saveNullFields(true));
+            return Arrays.asList(mapper.saveQuery(value, options.toArray(new Option[options.size()])));
+        } else if (operation == ObjectMapperOperation.SAVE_IGNORE_NULLS) {
+            options.add(Option.saveNullFields(false));
+            return Arrays.asList(mapper.saveQuery(value, options.toArray(new Option[options.size()])));
+        } else if (operation == ObjectMapperOperation.DELETE) {
+            return Arrays.asList(mapper.deleteQuery(value, options.toArray(new Option[options.size()])));
+        } else {
+            throw new UnsupportedOperationException("Unknown operation: " + operation);
+        }
+    }
+
+    private MappingManager getMappingManager(Session session) {
+        synchronized (mappingManagers) {
+            MappingManager mappingManager = mappingManagers.get(session);
+            if (mappingManager == null) {
+                mappingManager = new MappingManager(session);
+                mappingManagers.put(session, mappingManager);
+                CodecRegistry codecRegistry = session.getCluster().getConfiguration().getCodecRegistry();
+                for (TypeCodec<?> codec : codecs) {
+                    codecRegistry.register(codec);
+                }
+                for (Class<?> udtClass : udtClasses) {
+                    mappingManager.udtCodec(udtClass);
+                }
+            }
+            return mappingManager;
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/storm/blob/d16c8212/storm-client/src/jvm/org/apache/storm/lambda/SerializableCallable.java
----------------------------------------------------------------------
diff --git a/storm-client/src/jvm/org/apache/storm/lambda/SerializableCallable.java b/storm-client/src/jvm/org/apache/storm/lambda/SerializableCallable.java
new file mode 100644
index 0000000..52fe739
--- /dev/null
+++ b/storm-client/src/jvm/org/apache/storm/lambda/SerializableCallable.java
@@ -0,0 +1,25 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.storm.lambda;
+
+import java.io.Serializable;
+import java.util.concurrent.Callable;
+
+public interface SerializableCallable<R> extends Callable<R>, Serializable {
+}


[2/2] storm git commit: Merge branch 'cassandra-object-mapper' of https://github.com/tandrup/storm into STORM-2492-merge

Posted by ka...@apache.org.
Merge branch 'cassandra-object-mapper' of https://github.com/tandrup/storm into STORM-2492-merge


Project: http://git-wip-us.apache.org/repos/asf/storm/repo
Commit: http://git-wip-us.apache.org/repos/asf/storm/commit/b87dcc0f
Tree: http://git-wip-us.apache.org/repos/asf/storm/tree/b87dcc0f
Diff: http://git-wip-us.apache.org/repos/asf/storm/diff/b87dcc0f

Branch: refs/heads/master
Commit: b87dcc0f7c28e6bc5de77dfffb95e506f8eeba45
Parents: 0ffbe88 d16c821
Author: Jungtaek Lim <ka...@gmail.com>
Authored: Mon Aug 21 07:41:12 2017 +0900
Committer: Jungtaek Lim <ka...@gmail.com>
Committed: Mon Aug 21 07:41:12 2017 +0900

----------------------------------------------------------------------
 external/storm-cassandra/README.md              |  34 +++++
 external/storm-cassandra/pom.xml                |   7 +
 .../cassandra/query/ObjectMapperOperation.java  |  36 +++++
 .../ObjectMapperCqlStatementMapperBuilder.java  |  94 +++++++++++++
 .../impl/ObjectMapperCqlStatementMapper.java    | 138 +++++++++++++++++++
 .../storm/lambda/SerializableCallable.java      |  25 ++++
 6 files changed, 334 insertions(+)
----------------------------------------------------------------------