You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@apex.apache.org by pr...@apache.org on 2016/11/22 05:18:03 UTC
[1/2] apex-malhar git commit: APEXMALHAR-2181 Cassandra Upsert
Operator
Repository: apex-malhar
Updated Branches:
refs/heads/master 05a7ca3e4 -> 664257b4a
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/664257b4/contrib/src/main/java/com/datatorrent/contrib/cassandra/ConnectionStateManager.java
----------------------------------------------------------------------
diff --git a/contrib/src/main/java/com/datatorrent/contrib/cassandra/ConnectionStateManager.java b/contrib/src/main/java/com/datatorrent/contrib/cassandra/ConnectionStateManager.java
new file mode 100644
index 0000000..cfc2b46
--- /dev/null
+++ b/contrib/src/main/java/com/datatorrent/contrib/cassandra/ConnectionStateManager.java
@@ -0,0 +1,477 @@
+/**
+ * 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 com.datatorrent.contrib.cassandra;
+
+import java.io.Serializable;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Set;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.apache.hadoop.classification.InterfaceStability;
+
+import com.datastax.driver.core.Cluster;
+import com.datastax.driver.core.ConsistencyLevel;
+import com.datastax.driver.core.Host;
+import com.datastax.driver.core.Metadata;
+import com.datastax.driver.core.ProtocolVersion;
+import com.datastax.driver.core.QueryOptions;
+import com.datastax.driver.core.Session;
+import com.datastax.driver.core.policies.DCAwareRoundRobinPolicy;
+import com.datastax.driver.core.policies.DowngradingConsistencyRetryPolicy;
+import com.datastax.driver.core.policies.ExponentialReconnectionPolicy;
+import com.datastax.driver.core.policies.LoadBalancingPolicy;
+import com.datastax.driver.core.policies.ReconnectionPolicy;
+import com.datastax.driver.core.policies.RetryPolicy;
+import com.datastax.driver.core.policies.TokenAwarePolicy;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * This is used to specify the connection parameters that is used to process mutations for the
+ * {@link AbstractUpsertOutputOperator}.
+ * The Connection can only be set for one table in a given space and as such is used to define the following
+ * - Connection Parameters
+ * - Defaults to be used in case the execution context tuple {@link UpsertExecutionContext} does not want to
+ * set one explicitly.
+ * Note that the {@link ConnectionBuilder} is used to build an instance of the ConnectionStateManager.
+ * An instance of this class is typically instantiated by implementing the following inside
+ * {@link AbstractUpsertOutputOperator} withConnectionBuilder() method.
+ * {@code
+ @Override
+ public ConnectionStateManager.ConnectionBuilder withConnectionBuilder()
+ {
+ return ConnectionStateManager.withNewBuilder()
+ .withSeedNodes("localhost") // format of host1:port;host2:port;host3:port
+ .withClusterNameAs("Test Cluster") // mandatory param
+ .withDCNameAs("datacenter1") // mandatory param
+ .withTableNameAs("users") // mandatory param
+ .withKeySpaceNameAs("unittests") // mandatory param
+ .withdefaultConsistencyLevel(ConsistencyLevel.LOCAL_ONE); // Set if required. Default of LOCAL_QUORUM
+ // Rest of the configs are initialized to sane defaults
+ }
+ * }
+ * Please refer {@link ConnectionBuilder} for details about parameters that can be used to define the connection
+ * and its default behaviour
+ */
+@InterfaceStability.Evolving
+public class ConnectionStateManager implements AutoCloseable, Serializable
+{
+
+ private static final long serialVersionUID = -6024334738016015213L;
+ private static final long DEFAULT_MAX_DELAY_MS = 30000L;
+ private static final long DEFAULT_BASE_DELAY_MS = 10000L;
+ // Cassandra cluster name
+ private final String clusterName;
+ // Cassandra DC Name
+ private final String dcName;
+ // Seeds nodes. The format for specifying the host names are host1:port;host2:port
+ private final String seedNodesStr;
+ private final long baseDelayMs;
+ private final long maxDelayMs;
+ // Other operational constraints
+ private final transient LoadBalancingPolicy loadBalancingPolicy;
+ private final transient RetryPolicy retryPolicy;
+ private final transient QueryOptions queryOptions;
+ private final transient ReconnectionPolicy reconnectionPolicy;
+ private final transient ProtocolVersion protocolVersion;
+ private transient Logger LOG = LoggerFactory.getLogger(ConnectionStateManager.class);
+ // Connection specific config elements
+ private transient Cluster cluster;
+ // Session specific metadata
+ private transient Session session;
+ private String keyspaceName;
+ private String tableName;
+ private int defaultTtlInSecs;
+ private ConsistencyLevel defaultConsistencyLevel;
+ // Standard defaults
+ private boolean isTTLSet = false;
+
+ private ConnectionStateManager(final ConnectionBuilder connectionBuilder)
+ {
+ checkNotNull(connectionBuilder, "Connection Builder passed in as Null");
+ checkNotNull(connectionBuilder.clusterName, "Cluster Name not set for Cassandra");
+ checkNotNull(connectionBuilder.dcName, "DataCenter Name not set for Cassandra");
+ checkNotNull(connectionBuilder.seedNodesStr,
+ "Seed nodes not set for Cassandra. Pattern is host1:port;host2:port");
+ checkNotNull(connectionBuilder.keyspaceName, "Keyspace Name not set for Cassandra");
+ checkNotNull(connectionBuilder.tableName, "Table Name not set for Cassandra");
+ //Required params
+ this.clusterName = connectionBuilder.clusterName;
+ this.dcName = connectionBuilder.dcName;
+ this.seedNodesStr = connectionBuilder.seedNodesStr;
+ this.keyspaceName = connectionBuilder.keyspaceName;
+ this.tableName = connectionBuilder.tableName;
+
+ // optional params
+ if (connectionBuilder.maxDelayMs != null) {
+ maxDelayMs = connectionBuilder.maxDelayMs;
+ } else {
+ maxDelayMs = DEFAULT_MAX_DELAY_MS; // 30 seconds
+ }
+ if (connectionBuilder.baseDelayMs != null) {
+ baseDelayMs = connectionBuilder.baseDelayMs;
+ } else {
+ baseDelayMs = DEFAULT_BASE_DELAY_MS; // 10 seconds
+ }
+ if (connectionBuilder.defaultTtlInSecs != null) {
+ defaultTtlInSecs = connectionBuilder.defaultTtlInSecs;
+ isTTLSet = true;
+ }
+ if (connectionBuilder.defaultConsistencyLevel != null) {
+ defaultConsistencyLevel = connectionBuilder.defaultConsistencyLevel;
+ } else {
+ defaultConsistencyLevel = ConsistencyLevel.LOCAL_QUORUM;
+ }
+ if (connectionBuilder.loadBalancingPolicy != null) {
+ loadBalancingPolicy = connectionBuilder.loadBalancingPolicy;
+ } else {
+ loadBalancingPolicy = new TokenAwarePolicy(
+ DCAwareRoundRobinPolicy.builder()
+ .withLocalDc(dcName)
+ .build());
+ }
+ if (connectionBuilder.retryPolicy != null) {
+ this.retryPolicy = connectionBuilder.retryPolicy;
+ } else {
+ retryPolicy = DowngradingConsistencyRetryPolicy.INSTANCE;
+ }
+ if (connectionBuilder.queryOptions != null) {
+ this.queryOptions = connectionBuilder.queryOptions;
+ } else {
+ this.queryOptions = new QueryOptions().setConsistencyLevel(defaultConsistencyLevel);
+ }
+ if (connectionBuilder.reconnectionPolicy != null) {
+ this.reconnectionPolicy = connectionBuilder.reconnectionPolicy;
+ } else {
+ this.reconnectionPolicy = new ExponentialReconnectionPolicy(baseDelayMs, maxDelayMs);
+ }
+ if (connectionBuilder.protocolVersion != null) {
+ this.protocolVersion = connectionBuilder.protocolVersion;
+ } else {
+ this.protocolVersion = ProtocolVersion.NEWEST_SUPPORTED;
+ }
+ establishSessionWithPolicies();
+ }
+
+ public static ConnectionBuilder withNewBuilder()
+ {
+ return new ConnectionBuilder();
+ }
+
+ private void establishSessionWithPolicies()
+ {
+ Cluster.Builder clusterBuilder = Cluster.builder();
+ String[] seedNodesSplit = seedNodesStr.split(";");
+ Collection<InetAddress> allSeeds = new ArrayList<>();
+ Set<String> allKnownPorts = new HashSet<>();
+ for (String seedNode : seedNodesSplit) {
+ String[] nodeAndPort = seedNode.split(":");
+ if (nodeAndPort.length > 1) {
+ allKnownPorts.add(nodeAndPort[1]);
+ }
+ try {
+ allSeeds.add(InetAddress.getByName(nodeAndPort[0]));
+ } catch (UnknownHostException e) {
+ LOG.error(" Error while trying to initialize the seed brokers for the cassandra cluster " + e.getMessage(), e);
+ }
+ }
+ clusterBuilder = clusterBuilder.addContactPoints(allSeeds);
+ clusterBuilder
+ .withClusterName(clusterName)
+ // We can fail if all of the nodes die in the local DC
+ .withLoadBalancingPolicy(loadBalancingPolicy)
+ // Want Strong consistency
+ .withRetryPolicy(retryPolicy)
+ // Tolerate some nodes down
+ .withQueryOptions(queryOptions)
+ // Keep establishing connections after detecting a node down
+ .withReconnectionPolicy(reconnectionPolicy);
+ // Finally initialize the cluster info
+ if (allKnownPorts.size() > 0) {
+ int shortlistedPort = Integer.parseInt(allKnownPorts.iterator().next());
+ clusterBuilder = clusterBuilder.withPort(shortlistedPort);
+ }
+ cluster = clusterBuilder.build();
+ Metadata metadata = cluster.getMetadata();
+ LOG.info("Connected to cluster: \n" + metadata.getClusterName());
+ for (Host host : metadata.getAllHosts()) {
+ LOG.info(String.format("Datacenter: %s; Host: %s; Rack: %s\n",
+ host.getDatacenter(), host.getAddress(), host.getRack()));
+ }
+ session = cluster.connect(keyspaceName);
+ }
+
+ @Override
+ public void close()
+ {
+ if (session != null) {
+ session.close();
+ }
+ if (cluster != null) {
+ cluster.close();
+ }
+ }
+
+ public Cluster getCluster()
+ {
+ return cluster;
+ }
+
+ public void setCluster(Cluster cluster)
+ {
+ this.cluster = cluster;
+ }
+
+ public Session getSession()
+ {
+ return session;
+ }
+
+ public void setSession(Session session)
+ {
+ this.session = session;
+ }
+
+ public String getKeyspaceName()
+ {
+ return keyspaceName;
+ }
+
+ public void setKeyspaceName(String keyspaceName)
+ {
+ this.keyspaceName = keyspaceName;
+ }
+
+ public String getTableName()
+ {
+ return tableName;
+ }
+
+ public void setTableName(String tableName)
+ {
+ this.tableName = tableName;
+ }
+
+ public int getDefaultTtlInSecs()
+ {
+ return defaultTtlInSecs;
+ }
+
+ public void setDefaultTtlInSecs(int defaultTtlInSecs)
+ {
+ this.defaultTtlInSecs = defaultTtlInSecs;
+ }
+
+ public boolean isTTLSet()
+ {
+ return isTTLSet;
+ }
+
+ public void setTTLSet(boolean TTLSet)
+ {
+ isTTLSet = TTLSet;
+ }
+
+ public static class ConnectionBuilder
+ {
+
+ private String clusterName;
+ private String dcName;
+ private String seedNodesStr;
+ private Long baseDelayMs; // Class to enable check for nulls and is optional
+ private Long maxDelayMs; // Class to enable check for nulls and is optional
+
+ private String keyspaceName;
+ private String tableName;
+ private Integer defaultTtlInSecs; // Class to enable checks for nulls and is optional
+ private ConsistencyLevel defaultConsistencyLevel;
+
+ private LoadBalancingPolicy loadBalancingPolicy;
+ private RetryPolicy retryPolicy;
+ private QueryOptions queryOptions;
+ private ReconnectionPolicy reconnectionPolicy;
+ private ProtocolVersion protocolVersion;
+
+ public static final String CLUSTER_NAME_IN_PROPS_FILE = "cluster.name";
+ public static final String DC_NAME_IN_PROPS_FILE = "dc.name";
+ public static final String KEYSPACE_NAME_IN_PROPS_FILE = "keyspace.name";
+ public static final String TABLE_NAME_IN_PROPS_FILE = "table.name";
+ public static final String SEEDNODES_IN_PROPS_FILE = "seednodes";
+
+ public ConnectionBuilder withClusterNameAs(String clusterName)
+ {
+ this.clusterName = clusterName;
+ return this;
+ }
+
+ public ConnectionBuilder withDCNameAs(String dcName)
+ {
+ this.dcName = dcName;
+ return this;
+ }
+
+ /**
+ * Used to specify the seed nodes of the target cassandra cluster. Format is
+ * host1:port;host2:port;host3:port
+ * @param seedNodesStr
+ * @return The builder instance as initially created updated with this value
+ */
+ public ConnectionBuilder withSeedNodes(String seedNodesStr)
+ {
+ this.seedNodesStr = seedNodesStr;
+ return this;
+ }
+
+ /**
+ * Used to specify the base delay while trying to set a Connection attempt policy
+ * @param baseDelayMillis
+ * @return The builder instance as initially created updated with this value
+ */
+ public ConnectionBuilder withBaseDelayMillis(long baseDelayMillis)
+ {
+ this.baseDelayMs = baseDelayMillis;
+ return this;
+ }
+
+ /**
+ * Used to specify the maximum time that can elapse before which a connection is given up as a failure attmept
+ * @param maxDelayMillis
+ * @return The builder instance as initially created updated with this value
+ */
+ public ConnectionBuilder withMaxDelayMillis(long maxDelayMillis)
+ {
+ this.maxDelayMs = maxDelayMillis;
+ return this;
+ }
+
+ public ConnectionBuilder withKeySpaceNameAs(String keyspaceName)
+ {
+ this.keyspaceName = keyspaceName;
+ return this;
+ }
+
+ public ConnectionBuilder withTableNameAs(String tableName)
+ {
+ this.tableName = tableName;
+ return this;
+ }
+
+ public ConnectionBuilder withdefaultTTL(Integer defaultTtlInSecs)
+ {
+ this.defaultTtlInSecs = defaultTtlInSecs;
+ return this;
+ }
+
+ /**
+ * Used to specify the default consistency level when executing the mutations on the cluster.
+ * Default if not set is LOCAL_QUORUM. Can be overriden at the tuple level using {@link UpsertExecutionContext}
+ * @param consistencyLevel
+ * @return The builder instance as initially created updated with this value
+ */
+ public ConnectionBuilder withdefaultConsistencyLevel(ConsistencyLevel consistencyLevel)
+ {
+ this.defaultConsistencyLevel = consistencyLevel;
+ return this;
+ }
+
+ /**
+ * Used to define how the nodes in the cluster will be contacted for executing a mutation.
+ * The following is the default behaviour if not set.
+ * 1. Use a TokenAware approach i.e. the row key is used to decide the right node to execute the mutation
+ * on the target cassandra node. i.e. One of the R-1 replicas is used as the coordinator node.
+ * This effectively balances the traffic onto all nodes of the cassandra cluster for the given
+ * Operator instance. Of course this assumes the keys are evenly distributed in the cluster
+ * which is normally the case
+ * 2. Overlay TokenAware with DC aware approach - The above token aware approach is further segmented to use only
+ * the local DC for the mutation executions. Cassandras multi-DC execution will take care of the cross DC
+ * replication thus achieving the lowest possible latencies for the given mutation of writes.
+ *
+ * Using this effectively removes the need for an extra implementation of the Partitioning logic of the Operator
+ * Nor would we need any extra logic ( for most use cases ) for dynamic partitioning implementations as the
+ * underlying driver normalizes the traffic pattern anyways.
+ * @param loadBalancingPolicy
+ * @return The builder instance as initially created updated with this value
+ */
+ public ConnectionBuilder withLoadBalancingPolicy(LoadBalancingPolicy loadBalancingPolicy)
+ {
+ this.loadBalancingPolicy = loadBalancingPolicy;
+ return this;
+ }
+
+ /**
+ * Used to specify how queries will need to be retried in case the current in progress one fails.
+ * The default is to use a DowngradingConsistency Policy i.e. first LOCAL_QUORUM is attempted and if
+ * there is a failure because less than RF/2-1 nodes are alive, it automatically switches to the Consistency Level
+ * of LOCAL_ONE and so on. ( and hope that hint windows take care of the rest when the nodes come back up )
+ * @param retryPolicy
+ * @return The builder instance as initially created updated with this value
+ */
+ public ConnectionBuilder withRetryPolicy(RetryPolicy retryPolicy)
+ {
+ this.retryPolicy = retryPolicy;
+ return this;
+ }
+
+ /**
+ * Used to set various aspects for executing a given query / mutation.
+ * The default is to use LOCAL_QUORUM consistency for all mutation queries
+ * @param queryOptions
+ * @return The builder instance as initially created updated with this value
+ */
+ public ConnectionBuilder withQueryOptions(QueryOptions queryOptions)
+ {
+ this.queryOptions = queryOptions;
+ return this;
+ }
+
+ /**
+ * Used to decide how to establish a connection to the cluster in case the current one fails.
+ * The default if not set is to use an ExponentialRetry Policy.
+ * The baseDelay and maxDelay are the two time windows that are used to specify the retry attempts
+ * in an exponential manner
+ * @param reconnectionPolicy
+ * @return The builder instance as initially created updated with this value
+ */
+ public ConnectionBuilder withReconnectionPolicy(ReconnectionPolicy reconnectionPolicy)
+ {
+ this.reconnectionPolicy = reconnectionPolicy;
+ return this;
+ }
+
+ public ConnectionBuilder withProtocolVersion(ProtocolVersion protocolVersion)
+ {
+ this.protocolVersion = protocolVersion;
+ return this;
+ }
+
+ protected ConnectionStateManager initialize()
+ {
+ ConnectionStateManager operatorConnection = new ConnectionStateManager(this);
+ return operatorConnection;
+ }
+
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/664257b4/contrib/src/main/java/com/datatorrent/contrib/cassandra/UpsertExecutionContext.java
----------------------------------------------------------------------
diff --git a/contrib/src/main/java/com/datatorrent/contrib/cassandra/UpsertExecutionContext.java b/contrib/src/main/java/com/datatorrent/contrib/cassandra/UpsertExecutionContext.java
new file mode 100644
index 0000000..66d0b6c
--- /dev/null
+++ b/contrib/src/main/java/com/datatorrent/contrib/cassandra/UpsertExecutionContext.java
@@ -0,0 +1,210 @@
+/**
+ * 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 com.datatorrent.contrib.cassandra;
+
+import com.datastax.driver.core.ConsistencyLevel;
+
+/**
+ * Each mutation in the cassandra table is decided by a context which can be passed to the Operator at runtime.
+ * This class represents such a context. The context is to be set by the upstream operator and is used to
+ * define how to mutate a row by passing it as a tuple to the {@link AbstractUpsertOutputOperator}
+ * The row to be mutated is represented by the payload represented by the template parameter T
+ * The following aspects of the mutation can be controlled by the context tuple.
+ * 1. Collection Mutation Style
+ * 2. List Placement style
+ * 3. Null Handling styles for partial mutations of a given row
+ * 4. Update only if a Primary Key exists
+ * 5. Override the TTL that is set at the default connection config.
+ * See {@link ConnectionStateManager.ConnectionBuilder} to set the default TTL for all payload executions
+ * 6. Override the default Consistency level to be used for the current mutation
+ * See {@link ConnectionStateManager.ConnectionBuilder} for setting default consistency.
+ */
+public class UpsertExecutionContext<T>
+{
+
+ private CollectionMutationStyle collectionMutationStyle = CollectionMutationStyle.UNDEFINED;
+
+
+ private ListPlacementStyle listPlacementStyle = ListPlacementStyle.UNDEFINED;
+
+ private NullHandlingMutationStyle nullHandlingMutationStyle = NullHandlingMutationStyle.UNDEFINED;
+
+ private int overridingTTL;
+
+ private ConsistencyLevel overridingConsistencyLevel;
+
+ private boolean isTtlOverridden = false;
+
+ private boolean isOverridingConsistencyLevelSet = false;
+
+ private boolean updateOnlyIfPrimaryKeyExists = false;
+
+ private T payload;
+
+ public T getPayload()
+ {
+ return payload;
+ }
+
+ public void setPayload(T payload)
+ {
+ this.payload = payload;
+ }
+
+ /**
+ * Represents if the collection object inside the pojo needs to be added or remove
+ * from the table column represented by the POJO field. Refer {@link CollectionMutationStyle} for possible value
+ * If set as UNDEFINED, the default behaviour is to add the entry to the collection column
+ * @return
+ */
+ public CollectionMutationStyle getCollectionMutationStyle()
+ {
+ return collectionMutationStyle;
+ }
+
+ public void setCollectionMutationStyle(CollectionMutationStyle collectionMutationStyle)
+ {
+ this.collectionMutationStyle = collectionMutationStyle;
+ }
+
+ /**
+ * Represents how the POJO List type field is mutated on the cassandra column which is of type
+ * list. Note that this is only applicable for list collections and decides whether the POJO element ( of type list)
+ * will be placed before
+ * @return The List Placement style that is used to execute this mutation. Defaults to Append mode if not set
+ */
+ public ListPlacementStyle getListPlacementStyle()
+ {
+ return listPlacementStyle;
+ }
+
+ public void setListPlacementStyle(ListPlacementStyle listPlacementStyle)
+ {
+ this.listPlacementStyle = listPlacementStyle;
+ }
+
+ /**
+ * This decides how to handle nulls for POJO fields. This comes in handy when one would want to
+ * avoid a read of the column and then mutate the row i.e. in other words if the POJO is representing only a subset
+ * of the columns that needs to be mutated on the table. In such scenarios, set NullHandlingMutation Style to
+ * IGNORE_NULL_COLUMNS which would make the operator avoid an update on the column and thus preventing over-writing
+ * an existing value
+ * @return The Null handling style that is going to be used for the execution context. Defaults to Set Nulls and not
+ * ignore them
+ * */
+ public NullHandlingMutationStyle getNullHandlingMutationStyle()
+ {
+ return nullHandlingMutationStyle;
+ }
+
+ public void setNullHandlingMutationStyle(NullHandlingMutationStyle nullHandlingMutationStyle)
+ {
+ this.nullHandlingMutationStyle = nullHandlingMutationStyle;
+ }
+
+ /**
+ * This decides if we want to override the default TTL if at all set in the
+ * {@link com.datatorrent.contrib.cassandra.ConnectionStateManager.ConnectionBuilder} that is used to execute a
+ * mutation. Note that TTLs are not mandatory for mutations.
+ * Also it is supported to have TTLs only for the current execution context but not set a default at the
+ * connection state manager level
+ * Unit of time is seconds
+ * @return The overriding TTL that will be used for the given execution context.
+ */
+ public int getOverridingTTL()
+ {
+ return overridingTTL;
+ }
+
+ public void setOverridingTTL(int overridingTTL)
+ {
+ this.overridingTTL = overridingTTL;
+ isTtlOverridden = true;
+ }
+
+ public boolean isTtlOverridden()
+ {
+ return isTtlOverridden;
+ }
+
+ /**
+ * Used to override the default consistency level that is set at the {@link ConnectionStateManager } level
+ * The default is to use LOCAL_QUORUM. This can be overridden at the execution context level on a per
+ * tuple basis.
+ * @return The consistency level that would be used to execute the current payload mutation
+ * */
+ public ConsistencyLevel getOverridingConsistencyLevel()
+ {
+ return overridingConsistencyLevel;
+ }
+
+ public void setOverridingConsistencyLevel(ConsistencyLevel overridingConsistencyLevel)
+ {
+ this.overridingConsistencyLevel = overridingConsistencyLevel;
+ isOverridingConsistencyLevelSet = true;
+ }
+
+ public boolean isOverridingConsistencyLevelSet()
+ {
+ return isOverridingConsistencyLevelSet;
+ }
+
+ public boolean isUpdateOnlyIfPrimaryKeyExists()
+ {
+ return updateOnlyIfPrimaryKeyExists;
+ }
+
+ /**
+ * Used to execute a mutation only if the primary key exists. This can be used to conditionally execute
+ * a mutation i.e. if only the primary key exists. This can be used to force an "UPDATE" only use case
+ * for the current mutation.
+ * @param updateOnlyIfPrimaryKeyExists
+ */
+ public void setUpdateOnlyIfPrimaryKeyExists(boolean updateOnlyIfPrimaryKeyExists)
+ {
+ this.updateOnlyIfPrimaryKeyExists = updateOnlyIfPrimaryKeyExists;
+ }
+
+ enum CollectionMutationStyle implements BaseMutationStyle
+ {
+ ADD_TO_EXISTING_COLLECTION,
+ REMOVE_FROM_EXISTING_COLLECTION,
+ UNDEFINED
+ }
+
+ enum ListPlacementStyle implements BaseMutationStyle
+ {
+ APPEND_TO_EXISTING_LIST,
+ PREPEND_TO_EXISTING_LIST,
+ UNDEFINED
+ }
+
+ enum NullHandlingMutationStyle implements BaseMutationStyle
+ {
+ IGNORE_NULL_COLUMNS,
+ SET_NULL_COLUMNS,
+ UNDEFINED
+ }
+
+ interface BaseMutationStyle
+ {
+
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/664257b4/contrib/src/test/java/com/datatorrent/contrib/cassandra/AbstractUpsertOutputOperatorCodecsTest.java
----------------------------------------------------------------------
diff --git a/contrib/src/test/java/com/datatorrent/contrib/cassandra/AbstractUpsertOutputOperatorCodecsTest.java b/contrib/src/test/java/com/datatorrent/contrib/cassandra/AbstractUpsertOutputOperatorCodecsTest.java
new file mode 100644
index 0000000..87c783f
--- /dev/null
+++ b/contrib/src/test/java/com/datatorrent/contrib/cassandra/AbstractUpsertOutputOperatorCodecsTest.java
@@ -0,0 +1,476 @@
+/**
+ * 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 com.datatorrent.contrib.cassandra;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.datastax.driver.core.ConsistencyLevel;
+import com.datastax.driver.core.ResultSet;
+import com.datastax.driver.core.Row;
+import com.datatorrent.api.Attribute;
+import com.datatorrent.api.Context;
+import com.datatorrent.api.DAG;
+import com.datatorrent.lib.helper.OperatorContextTestHelper;
+import com.datatorrent.lib.helper.TestPortContext;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+public class AbstractUpsertOutputOperatorCodecsTest
+{
+
+ /***
+ * The schema that is used
+ *
+ *
+ *
+ *
+ * CREATE KEYSPACE unittests
+ * WITH replication = {
+ * 'class' : 'SimpleStrategy',
+ * 'replication_factor' : 1
+ * };
+ *
+ * CREATE TYPE unittests.address (
+ * street text,
+ * city text,
+ * zip_code int,
+ * phones set<text>
+ * );
+ *
+ * CREATE TYPE unittests.fullname ( firstname text, lastname text );
+ *
+ * CREATE TABLE unittests.users (
+ * userid text PRIMARY KEY,
+ * username FROZEN<fullname>,
+ * emails set<text>,
+ * top_scores list<int>,
+ * todo map<timestamp, text>,
+ * siblings tuple<int, text,text>,
+ * currentaddress FROZEN<address>,
+ * previousnames FROZEN<list<fullname>>
+ * );
+ *
+ * CREATE TABLE unittests.userupdates (
+ * userid text PRIMARY KEY,
+ * updatecount counter
+ * );
+ *
+ * CREATE TABLE unittests.userstatus (
+ * userid text,
+ * day int,
+ * month int,
+ * year int,
+ * employeeid text,
+ * currentstatus text,
+ * PRIMARY KEY ((userid,day,month,year), employeeid));
+ */
+
+ public static final String APP_ID = "TestCassandraUpsertOperator";
+ public static final int OPERATOR_ID_FOR_USER_UPSERTS = 0;
+
+
+ UserUpsertOperator userUpsertOperator = null;
+ OperatorContextTestHelper.TestIdOperatorContext contextForUserUpsertOperator;
+ TestPortContext testPortContextForUserUpserts;
+
+
+ @Before
+ public void setupApexContexts() throws Exception
+ {
+ Attribute.AttributeMap.DefaultAttributeMap attributeMap = new Attribute.AttributeMap.DefaultAttributeMap();
+ attributeMap.put(DAG.APPLICATION_ID, APP_ID);
+ contextForUserUpsertOperator = new OperatorContextTestHelper.TestIdOperatorContext(OPERATOR_ID_FOR_USER_UPSERTS,
+ attributeMap);
+ userUpsertOperator = new UserUpsertOperator();
+
+ Attribute.AttributeMap.DefaultAttributeMap portAttributes = new Attribute.AttributeMap.DefaultAttributeMap();
+ portAttributes.put(Context.PortContext.TUPLE_CLASS, User.class);
+ testPortContextForUserUpserts = new TestPortContext(portAttributes);
+
+ userUpsertOperator.setup(contextForUserUpsertOperator);
+ userUpsertOperator.activate(contextForUserUpsertOperator);
+ userUpsertOperator.input.setup(testPortContextForUserUpserts);
+ }
+
+ @Test
+ public void testActivateForSchemaDetection() throws Exception
+ {
+ assertEquals(12, userUpsertOperator.getPreparedStatementTypes().size());
+ assertEquals(
+ " UPDATE unittests.users SET currentaddress = :currentaddress, emails = emails - :emails, " +
+ "todo = todo - :todo, top_scores = top_scores - :top_scores, siblings = :siblings, " +
+ "previousnames = :previousnames, username = :username WHERE userid = :userid",
+ userUpsertOperator.getPreparedStatementTypes().get(101001100L).getQueryString());
+ assertEquals(8, userUpsertOperator.getColumnDefinitions().size());
+ assertEquals(true, userUpsertOperator.getColumnDefinitions().get("currentaddress").isFrozen());
+ assertEquals(false, userUpsertOperator.getColumnDefinitions().get("currentaddress").isCollection());
+ assertEquals(true, userUpsertOperator.getColumnDefinitions().get("top_scores").isCollection());
+ assertEquals(true, userUpsertOperator.getColumnDefinitions().get("username").isFrozen());
+ assertEquals(false, userUpsertOperator.getColumnDefinitions().get("username").isCollection());
+ }
+
+ @Test
+ public void testForGetters() throws Exception
+ {
+ Map<String, Object> getters = userUpsertOperator.getGetters();
+ assertNotNull(getters);
+ assertEquals(7, getters.size());
+ }
+
+ @Test
+ public void testForSingleRowInsertWithCodecs() throws Exception
+ {
+ User aUser = new User();
+ aUser.setUserid("user" + System.currentTimeMillis());
+ FullName fullName = new FullName("first1" + System.currentTimeMillis(), "last1" + System.currentTimeMillis());
+ aUser.setUsername(fullName);
+ Address address = new Address("wer", "hjfh", 12, null);
+ aUser.setCurrentaddress(address);
+ UpsertExecutionContext<User> anUpdate = new UpsertExecutionContext<>();
+ anUpdate.setPayload(aUser);
+ userUpsertOperator.beginWindow(0);
+ userUpsertOperator.input.process(anUpdate);
+ userUpsertOperator.endWindow();
+
+ ResultSet results = userUpsertOperator.session.execute(
+ "SELECT * FROM unittests.users WHERE userid = '" + aUser.getUserid() + "'");
+ List<Row> rows = results.all();
+ assertEquals(rows.size(), 1);
+ assertTrue(results.isExhausted());
+ }
+
+ @Test
+ public void testForListAppend() throws Exception
+ {
+ User aUser = new User();
+ String userId = "user" + System.currentTimeMillis();
+ aUser.setUserid(userId);
+ FullName fullName = new FullName("first1" + System.currentTimeMillis(), "last1" + System.currentTimeMillis());
+ aUser.setUsername(fullName);
+ Address address = new Address("street1", "city1", 13, null);
+ aUser.setCurrentaddress(address);
+ Set<String> emails = new HashSet<>();
+ emails.add(new String("1"));
+ emails.add(new String("2"));
+ aUser.setEmails(emails);
+ List<Integer> topScores = new ArrayList<>();
+ topScores.add(1);
+ topScores.add(2);
+ aUser.setTopScores(topScores);
+ UpsertExecutionContext<User> originalEntry = new UpsertExecutionContext<>();
+ originalEntry.setPayload(aUser);
+
+ UpsertExecutionContext<User> subsequentUpdateForTopScores = new UpsertExecutionContext<>();
+ subsequentUpdateForTopScores.setListPlacementStyle(
+ UpsertExecutionContext.ListPlacementStyle.APPEND_TO_EXISTING_LIST);
+ subsequentUpdateForTopScores.setCollectionMutationStyle(
+ UpsertExecutionContext.CollectionMutationStyle.ADD_TO_EXISTING_COLLECTION);
+ subsequentUpdateForTopScores.setNullHandlingMutationStyle(
+ UpsertExecutionContext.NullHandlingMutationStyle.IGNORE_NULL_COLUMNS);
+ User oldUser = new User();
+ oldUser.setUserid(userId);
+ List<Integer> topScoresAppended = new ArrayList<>();
+ topScoresAppended.add(3);
+ oldUser.setTopScores(topScoresAppended);
+ subsequentUpdateForTopScores.setPayload(oldUser);
+ userUpsertOperator.beginWindow(1);
+ userUpsertOperator.input.process(originalEntry);
+ userUpsertOperator.input.process(subsequentUpdateForTopScores);
+ userUpsertOperator.endWindow();
+
+ ResultSet results = userUpsertOperator.session.execute(
+ "SELECT * FROM unittests.users WHERE userid = '" + userId + "'");
+ List<Row> rows = results.all();
+ Row userRow = rows.get(0);
+ List<Integer> topScoresEntry = userRow.getList("top_scores", Integer.class);
+ assertEquals(3, topScoresEntry.size());
+ assertEquals("" + 3, "" + topScoresEntry.get(2));
+ }
+
+ @Test
+ public void testForListPrepend() throws Exception
+ {
+ User aUser = new User();
+ String userId = "user" + System.currentTimeMillis();
+ aUser.setUserid(userId);
+ FullName fullName = new FullName("first1" + System.currentTimeMillis(), "last1" + System.currentTimeMillis());
+ aUser.setUsername(fullName);
+ List<Integer> topScores = new ArrayList<>();
+ topScores.add(1);
+ topScores.add(2);
+ aUser.setTopScores(topScores);
+ UpsertExecutionContext<User> originalEntry = new UpsertExecutionContext<>();
+ originalEntry.setPayload(aUser);
+
+ UpsertExecutionContext<User> subsequentUpdateForTopScores = new UpsertExecutionContext<>();
+ subsequentUpdateForTopScores.setListPlacementStyle(
+ UpsertExecutionContext.ListPlacementStyle.PREPEND_TO_EXISTING_LIST);
+ subsequentUpdateForTopScores.setCollectionMutationStyle(
+ UpsertExecutionContext.CollectionMutationStyle.ADD_TO_EXISTING_COLLECTION);
+ subsequentUpdateForTopScores.setNullHandlingMutationStyle(
+ UpsertExecutionContext.NullHandlingMutationStyle.IGNORE_NULL_COLUMNS);
+ User oldUser = new User();
+ oldUser.setUserid(userId);
+ List<Integer> topScoresAppended = new ArrayList<>();
+ topScoresAppended.add(3);
+ oldUser.setTopScores(topScoresAppended);
+ subsequentUpdateForTopScores.setPayload(oldUser);
+ userUpsertOperator.beginWindow(2);
+ userUpsertOperator.input.process(originalEntry);
+ userUpsertOperator.input.process(subsequentUpdateForTopScores);
+ userUpsertOperator.endWindow();
+
+ ResultSet results = userUpsertOperator.session.execute(
+ "SELECT * FROM unittests.users WHERE userid = '" + userId + "'");
+ List<Row> rows = results.all();
+ Row userRow = rows.get(0);
+ List<Integer> topScoresEntry = userRow.getList("top_scores", Integer.class);
+ assertEquals(3, topScoresEntry.size());
+ assertEquals("" + 3, "" + topScoresEntry.get(0));
+ }
+
+ @Test
+ public void testForCollectionRemoval() throws Exception
+ {
+ User aUser = new User();
+ String userId = "user" + System.currentTimeMillis();
+ aUser.setUserid(userId);
+ FullName fullName = new FullName("first12" + System.currentTimeMillis(), "last12" + System.currentTimeMillis());
+ aUser.setUsername(fullName);
+ Set<String> emails = new HashSet<>();
+ emails.add(new String("1"));
+ emails.add(new String("2"));
+ aUser.setEmails(emails);
+
+ UpsertExecutionContext<User> originalEntry = new UpsertExecutionContext<>();
+ originalEntry.setPayload(aUser);
+
+ UpsertExecutionContext<User> subsequentUpdateForEmails = new UpsertExecutionContext<>();
+ subsequentUpdateForEmails.setCollectionMutationStyle(
+ UpsertExecutionContext.CollectionMutationStyle.REMOVE_FROM_EXISTING_COLLECTION);
+ subsequentUpdateForEmails.setNullHandlingMutationStyle(
+ UpsertExecutionContext.NullHandlingMutationStyle.IGNORE_NULL_COLUMNS);
+ User oldUser = new User();
+ oldUser.setUserid(userId);
+ Set<String> updatedEmails = new HashSet<>();
+ updatedEmails.add(new String("1"));
+ oldUser.setEmails(updatedEmails);
+ subsequentUpdateForEmails.setPayload(oldUser);
+ userUpsertOperator.beginWindow(3);
+ userUpsertOperator.input.process(originalEntry);
+ userUpsertOperator.input.process(subsequentUpdateForEmails);
+ userUpsertOperator.endWindow();
+
+ ResultSet results = userUpsertOperator.session.execute(
+ "SELECT * FROM unittests.users WHERE userid = '" + userId + "'");
+ List<Row> rows = results.all();
+ Row userRow = rows.get(0);
+ Set<String> existingEmailsEntry = userRow.getSet("emails", String.class);
+ assertEquals(1, existingEmailsEntry.size());
+ assertEquals("" + 2, "" + existingEmailsEntry.iterator().next());
+ }
+
+ @Test
+ public void testForCollectionRemovalAndIfExists() throws Exception
+ {
+ User aUser = new User();
+ String userId = "user" + System.currentTimeMillis();
+ aUser.setUserid(userId);
+ FullName fullName = new FullName("first12" + System.currentTimeMillis(), "last12" + System.currentTimeMillis());
+ aUser.setUsername(fullName);
+ Set<String> emails = new HashSet<>();
+ emails.add(new String("1"));
+ emails.add(new String("2"));
+ aUser.setEmails(emails);
+
+ UpsertExecutionContext<User> originalEntry = new UpsertExecutionContext<>();
+ originalEntry.setPayload(aUser);
+
+ UpsertExecutionContext<User> subsequentUpdateForEmails = new UpsertExecutionContext<>();
+ subsequentUpdateForEmails.setCollectionMutationStyle(
+ UpsertExecutionContext.CollectionMutationStyle.REMOVE_FROM_EXISTING_COLLECTION);
+ subsequentUpdateForEmails.setNullHandlingMutationStyle(
+ UpsertExecutionContext.NullHandlingMutationStyle.IGNORE_NULL_COLUMNS);
+ subsequentUpdateForEmails.setUpdateOnlyIfPrimaryKeyExists(true);
+ User oldUser = new User();
+ oldUser.setUserid(userId + System.currentTimeMillis()); // overriding with a non-existent user id
+ Set<String> updatedEmails = new HashSet<>();
+ updatedEmails.add(new String("1"));
+ oldUser.setEmails(updatedEmails);
+ subsequentUpdateForEmails.setPayload(oldUser);
+ userUpsertOperator.beginWindow(4);
+ userUpsertOperator.input.process(originalEntry);
+ userUpsertOperator.input.process(subsequentUpdateForEmails);
+ userUpsertOperator.endWindow();
+
+ ResultSet results = userUpsertOperator.session.execute(
+ "SELECT * FROM unittests.users WHERE userid = '" + userId + "'");
+ List<Row> rows = results.all();
+ Row userRow = rows.get(0);
+ Set<String> existingEmailsEntry = userRow.getSet("emails", String.class);
+ assertEquals(2, existingEmailsEntry.size());
+ assertEquals("" + 1, "" + existingEmailsEntry.iterator().next());
+ }
+
+ @Test
+ public void testForListAppendAndIfExists() throws Exception
+ {
+ User aUser = new User();
+ String userId = "user" + System.currentTimeMillis();
+ aUser.setUserid(userId);
+ FullName fullName = new FullName("first" + System.currentTimeMillis(), "last" + System.currentTimeMillis());
+ aUser.setUsername(fullName);
+ Address address = new Address("street1", "city1", 13, null);
+ aUser.setCurrentaddress(address);
+ Set<String> emails = new HashSet<>();
+ emails.add(new String("1"));
+ emails.add(new String("2"));
+ aUser.setEmails(emails);
+ List<Integer> topScores = new ArrayList<>();
+ topScores.add(1);
+ topScores.add(2);
+ aUser.setTopScores(topScores);
+ UpsertExecutionContext<User> originalEntry = new UpsertExecutionContext<>();
+ originalEntry.setPayload(aUser);
+
+ UpsertExecutionContext<User> subsequentUpdateForTopScores = new UpsertExecutionContext<>();
+ subsequentUpdateForTopScores.setListPlacementStyle(
+ UpsertExecutionContext.ListPlacementStyle.APPEND_TO_EXISTING_LIST);
+ subsequentUpdateForTopScores.setCollectionMutationStyle(
+ UpsertExecutionContext.CollectionMutationStyle.ADD_TO_EXISTING_COLLECTION);
+ subsequentUpdateForTopScores.setNullHandlingMutationStyle(
+ UpsertExecutionContext.NullHandlingMutationStyle.IGNORE_NULL_COLUMNS);
+ subsequentUpdateForTopScores.setUpdateOnlyIfPrimaryKeyExists(true);
+ User oldUser = new User();
+ oldUser.setUserid(userId + System.currentTimeMillis());
+ List<Integer> topScoresAppended = new ArrayList<>();
+ topScoresAppended.add(3);
+ oldUser.setTopScores(topScoresAppended);
+ subsequentUpdateForTopScores.setPayload(oldUser);
+ userUpsertOperator.beginWindow(5);
+ userUpsertOperator.input.process(originalEntry);
+ userUpsertOperator.input.process(subsequentUpdateForTopScores);
+ userUpsertOperator.endWindow();
+
+ ResultSet results = userUpsertOperator.session.execute(
+ "SELECT * FROM unittests.users WHERE userid = '" + userId + "'");
+ List<Row> rows = results.all();
+ Row userRow = rows.get(0);
+ List<Integer> topScoresEntry = userRow.getList("top_scores", Integer.class);
+ assertEquals(2, topScoresEntry.size());
+ assertEquals("" + 2, "" + topScoresEntry.get(1));
+ }
+
+ @Test
+ public void testForListPrependAndExplicitNullForSomeColumns() throws Exception
+ {
+ User aUser = new User();
+ String userId = "user" + System.currentTimeMillis();
+ aUser.setUserid(userId);
+ FullName fullName = new FullName("first24" + System.currentTimeMillis(), "last" + System.currentTimeMillis());
+ aUser.setUsername(fullName);
+ List<Integer> topScores = new ArrayList<>();
+ topScores.add(1);
+ topScores.add(2);
+ aUser.setTopScores(topScores);
+ UpsertExecutionContext<User> originalEntry = new UpsertExecutionContext<>();
+ originalEntry.setPayload(aUser);
+
+ UpsertExecutionContext<User> subsequentUpdateForTopScores = new UpsertExecutionContext<>();
+ subsequentUpdateForTopScores.setListPlacementStyle(
+ UpsertExecutionContext.ListPlacementStyle.PREPEND_TO_EXISTING_LIST);
+ subsequentUpdateForTopScores.setCollectionMutationStyle(
+ UpsertExecutionContext.CollectionMutationStyle.ADD_TO_EXISTING_COLLECTION);
+ subsequentUpdateForTopScores.setNullHandlingMutationStyle(
+ UpsertExecutionContext.NullHandlingMutationStyle.SET_NULL_COLUMNS);
+ User oldUser = new User();
+ oldUser.setUserid(userId);
+ List<Integer> topScoresAppended = new ArrayList<>();
+ topScoresAppended.add(3);
+ oldUser.setTopScores(topScoresAppended);
+ subsequentUpdateForTopScores.setPayload(oldUser);
+ userUpsertOperator.beginWindow(6);
+ userUpsertOperator.input.process(originalEntry);
+ userUpsertOperator.input.process(subsequentUpdateForTopScores);
+ userUpsertOperator.endWindow();
+
+ ResultSet results = userUpsertOperator.session.execute(
+ "SELECT * FROM unittests.users WHERE userid = '" + userId + "'");
+ List<Row> rows = results.all();
+ Row userRow = rows.get(0);
+ FullName name = userRow.get("username", FullName.class);
+ assertEquals(null, name);
+ }
+
+ @Test
+ public void testForSingleRowInsertWithTTL() throws Exception
+ {
+ User aUser = new User();
+ aUser.setUserid("userWithTTL" + System.currentTimeMillis());
+ FullName fullName = new FullName("firstname" + System.currentTimeMillis(), "lasName" + System.currentTimeMillis());
+ aUser.setUsername(fullName);
+ Address address = new Address("city1", "Street1", 12, null);
+ aUser.setCurrentaddress(address);
+ UpsertExecutionContext<User> anUpdate = new UpsertExecutionContext<>();
+ anUpdate.setOverridingTTL(5000);
+ anUpdate.setPayload(aUser);
+ userUpsertOperator.beginWindow(7);
+ userUpsertOperator.input.process(anUpdate);
+ userUpsertOperator.endWindow();
+
+ ResultSet results = userUpsertOperator.session.execute(
+ "SELECT * FROM unittests.users WHERE userid = '" + aUser.getUserid() + "'");
+ List<Row> rows = results.all();
+ assertEquals(rows.size(), 1);
+ assertTrue(results.isExhausted());
+ }
+
+ @Test
+ public void testForSingleRowInsertWithOverridingConsistency() throws Exception
+ {
+ User aUser = new User();
+ aUser.setUserid("userWithConsistency" + System.currentTimeMillis());
+ FullName fullName = new FullName("first" + System.currentTimeMillis(), "last" + System.currentTimeMillis());
+ aUser.setUsername(fullName);
+ Address address = new Address("city21", "Street31", 12, null);
+ aUser.setCurrentaddress(address);
+ UpsertExecutionContext<User> anUpdate = new UpsertExecutionContext<>();
+ anUpdate.setOverridingConsistencyLevel(ConsistencyLevel.LOCAL_SERIAL);
+ anUpdate.setPayload(aUser);
+ userUpsertOperator.beginWindow(8);
+ userUpsertOperator.input.process(anUpdate);
+ userUpsertOperator.endWindow();
+
+ ResultSet results = userUpsertOperator.session.execute(
+ "SELECT * FROM unittests.users WHERE userid = '" + aUser.getUserid() + "'");
+ List<Row> rows = results.all();
+ assertEquals(rows.size(), 1);
+ assertTrue(results.isExhausted());
+ }
+
+
+}
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/664257b4/contrib/src/test/java/com/datatorrent/contrib/cassandra/AbstractUpsertOutputOperatorCompositePKTest.java
----------------------------------------------------------------------
diff --git a/contrib/src/test/java/com/datatorrent/contrib/cassandra/AbstractUpsertOutputOperatorCompositePKTest.java b/contrib/src/test/java/com/datatorrent/contrib/cassandra/AbstractUpsertOutputOperatorCompositePKTest.java
new file mode 100644
index 0000000..b0863e0
--- /dev/null
+++ b/contrib/src/test/java/com/datatorrent/contrib/cassandra/AbstractUpsertOutputOperatorCompositePKTest.java
@@ -0,0 +1,99 @@
+/**
+ * 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 com.datatorrent.contrib.cassandra;
+
+import java.util.List;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.datastax.driver.core.ResultSet;
+import com.datastax.driver.core.Row;
+import com.datatorrent.api.Attribute;
+import com.datatorrent.api.Context;
+import com.datatorrent.api.DAG;
+import com.datatorrent.lib.helper.OperatorContextTestHelper;
+import com.datatorrent.lib.helper.TestPortContext;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * A simple test class that checks functionality when composite primary keys are present as table definitions
+ */
+public class AbstractUpsertOutputOperatorCompositePKTest
+{
+
+
+ public static final String APP_ID = "TestCassandraUpsertOperator";
+ public static final int OPERATOR_ID_FOR_COMPOSITE_PRIMARY_KEYS = 2;
+
+ CompositePrimaryKeyUpdateOperator compositePrimaryKeysOperator = null;
+ OperatorContextTestHelper.TestIdOperatorContext contextForCompositePrimaryKeysOperator;
+ TestPortContext testPortContextForCompositePrimaryKeys;
+
+ @Before
+ public void setupApexContexts() throws Exception
+ {
+ Attribute.AttributeMap.DefaultAttributeMap attributeMapForCompositePrimaryKey =
+ new Attribute.AttributeMap.DefaultAttributeMap();
+ attributeMapForCompositePrimaryKey.put(DAG.APPLICATION_ID, APP_ID);
+ contextForCompositePrimaryKeysOperator = new OperatorContextTestHelper.TestIdOperatorContext(
+ OPERATOR_ID_FOR_COMPOSITE_PRIMARY_KEYS,
+ attributeMapForCompositePrimaryKey);
+
+ Attribute.AttributeMap.DefaultAttributeMap portAttributesForCompositePrimaryKeys =
+ new Attribute.AttributeMap.DefaultAttributeMap();
+ portAttributesForCompositePrimaryKeys.put(Context.PortContext.TUPLE_CLASS, CompositePrimaryKeyRow.class);
+ testPortContextForCompositePrimaryKeys = new TestPortContext(portAttributesForCompositePrimaryKeys);
+
+ compositePrimaryKeysOperator = new CompositePrimaryKeyUpdateOperator();
+ compositePrimaryKeysOperator.setup(contextForCompositePrimaryKeysOperator);
+ compositePrimaryKeysOperator.activate(contextForCompositePrimaryKeysOperator);
+ compositePrimaryKeysOperator.input.setup(testPortContextForCompositePrimaryKeys);
+ }
+
+ @Test
+ public void testForCompositeRowKeyBasedTable() throws Exception
+ {
+ CompositePrimaryKeyRow aCompositeRowKey = new CompositePrimaryKeyRow();
+ String userId = new String("user1" + System.currentTimeMillis());
+ String employeeId = new String(userId + System.currentTimeMillis());
+ int day = 1;
+ int month = 12;
+ int year = 2017;
+ aCompositeRowKey.setDay(day);
+ aCompositeRowKey.setMonth(month);
+ aCompositeRowKey.setYear(year);
+ aCompositeRowKey.setCurrentstatus("status" + System.currentTimeMillis());
+ aCompositeRowKey.setUserid(userId);
+ aCompositeRowKey.setEmployeeid(employeeId);
+ UpsertExecutionContext<CompositePrimaryKeyRow> anUpdate = new UpsertExecutionContext<>();
+ anUpdate.setPayload(aCompositeRowKey);
+ compositePrimaryKeysOperator.beginWindow(12);
+ compositePrimaryKeysOperator.input.process(anUpdate);
+ compositePrimaryKeysOperator.endWindow();
+
+ ResultSet results = compositePrimaryKeysOperator.session.execute(
+ "SELECT * FROM unittests.userstatus WHERE userid = '" + userId + "' and day=" + day + " and month=" +
+ month + " and year=" + year + " and employeeid='" + employeeId + "'");
+ List<Row> rows = results.all();
+ assertEquals(rows.size(), 1);
+ }
+
+
+}
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/664257b4/contrib/src/test/java/com/datatorrent/contrib/cassandra/AbstractUpsertOutputOperatorCountersTest.java
----------------------------------------------------------------------
diff --git a/contrib/src/test/java/com/datatorrent/contrib/cassandra/AbstractUpsertOutputOperatorCountersTest.java b/contrib/src/test/java/com/datatorrent/contrib/cassandra/AbstractUpsertOutputOperatorCountersTest.java
new file mode 100644
index 0000000..b32dc43
--- /dev/null
+++ b/contrib/src/test/java/com/datatorrent/contrib/cassandra/AbstractUpsertOutputOperatorCountersTest.java
@@ -0,0 +1,120 @@
+/**
+ * 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 com.datatorrent.contrib.cassandra;
+
+import java.util.List;
+import org.junit.Before;
+import org.junit.Test;
+import com.datastax.driver.core.ConsistencyLevel;
+import com.datastax.driver.core.ResultSet;
+import com.datastax.driver.core.Row;
+import com.datatorrent.api.Attribute;
+import com.datatorrent.api.Context;
+import com.datatorrent.api.DAG;
+import com.datatorrent.lib.helper.OperatorContextTestHelper;
+import com.datatorrent.lib.helper.TestPortContext;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * A simple test class that tests the functionality when a table with counters is the sink for POJOS
+ */
+public class AbstractUpsertOutputOperatorCountersTest
+{
+
+ public static final String APP_ID = "TestCassandraUpsertOperator";
+ public static final int OPERATOR_ID_FOR_COUNTER_COLUMNS = 1;
+
+ CounterColumnUpdatesOperator counterUpdatesOperator = null;
+ OperatorContextTestHelper.TestIdOperatorContext contextForCountersOperator;
+ TestPortContext testPortContextForCounters;
+
+ @Before
+ public void setupApexContexts() throws Exception
+ {
+ Attribute.AttributeMap.DefaultAttributeMap attributeMapForCounters =
+ new Attribute.AttributeMap.DefaultAttributeMap();
+ attributeMapForCounters.put(DAG.APPLICATION_ID, APP_ID);
+ contextForCountersOperator = new OperatorContextTestHelper.TestIdOperatorContext(OPERATOR_ID_FOR_COUNTER_COLUMNS,
+ attributeMapForCounters);
+
+ Attribute.AttributeMap.DefaultAttributeMap portAttributesForCounters =
+ new Attribute.AttributeMap.DefaultAttributeMap();
+ portAttributesForCounters.put(Context.PortContext.TUPLE_CLASS, CounterColumnTableEntry.class);
+ testPortContextForCounters = new TestPortContext(portAttributesForCounters);
+
+ counterUpdatesOperator = new CounterColumnUpdatesOperator();
+ counterUpdatesOperator.setup(contextForCountersOperator);
+ counterUpdatesOperator.activate(contextForCountersOperator);
+ counterUpdatesOperator.input.setup(testPortContextForCounters);
+ }
+
+ @Test
+ public void testForSingleRowInsertForCounterTables() throws Exception
+ {
+
+ CounterColumnTableEntry aCounterEntry = new CounterColumnTableEntry();
+ String userId = new String("user1" + System.currentTimeMillis());
+ aCounterEntry.setUserId(userId);
+ aCounterEntry.setUpdatecount(3);
+ UpsertExecutionContext<CounterColumnTableEntry> anUpdate = new UpsertExecutionContext<>();
+ anUpdate.setOverridingConsistencyLevel(ConsistencyLevel.LOCAL_ONE);
+ anUpdate.setPayload(aCounterEntry);
+ counterUpdatesOperator.beginWindow(9);
+ counterUpdatesOperator.input.process(anUpdate);
+ counterUpdatesOperator.endWindow();
+
+ ResultSet results = counterUpdatesOperator.session.execute(
+ "SELECT * FROM unittests.userupdates WHERE userid = '" + userId + "'");
+ List<Row> rows = results.all();
+ assertEquals(rows.size(), 1);
+ assertEquals(3, rows.get(0).getLong("updatecount"));
+
+ aCounterEntry = new CounterColumnTableEntry();
+ aCounterEntry.setUserId(userId);
+ aCounterEntry.setUpdatecount(2);
+ anUpdate = new UpsertExecutionContext<>();
+ anUpdate.setOverridingConsistencyLevel(ConsistencyLevel.LOCAL_ONE);
+ anUpdate.setPayload(aCounterEntry);
+ counterUpdatesOperator.beginWindow(10);
+ counterUpdatesOperator.input.process(anUpdate);
+ counterUpdatesOperator.endWindow();
+ results = counterUpdatesOperator.session.execute(
+ "SELECT * FROM unittests.userupdates WHERE userid = '" + userId + "'");
+ rows = results.all();
+ assertEquals(5, rows.get(0).getLong("updatecount"));
+ aCounterEntry = new CounterColumnTableEntry();
+ aCounterEntry.setUserId(userId);
+ aCounterEntry.setUpdatecount(-1);
+ anUpdate = new UpsertExecutionContext<>();
+ anUpdate.setOverridingConsistencyLevel(ConsistencyLevel.LOCAL_ONE);
+ anUpdate.setPayload(aCounterEntry);
+
+ counterUpdatesOperator.beginWindow(11);
+ counterUpdatesOperator.input.process(anUpdate);
+ counterUpdatesOperator.endWindow();
+
+ results = counterUpdatesOperator.session.execute(
+ "SELECT * FROM unittests.userupdates WHERE userid = '" + userId + "'");
+ rows = results.all();
+ assertEquals(4, rows.get(0).getLong("updatecount"));
+
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/664257b4/contrib/src/test/java/com/datatorrent/contrib/cassandra/Address.java
----------------------------------------------------------------------
diff --git a/contrib/src/test/java/com/datatorrent/contrib/cassandra/Address.java b/contrib/src/test/java/com/datatorrent/contrib/cassandra/Address.java
new file mode 100644
index 0000000..faeef03
--- /dev/null
+++ b/contrib/src/test/java/com/datatorrent/contrib/cassandra/Address.java
@@ -0,0 +1,82 @@
+/**
+ * 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 com.datatorrent.contrib.cassandra;
+
+import java.util.Set;
+
+/**
+ *
+ */
+public class Address
+{
+ private String city;
+
+ private String street;
+
+ private int zip_code;
+ private Set<String> phones;
+
+ public Address(String city, String street, int zipcode, Set<String> phones)
+ {
+ this.city = city;
+ this.street = street;
+ this.zip_code = zipcode;
+ this.phones = phones;
+ }
+
+ public String getCity()
+ {
+ return city;
+ }
+
+ public void setCity(String city)
+ {
+ this.city = city;
+ }
+
+ public String getStreet()
+ {
+ return street;
+ }
+
+ public void setStreet(String street)
+ {
+ this.street = street;
+ }
+
+ public int getZip_code()
+ {
+ return zip_code;
+ }
+
+ public void setZip_code(int zip_code)
+ {
+ this.zip_code = zip_code;
+ }
+
+ public Set<String> getPhones()
+ {
+ return phones;
+ }
+
+ public void setPhones(Set<String> phones)
+ {
+ this.phones = phones;
+ }
+}
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/664257b4/contrib/src/test/java/com/datatorrent/contrib/cassandra/AddressCodec.java
----------------------------------------------------------------------
diff --git a/contrib/src/test/java/com/datatorrent/contrib/cassandra/AddressCodec.java b/contrib/src/test/java/com/datatorrent/contrib/cassandra/AddressCodec.java
new file mode 100644
index 0000000..017b34d
--- /dev/null
+++ b/contrib/src/test/java/com/datatorrent/contrib/cassandra/AddressCodec.java
@@ -0,0 +1,85 @@
+/**
+ * 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 com.datatorrent.contrib.cassandra;
+
+import java.nio.ByteBuffer;
+
+import com.datastax.driver.core.ProtocolVersion;
+import com.datastax.driver.core.TypeCodec;
+import com.datastax.driver.core.UDTValue;
+import com.datastax.driver.core.UserType;
+import com.datastax.driver.core.exceptions.InvalidTypeException;
+
+public class AddressCodec extends TypeCodec<Address>
+{
+
+ private final TypeCodec<UDTValue> innerCodec;
+
+ private final UserType userType;
+
+ public AddressCodec(TypeCodec<UDTValue> innerCodec, Class<Address> javaType)
+ {
+ super(innerCodec.getCqlType(), javaType);
+ this.innerCodec = innerCodec;
+ this.userType = (UserType)innerCodec.getCqlType();
+ }
+
+ @Override
+ public ByteBuffer serialize(Address value, ProtocolVersion protocolVersion) throws InvalidTypeException
+ {
+ return innerCodec.serialize(toUDTValue(value), protocolVersion);
+ }
+
+ @Override
+ public Address deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) throws InvalidTypeException
+ {
+ return toAddress(innerCodec.deserialize(bytes, protocolVersion));
+ }
+
+ @Override
+ public Address parse(String value) throws InvalidTypeException
+ {
+ return value == null || value.isEmpty() ? null : toAddress(innerCodec.parse(value));
+ }
+
+ @Override
+ public String format(Address value) throws InvalidTypeException
+ {
+ return value == null ? null : innerCodec.format(toUDTValue(value));
+ }
+
+ protected Address toAddress(UDTValue value)
+ {
+ return value == null ? null : new Address(
+ value.getString("city"),
+ value.getString("street"),
+ value.getInt("zip_code"),
+ value.getSet("phones", String.class)
+ );
+ }
+
+ protected UDTValue toUDTValue(Address value)
+ {
+ return value == null ? null : userType.newValue()
+ .setString("street", value.getStreet())
+ .setInt("zip_code", value.getZip_code())
+ .setString("city", value.getCity())
+ .setSet("phones", value.getPhones());
+ }
+}
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/664257b4/contrib/src/test/java/com/datatorrent/contrib/cassandra/CassandraOperatorTest.java
----------------------------------------------------------------------
diff --git a/contrib/src/test/java/com/datatorrent/contrib/cassandra/CassandraOperatorTest.java b/contrib/src/test/java/com/datatorrent/contrib/cassandra/CassandraOperatorTest.java
index 74f99a8..4a1f883 100644
--- a/contrib/src/test/java/com/datatorrent/contrib/cassandra/CassandraOperatorTest.java
+++ b/contrib/src/test/java/com/datatorrent/contrib/cassandra/CassandraOperatorTest.java
@@ -18,30 +18,45 @@
*/
package com.datatorrent.contrib.cassandra;
-import com.datastax.driver.core.*;
-import com.datastax.driver.core.exceptions.DriverException;
-import com.datatorrent.api.Attribute;
-import com.datatorrent.api.Attribute.AttributeMap;
-import com.datatorrent.api.Context;
-import com.datatorrent.api.DAG;
-import com.datatorrent.lib.helper.TestPortContext;
-import com.datatorrent.lib.util.FieldInfo;
-import com.datatorrent.netlet.util.DTThrowable;
-import com.datatorrent.lib.helper.OperatorContextTestHelper;
-import com.datatorrent.lib.testbench.CollectorTestSink;
-import com.google.common.collect.Lists;
-
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.UUID;
import org.junit.After;
+import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
-import org.junit.AfterClass;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import com.datastax.driver.core.BoundStatement;
+import com.datastax.driver.core.Cluster;
+import com.datastax.driver.core.Configuration;
+import com.datastax.driver.core.PreparedStatement;
+import com.datastax.driver.core.ProtocolVersion;
+import com.datastax.driver.core.ResultSet;
+import com.datastax.driver.core.Row;
+import com.datastax.driver.core.Session;
+import com.datastax.driver.core.exceptions.DriverException;
+import com.google.common.collect.Lists;
+
+import com.datatorrent.api.Attribute;
+import com.datatorrent.api.Attribute.AttributeMap;
+import com.datatorrent.api.Context;
+import com.datatorrent.api.DAG;
+import com.datatorrent.lib.helper.OperatorContextTestHelper;
+import com.datatorrent.lib.helper.TestPortContext;
+import com.datatorrent.lib.testbench.CollectorTestSink;
+import com.datatorrent.lib.util.FieldInfo;
+import com.datatorrent.netlet.util.DTThrowable;
+
/**
* Tests for {@link AbstractCassandraTransactionableOutputOperator} and {@link AbstractCassandraInputOperator}
*/
@@ -249,7 +264,7 @@ public class CassandraOperatorTest
outputOperator.setup(context);
Configuration config = outputOperator.getStore().getCluster().getConfiguration();
- Assert.assertEquals("Procotol version was not set to V2.", ProtocolVersion.V2, config.getProtocolOptions().getProtocolVersionEnum());
+ Assert.assertEquals("Procotol version was not set to V2.", ProtocolVersion.V2, config.getProtocolOptions().getProtocolVersion());
}
@Test
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/664257b4/contrib/src/test/java/com/datatorrent/contrib/cassandra/CompositePrimaryKeyRow.java
----------------------------------------------------------------------
diff --git a/contrib/src/test/java/com/datatorrent/contrib/cassandra/CompositePrimaryKeyRow.java b/contrib/src/test/java/com/datatorrent/contrib/cassandra/CompositePrimaryKeyRow.java
new file mode 100644
index 0000000..9aa2dae
--- /dev/null
+++ b/contrib/src/test/java/com/datatorrent/contrib/cassandra/CompositePrimaryKeyRow.java
@@ -0,0 +1,98 @@
+/**
+ * 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 com.datatorrent.contrib.cassandra;
+
+/**
+ A pojo that represents the table row in which a row key is a composite
+ */
+public class CompositePrimaryKeyRow
+{
+
+ private String userid;
+
+ private int day;
+
+ private int month;
+
+ private int year;
+
+ private String employeeid;
+
+ private String currentstatus;
+
+ public String getUserid()
+ {
+ return userid;
+ }
+
+ public void setUserid(String userid)
+ {
+ this.userid = userid;
+ }
+
+ public int getDay()
+ {
+ return day;
+ }
+
+ public void setDay(int day)
+ {
+ this.day = day;
+ }
+
+ public int getMonth()
+ {
+ return month;
+ }
+
+ public void setMonth(int month)
+ {
+ this.month = month;
+ }
+
+ public int getYear()
+ {
+ return year;
+ }
+
+ public void setYear(int year)
+ {
+ this.year = year;
+ }
+
+ public String getEmployeeid()
+ {
+ return employeeid;
+ }
+
+ public void setEmployeeid(String employeeid)
+ {
+ this.employeeid = employeeid;
+ }
+
+ public String getCurrentstatus()
+ {
+ return currentstatus;
+ }
+
+ public void setCurrentstatus(String currentstatus)
+ {
+ this.currentstatus = currentstatus;
+ }
+}
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/664257b4/contrib/src/test/java/com/datatorrent/contrib/cassandra/CompositePrimaryKeyUpdateOperator.java
----------------------------------------------------------------------
diff --git a/contrib/src/test/java/com/datatorrent/contrib/cassandra/CompositePrimaryKeyUpdateOperator.java b/contrib/src/test/java/com/datatorrent/contrib/cassandra/CompositePrimaryKeyUpdateOperator.java
new file mode 100644
index 0000000..f2d6737
--- /dev/null
+++ b/contrib/src/test/java/com/datatorrent/contrib/cassandra/CompositePrimaryKeyUpdateOperator.java
@@ -0,0 +1,58 @@
+/**
+ * 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 com.datatorrent.contrib.cassandra;
+
+import java.util.Map;
+import com.datastax.driver.core.TypeCodec;
+
+/**
+ A concrete implementation of the AbstractUpsertOperator that tests composite primary key use cases
+ Please run the schema as given in {@link AbstractUpsertOutputOperatorCodecsTest}
+ */
+public class CompositePrimaryKeyUpdateOperator extends AbstractUpsertOutputOperator
+{
+ @Override
+ public ConnectionStateManager.ConnectionBuilder withConnectionBuilder()
+ {
+ return ConnectionStateManager.withNewBuilder()
+ .withSeedNodes("localhost")
+ .withClusterNameAs("Test Cluster")
+ .withDCNameAs("datacenter1")
+ .withTableNameAs("userstatus")
+ .withKeySpaceNameAs("unittests");
+ }
+
+ @Override
+ public Map<String, TypeCodec> getCodecsForUserDefinedTypes()
+ {
+ return null;
+ }
+
+ @Override
+ public Class getPayloadPojoClass()
+ {
+ return CompositePrimaryKeyRow.class;
+ }
+
+ @Override
+ boolean reconcileRecord(Object T, long windowId)
+ {
+ return true;
+ }
+}
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/664257b4/contrib/src/test/java/com/datatorrent/contrib/cassandra/CounterColumnTableEntry.java
----------------------------------------------------------------------
diff --git a/contrib/src/test/java/com/datatorrent/contrib/cassandra/CounterColumnTableEntry.java b/contrib/src/test/java/com/datatorrent/contrib/cassandra/CounterColumnTableEntry.java
new file mode 100644
index 0000000..72b3373
--- /dev/null
+++ b/contrib/src/test/java/com/datatorrent/contrib/cassandra/CounterColumnTableEntry.java
@@ -0,0 +1,47 @@
+/**
+ * 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 com.datatorrent.contrib.cassandra;
+
+public class CounterColumnTableEntry
+{
+ private String userId;
+
+ private long updatecount;
+
+ public String getUserId()
+ {
+ return userId;
+ }
+
+ public void setUserId(String userId)
+ {
+ this.userId = userId;
+ }
+
+ public long getUpdatecount()
+ {
+ return updatecount;
+ }
+
+ public void setUpdatecount(long updatecount)
+ {
+ this.updatecount = updatecount;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/664257b4/contrib/src/test/java/com/datatorrent/contrib/cassandra/CounterColumnUpdatesOperator.java
----------------------------------------------------------------------
diff --git a/contrib/src/test/java/com/datatorrent/contrib/cassandra/CounterColumnUpdatesOperator.java b/contrib/src/test/java/com/datatorrent/contrib/cassandra/CounterColumnUpdatesOperator.java
new file mode 100644
index 0000000..1ec416c
--- /dev/null
+++ b/contrib/src/test/java/com/datatorrent/contrib/cassandra/CounterColumnUpdatesOperator.java
@@ -0,0 +1,54 @@
+/**
+ * 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 com.datatorrent.contrib.cassandra;
+
+import java.util.Map;
+import com.datastax.driver.core.TypeCodec;
+
+public class CounterColumnUpdatesOperator extends AbstractUpsertOutputOperator
+{
+ @Override
+ public ConnectionStateManager.ConnectionBuilder withConnectionBuilder()
+ {
+ return ConnectionStateManager.withNewBuilder()
+ .withSeedNodes("localhost")
+ .withClusterNameAs("Test Cluster")
+ .withDCNameAs("datacenter1")
+ .withTableNameAs("userupdates")
+ .withKeySpaceNameAs("unittests");
+ }
+
+ @Override
+ public Map<String, TypeCodec> getCodecsForUserDefinedTypes()
+ {
+ return null;
+ }
+
+ @Override
+ public Class getPayloadPojoClass()
+ {
+ return CounterColumnTableEntry.class;
+ }
+
+ @Override
+ boolean reconcileRecord(Object T, long windowId)
+ {
+ return true;
+ }
+}
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/664257b4/contrib/src/test/java/com/datatorrent/contrib/cassandra/FullName.java
----------------------------------------------------------------------
diff --git a/contrib/src/test/java/com/datatorrent/contrib/cassandra/FullName.java b/contrib/src/test/java/com/datatorrent/contrib/cassandra/FullName.java
new file mode 100644
index 0000000..61636bb
--- /dev/null
+++ b/contrib/src/test/java/com/datatorrent/contrib/cassandra/FullName.java
@@ -0,0 +1,52 @@
+/**
+ * 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 com.datatorrent.contrib.cassandra;
+
+public class FullName
+{
+ private String firstname;
+
+ private String lastname;
+
+ public FullName(String firstname, String lastname)
+ {
+ this.firstname = firstname;
+ this.lastname = lastname;
+ }
+
+ public String getFirstname()
+ {
+ return firstname;
+ }
+
+ public void setFirstname(String firstname)
+ {
+ this.firstname = firstname;
+ }
+
+ public String getLastname()
+ {
+ return lastname;
+ }
+
+ public void setLastname(String lastname)
+ {
+ this.lastname = lastname;
+ }
+}
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/664257b4/contrib/src/test/java/com/datatorrent/contrib/cassandra/FullNameCodec.java
----------------------------------------------------------------------
diff --git a/contrib/src/test/java/com/datatorrent/contrib/cassandra/FullNameCodec.java b/contrib/src/test/java/com/datatorrent/contrib/cassandra/FullNameCodec.java
new file mode 100644
index 0000000..9816d43
--- /dev/null
+++ b/contrib/src/test/java/com/datatorrent/contrib/cassandra/FullNameCodec.java
@@ -0,0 +1,80 @@
+/**
+ * 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 com.datatorrent.contrib.cassandra;
+
+import java.nio.ByteBuffer;
+
+import com.datastax.driver.core.ProtocolVersion;
+import com.datastax.driver.core.TypeCodec;
+import com.datastax.driver.core.UDTValue;
+import com.datastax.driver.core.UserType;
+import com.datastax.driver.core.exceptions.InvalidTypeException;
+
+public class FullNameCodec extends TypeCodec<FullName>
+{
+
+ private final TypeCodec<UDTValue> innerCodec;
+
+ private final UserType userType;
+
+ public FullNameCodec(TypeCodec<UDTValue> innerCodec, Class<FullName> javaType)
+ {
+ super(innerCodec.getCqlType(), javaType);
+ this.innerCodec = innerCodec;
+ this.userType = (UserType)innerCodec.getCqlType();
+ }
+
+ @Override
+ public ByteBuffer serialize(FullName value, ProtocolVersion protocolVersion) throws InvalidTypeException
+ {
+ return innerCodec.serialize(toUDTValue(value), protocolVersion);
+ }
+
+ @Override
+ public FullName deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) throws InvalidTypeException
+ {
+ return toFullName(innerCodec.deserialize(bytes, protocolVersion));
+ }
+
+ @Override
+ public FullName parse(String value) throws InvalidTypeException
+ {
+ return value == null || value.isEmpty() ? null : toFullName(innerCodec.parse(value));
+ }
+
+ @Override
+ public String format(FullName value) throws InvalidTypeException
+ {
+ return value == null ? null : innerCodec.format(toUDTValue(value));
+ }
+
+ protected FullName toFullName(UDTValue value)
+ {
+ return value == null ? null : new FullName(
+ value.getString("firstname"),
+ value.getString("lastname"));
+ }
+
+ protected UDTValue toUDTValue(FullName value)
+ {
+ return value == null ? null : userType.newValue()
+ .setString("lastname", value.getLastname())
+ .setString("firstname", value.getFirstname());
+ }
+}
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/664257b4/contrib/src/test/java/com/datatorrent/contrib/cassandra/User.java
----------------------------------------------------------------------
diff --git a/contrib/src/test/java/com/datatorrent/contrib/cassandra/User.java b/contrib/src/test/java/com/datatorrent/contrib/cassandra/User.java
new file mode 100644
index 0000000..f4c1d8d
--- /dev/null
+++ b/contrib/src/test/java/com/datatorrent/contrib/cassandra/User.java
@@ -0,0 +1,130 @@
+/**
+ * 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 com.datatorrent.contrib.cassandra;
+
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+
+public class User
+{
+ private String userid;
+ private FullName username;
+ private Set<String> emails;
+ private List<Integer> topScores;
+ private Map<Date, String> todo;
+ private Address currentaddress;
+ private List<FullName> previousnames;
+
+ private String nonMatchingColumn;
+
+ private List<Integer> nonMatchingCollectionColumn;
+
+ public String getUserid()
+ {
+ return userid;
+ }
+
+ public void setUserid(String userid)
+ {
+ this.userid = userid;
+ }
+
+ public FullName getUsername()
+ {
+ return username;
+ }
+
+ public void setUsername(FullName username)
+ {
+ this.username = username;
+ }
+
+ public Set<String> getEmails()
+ {
+ return emails;
+ }
+
+ public void setEmails(Set<String> emails)
+ {
+ this.emails = emails;
+ }
+
+ public List<Integer> getTopScores()
+ {
+ return topScores;
+ }
+
+ public void setTopScores(List<Integer> topScores)
+ {
+ this.topScores = topScores;
+ }
+
+ public Map<Date, String> getTodo()
+ {
+ return todo;
+ }
+
+ public void setTodo(Map<Date, String> todo)
+ {
+ this.todo = todo;
+ }
+
+ public String getNonMatchingColumn()
+ {
+ return nonMatchingColumn;
+ }
+
+ public void setNonMatchingColumn(String nonMatchingColumn)
+ {
+ this.nonMatchingColumn = nonMatchingColumn;
+ }
+
+ public List<Integer> getNonMatchingCollectionColumn()
+ {
+ return nonMatchingCollectionColumn;
+ }
+
+ public void setNonMatchingCollectionColumn(List<Integer> nonMatchingCollectionColumn)
+ {
+ this.nonMatchingCollectionColumn = nonMatchingCollectionColumn;
+ }
+
+ public Address getCurrentaddress()
+ {
+ return currentaddress;
+ }
+
+ public void setCurrentaddress(Address currentaddress)
+ {
+ this.currentaddress = currentaddress;
+ }
+
+ public List<FullName> getPreviousnames()
+ {
+ return previousnames;
+ }
+
+ public void setPreviousnames(List<FullName> previousnames)
+ {
+ this.previousnames = previousnames;
+ }
+}
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/664257b4/contrib/src/test/java/com/datatorrent/contrib/cassandra/UserUpsertOperator.java
----------------------------------------------------------------------
diff --git a/contrib/src/test/java/com/datatorrent/contrib/cassandra/UserUpsertOperator.java b/contrib/src/test/java/com/datatorrent/contrib/cassandra/UserUpsertOperator.java
new file mode 100644
index 0000000..e15c9f6
--- /dev/null
+++ b/contrib/src/test/java/com/datatorrent/contrib/cassandra/UserUpsertOperator.java
@@ -0,0 +1,97 @@
+/**
+ * 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 com.datatorrent.contrib.cassandra;
+
+import java.util.HashMap;
+import java.util.Map;
+import com.datastax.driver.core.CodecRegistry;
+import com.datastax.driver.core.ConsistencyLevel;
+import com.datastax.driver.core.TypeCodec;
+import com.datastax.driver.core.UDTValue;
+import com.datastax.driver.core.UserType;
+
+/**
+ This operator is a concrete implementation of the {@link AbstractUpsertOutputOperator}. This is used
+ in unit tests ( refer {@link AbstractUpsertOutputOperatorCodecsTest} to showcase the use cases of:
+ => Connectivity options
+ => Codecs for uder defined types
+ => How to specify null overrides to avoid patterns of read and then set values in a row
+ => Collection mutations Add / Remove
+ => List placements Append/Prepend if there is a list column
+ => Map POJO field names to Cassandra columns if they do not match exactly
+ => Specifying TTL
+ => Overriding default consistency level set with Connection builder at a tuple level if required
+ */
+public class UserUpsertOperator extends AbstractUpsertOutputOperator
+{
+ @Override
+ public ConnectionStateManager.ConnectionBuilder withConnectionBuilder()
+ {
+ return ConnectionStateManager.withNewBuilder()
+ .withSeedNodes("localhost")
+ .withClusterNameAs("Test Cluster")
+ .withDCNameAs("datacenter1")
+ .withTableNameAs("users")
+ .withKeySpaceNameAs("unittests")
+ .withdefaultConsistencyLevel(ConsistencyLevel.LOCAL_ONE);
+ }
+
+ @Override
+ public Map<String, TypeCodec> getCodecsForUserDefinedTypes()
+ {
+ Map<String, TypeCodec> allCodecs = new HashMap<>();
+ CodecRegistry codecRegistry = cluster.getConfiguration().getCodecRegistry();
+
+ UserType addressType = cluster.getMetadata().getKeyspace(getConnectionStateManager().getKeyspaceName())
+ .getUserType("address");
+ TypeCodec<UDTValue> addressTypeCodec = codecRegistry.codecFor(addressType);
+ AddressCodec addressCodec = new AddressCodec(addressTypeCodec, Address.class);
+ allCodecs.put("currentaddress", addressCodec);
+
+ UserType userFullNameType = cluster.getMetadata().getKeyspace(getConnectionStateManager().getKeyspaceName())
+ .getUserType("fullname");
+ TypeCodec<UDTValue> userFullNameTypeCodec = codecRegistry.codecFor(userFullNameType);
+ FullNameCodec fullNameCodec = new FullNameCodec(userFullNameTypeCodec, FullName.class);
+ allCodecs.put("username", fullNameCodec);
+
+ return allCodecs;
+ }
+
+ @Override
+ public Class getPayloadPojoClass()
+ {
+ return User.class;
+ }
+
+
+ @Override
+ protected Map<String, String> getPojoFieldNameToCassandraColumnNameOverride()
+ {
+ Map<String,String> overridingColMap = new HashMap<>();
+ overridingColMap.put("topScores","top_scores");
+ return overridingColMap;
+ }
+
+ @Override
+ boolean reconcileRecord(Object T, long windowId)
+ {
+ return true;
+ }
+}
+
[2/2] apex-malhar git commit: APEXMALHAR-2181 Cassandra Upsert
Operator
Posted by pr...@apache.org.
APEXMALHAR-2181 Cassandra Upsert Operator
Project: http://git-wip-us.apache.org/repos/asf/apex-malhar/repo
Commit: http://git-wip-us.apache.org/repos/asf/apex-malhar/commit/664257b4
Tree: http://git-wip-us.apache.org/repos/asf/apex-malhar/tree/664257b4
Diff: http://git-wip-us.apache.org/repos/asf/apex-malhar/diff/664257b4
Branch: refs/heads/master
Commit: 664257b4a90d200137c4254428aa806dcd55c92d
Parents: 05a7ca3
Author: ananthc <an...@gmail.com>
Authored: Tue Nov 22 07:26:12 2016 +1100
Committer: ananthc <an...@gmail.com>
Committed: Tue Nov 22 07:26:12 2016 +1100
----------------------------------------------------------------------
contrib/pom.xml | 8 +-
.../cassandra/AbstractUpsertOutputOperator.java | 1012 ++++++++++++++++++
.../cassandra/CassandraPOJOInputOperator.java | 2 +-
.../cassandra/CassandraPOJOOutputOperator.java | 2 +-
.../contrib/cassandra/CassandraPojoUtils.java | 316 ++++++
.../CassandraPreparedStatementGenerator.java | 287 +++++
.../cassandra/ConnectionStateManager.java | 477 +++++++++
.../cassandra/UpsertExecutionContext.java | 210 ++++
.../AbstractUpsertOutputOperatorCodecsTest.java | 476 ++++++++
...ractUpsertOutputOperatorCompositePKTest.java | 99 ++
...bstractUpsertOutputOperatorCountersTest.java | 120 +++
.../datatorrent/contrib/cassandra/Address.java | 82 ++
.../contrib/cassandra/AddressCodec.java | 85 ++
.../cassandra/CassandraOperatorTest.java | 47 +-
.../cassandra/CompositePrimaryKeyRow.java | 98 ++
.../CompositePrimaryKeyUpdateOperator.java | 58 +
.../cassandra/CounterColumnTableEntry.java | 47 +
.../cassandra/CounterColumnUpdatesOperator.java | 54 +
.../datatorrent/contrib/cassandra/FullName.java | 52 +
.../contrib/cassandra/FullNameCodec.java | 80 ++
.../com/datatorrent/contrib/cassandra/User.java | 130 +++
.../contrib/cassandra/UserUpsertOperator.java | 97 ++
22 files changed, 3817 insertions(+), 22 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/664257b4/contrib/pom.xml
----------------------------------------------------------------------
diff --git a/contrib/pom.xml b/contrib/pom.xml
index 84a7e05..566d03d 100755
--- a/contrib/pom.xml
+++ b/contrib/pom.xml
@@ -487,13 +487,13 @@
<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-core</artifactId>
- <version>2.1.8</version>
+ <version>3.1.0</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
- <version>14.0.1</version>
+ <version>16.0.1</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>
@@ -639,13 +639,13 @@
<artifactId>*</artifactId>
</exclusion>
</exclusions>
- </dependency>
+ </dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
<optional>true</optional>
- </dependency>
+ </dependency>
<dependency>
<groupId>com.github.fge</groupId>
<artifactId>json-schema-validator</artifactId>
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/664257b4/contrib/src/main/java/com/datatorrent/contrib/cassandra/AbstractUpsertOutputOperator.java
----------------------------------------------------------------------
diff --git a/contrib/src/main/java/com/datatorrent/contrib/cassandra/AbstractUpsertOutputOperator.java b/contrib/src/main/java/com/datatorrent/contrib/cassandra/AbstractUpsertOutputOperator.java
new file mode 100644
index 0000000..95f98fe
--- /dev/null
+++ b/contrib/src/main/java/com/datatorrent/contrib/cassandra/AbstractUpsertOutputOperator.java
@@ -0,0 +1,1012 @@
+/**
+ * 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 com.datatorrent.contrib.cassandra;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.lang.reflect.Field;
+import java.util.EnumSet;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
+
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.apache.apex.malhar.lib.wal.FSWindowDataManager;
+import org.apache.apex.malhar.lib.wal.WindowDataManager;
+import org.apache.hadoop.classification.InterfaceStability;
+
+import com.codahale.metrics.Timer;
+
+import com.datastax.driver.core.BoundStatement;
+import com.datastax.driver.core.Cluster;
+import com.datastax.driver.core.CodecRegistry;
+import com.datastax.driver.core.ColumnMetadata;
+import com.datastax.driver.core.ConsistencyLevel;
+import com.datastax.driver.core.DataType;
+import com.datastax.driver.core.Host;
+import com.datastax.driver.core.KeyspaceMetadata;
+import com.datastax.driver.core.Metrics;
+import com.datastax.driver.core.PreparedStatement;
+import com.datastax.driver.core.ResultSet;
+import com.datastax.driver.core.Session;
+import com.datastax.driver.core.TableMetadata;
+import com.datastax.driver.core.TypeCodec;
+
+
+
+import com.datatorrent.api.AutoMetric;
+import com.datatorrent.api.Context;
+import com.datatorrent.api.DefaultInputPort;
+import com.datatorrent.api.Operator;
+import com.datatorrent.api.annotation.InputPortFieldAnnotation;
+import com.datatorrent.api.annotation.Stateless;
+import com.datatorrent.common.util.BaseOperator;
+
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * An abstract operator that is used to mutate cassandra rows using PreparedStatements for faster executions
+ * and accommodates EXACTLY_ONCE Semantics if concrete implementations choose to implement an abstract method with
+ * meaningful implementation (as Cassandra is not a pure transactional database , the burden is on the concrete
+ * implementation of the operator ONLY during the reconciliation window (and not for any other windows).
+ *
+ * The typical implementation model is as follows :
+ * 1. Create a concrete implementation of this class by extending this class and implement a few methods.
+ * 2. Define the payload that is the POJO that represents a Cassandra Row is part of this execution context
+ * {@link UpsertExecutionContext}. The payload is a template Parameter of this class
+ * 3. The Upstream operator that wants to write to Cassandra does the following
+ * a. Create an instance of {@link UpsertExecutionContext}
+ * b. Set the payload ( an instance of the POJO created as step two above )
+ * c. Set additional execution context parameters like CollectionHandling style, List placement Styles
+ * overriding TTLs, Update only if Primary keys exist and Consistency Levels etc.
+ * 4. The concrete implementation would then execute this context as a cassandra row mutation
+ *
+ * This operator supports the following features
+ * 1. Highly customizable Connection policies. This is achieved by specifying the ConnectionStateManager.
+ * There are quite a few connection management aspects that can be
+ * controlled via {@link ConnectionStateManager} like consistency, load balancing, connection retries,
+ * table to use, keyspace to use etc. Please refer javadoc of {@link ConnectionStateManager}
+ * 2. Support for Collections : Map, List and Sets are supported
+ * User Defined types as part of collections is also supported.
+ * 3. Support exists for both adding to an existing collection or removing entries from an existing collection.
+ * The POJO field that represents a collection is used to represent the collection that is added or removed.
+ * Thus this can be used to avoid a pattern of read and then write the final value into the cassandra column
+ * which can be used for low latency / high write pattern applications as we can avoid a read in the process.
+ * 4. Supports List Placements : The execution context can be used to specify where the new incoming list
+ * is to be added ( in case there is an existing list in the current column of the current row being mutated.
+ * Supported options are APPEND or PREPEND to an existing list
+ * 5. Support for User Defined Types. A pojo can have fields that represent the Cassandra Columns that are custom
+ * user defined types. Concrete implementations of the operator provide a mapping of the cassandra column name
+ * to the TypeCodec that is to be used for that field inside cassandra. Please refer javadocs of
+ * {@link this.getCodecsForUserDefinedTypes() } for more details
+ * 6. Support for custom mapping of POJO payload field names to that of cassandra columns. Practically speaking,
+ * POJO field names might not always match with Cassandra Column names and hence this support. This will also avoid
+ * writing a POJO just for the cassandra operator and thus an existing POJO can be passed around to this operator.
+ * Please refer javadoc {@link this.getPojoFieldNameToCassandraColumnNameOverride()} for an example
+ * 7. TTL support - A default TTL can be set for the Connection ( via {@link ConnectionStateManager} and then used
+ * for all mutations. This TTL can further be overridden at a tuple execution level to accomodate use cases of
+ * setting custom column expirations typically useful in wide row implementations.
+ * 8. Support for Counter Column tables. Counter tables are also supported with the values inside the incoming
+ * POJO added/subtracted from the counter column accordingly. Please note that the value is not absolute set but
+ * rather representing the value that needs to be added to or subtracted from the current counter.
+ * 9. Support for Composite Primary Keys is also supported. All the POJO fields that map to the composite
+ * primary key are used to resolve the primary key in case of a Composite Primary key table
+ * 10. Support for conditional updates : This operator can be used as an Update Only operator as opposed to an
+ * Upsert operator. i.e. Update only IF EXISTS . This is achieved by setting the appropriate boolean in the
+ * {@link UpsertExecutionContext} tuple that is passed from the upstream operator.
+ * 11. Lenient mapping of POJO fields to Cassandra column names. By default the POJO field names are case insensitive
+ * to cassandra column names. This can be further enhanced by over-riding mappings. Please refer feature 6 above.
+ * 12. Defaults can be overridden at at tuple execution level for TTL & Consistency Policies
+ * 13. Support for handling Nulls i.e. whether null values in the POJO are to be persisted as is or to be ignored so
+ * that the application need not perform a read to populate a POJO field if it is not available in the context
+ * 14. A few autometrics are provided for monitoring the latency aspects of the cassandra cluster
+ */
+
+@InterfaceStability.Evolving
+public abstract class AbstractUpsertOutputOperator extends BaseOperator implements
+ Operator.ActivationListener<Context.OperatorContext>, Operator.CheckpointNotificationListener
+{
+
+ protected ConnectionStateManager connectionStateManager;
+
+ private WindowDataManager windowDataManager;
+
+ private long currentWindowId;
+
+ private transient boolean isInSafeMode;
+
+ private transient long reconcilingWindowId;
+
+ private transient boolean isInReconcilingMode;
+
+ protected transient Session session;
+
+ protected transient Cluster cluster;
+
+ transient Map<String, TypeCodec> complexTypeCodecs;
+
+ transient Map<String, Class> userDefinedTypesClass;
+
+ transient Map<String, DataType> columnDefinitions;
+
+ transient Map<String, String> colNamesMap;
+
+ transient Set<String> pkColumnNames;
+
+ transient Set<String> counterColumns;
+
+ transient Set<String> collectionColumns;
+
+ transient Set<String> listColumns;
+
+ transient Set<String> mapColumns;
+
+ transient Set<String> setColumns;
+
+ transient Set<String> userDefinedTypeColumns;
+
+ transient Set<String> regularColumns;
+
+ protected Map<String, Object> getters;
+
+ protected Map<String, TypeCodec> codecsForCassandraColumnNames;
+
+ CassandraPreparedStatementGenerator cassandraPreparedStatementGenerationUtil;
+
+ transient Map<Long, PreparedStatement> preparedStatementTypes;
+ transient Class<?> tuplePayloadClass;
+
+ private static final transient Logger LOG = LoggerFactory.getLogger(AbstractUpsertOutputOperator.class);
+
+ private static final String CASSANDRA_CONNECTION_PROPS_FILENAME = "CassandraOutputOperator.properties";
+
+ // Metrics
+
+ @AutoMetric
+ transient long ignoredRequestsDuetoIfExistsCheck = 0;
+
+ @AutoMetric
+ transient long successfullWrites = 0;
+
+ @AutoMetric
+ long totalWriteRequestsAttempted = 0;
+
+ @AutoMetric
+ transient int numberOfHostsWrittenTo = 0;
+
+ @AutoMetric
+ transient double fifteenMinuteWriteRateLatency = 0.0;
+
+ @AutoMetric
+ transient double fiveMinuteWriteRateLatency = 0.0;
+
+ @AutoMetric
+ transient double oneMinuteWriteRateLatency = 0.0;
+
+ @AutoMetric
+ transient double meanWriteRateLatency = 0.0;
+
+ @AutoMetric
+ transient long totalIgnoresInThisWindow = 0;
+
+ @AutoMetric
+ long totalIgnoresSinceStart = 0;
+
+ @AutoMetric
+ transient long totalWriteTimeoutsInThisWindow = 0;
+
+ @AutoMetric
+ long totalWriteTimeoutsSinceStart = 0;
+
+ @AutoMetric
+ transient long totalWriteRetriesInThisWindow = 0;
+
+ @AutoMetric
+ long totalWriteRetriesSinceStart = 0;
+
+ @AutoMetric
+ transient long writesWithConsistencyOne = 0;
+
+ @AutoMetric
+ transient long writesWithConsistencyTwo = 0;
+
+ @AutoMetric
+ transient long writesWithConsistencyThree = 0;
+
+ @AutoMetric
+ transient long writesWithConsistencyAll = 0;
+
+ @AutoMetric
+ transient long writesWithConsistencyLocalOne = 0;
+
+ @AutoMetric
+ transient long writesWithConsistencyQuorum = 0;
+
+ @AutoMetric
+ transient long writesWithConsistencyLocalQuorum = 0;
+
+ @AutoMetric
+ transient long writeWithConsistencyLocalSerial = 0;
+
+ @AutoMetric
+ transient long writesWithConsistencyEachQuorum = 0;
+
+ @AutoMetric
+ transient long writesWithConsistencySerial = 0;
+
+ @AutoMetric
+ transient long writesWithConsistencyAny = 0;
+
+ transient Set<Host> uniqueHostsWrittenToInCurrentWindow;
+
+
+ @InputPortFieldAnnotation
+ public final transient DefaultInputPort<UpsertExecutionContext> input = new DefaultInputPort<UpsertExecutionContext>()
+ {
+ @Override
+ public void process(final UpsertExecutionContext tuple)
+ {
+ if (!isEligbleForPassivation(tuple)) {
+ return;
+ }
+ BoundStatement stmnt = setDefaultsAndPrepareBoundStatement(tuple);
+ ResultSet result = session.execute(stmnt);
+ updatePerRowMetric(result);
+ }
+ }; // end of input port implementation
+
+ @Override
+ public void setup(Context.OperatorContext context)
+ {
+ super.setup(context);
+ windowDataManager = getWindowDataManager();
+ if ( windowDataManager == null) {
+ windowDataManager = new FSWindowDataManager();
+ }
+ windowDataManager.setup(context);
+ }
+
+ @Override
+ public void teardown()
+ {
+ super.teardown();
+ if (null != windowDataManager) {
+ windowDataManager.teardown();
+ }
+ }
+
+
+ /**
+ * Primarily resets the per window counter metrics.
+ * @param windowId The windowid as provided by the apex framework
+ */
+ @Override
+ public void beginWindow(long windowId)
+ {
+ super.beginWindow(windowId);
+ totalIgnoresInThisWindow = 0;
+ totalWriteTimeoutsInThisWindow = 0;
+ totalWriteRetriesInThisWindow = 0;
+ uniqueHostsWrittenToInCurrentWindow.clear();
+ successfullWrites = 0;
+ ignoredRequestsDuetoIfExistsCheck = 0;
+ writesWithConsistencyOne = 0;
+ writesWithConsistencyTwo = 0;
+ writesWithConsistencyThree = 0;
+ writesWithConsistencyAll = 0;
+ writesWithConsistencyLocalOne = 0;
+ writesWithConsistencyQuorum = 0;
+ writesWithConsistencyLocalQuorum = 0;
+ writeWithConsistencyLocalSerial = 0;
+ writesWithConsistencyEachQuorum = 0;
+ writesWithConsistencySerial = 0;
+ writesWithConsistencyAny = 0;
+ currentWindowId = windowId;
+ if ( currentWindowId != Stateless.WINDOW_ID) {
+ if (currentWindowId > reconcilingWindowId) {
+ isInSafeMode = false;
+ isInReconcilingMode = false;
+ }
+ if (currentWindowId == reconcilingWindowId) {
+ isInReconcilingMode = true;
+ isInSafeMode = false;
+ }
+ if (currentWindowId < reconcilingWindowId) {
+ isInReconcilingMode = false;
+ isInSafeMode = true;
+ }
+ }
+ }
+
+ /**
+ * Builds the metrics that can be sent to Application master.
+ * Note that some of the metrics are computed in the cassandra driver itself and hence just
+ * extracted from the driver state itself.
+ */
+ @Override
+ public void endWindow()
+ {
+ super.endWindow();
+ Timer timerForThisWindow = session.getCluster().getMetrics().getRequestsTimer();
+ totalWriteRequestsAttempted += timerForThisWindow.getCount();
+ numberOfHostsWrittenTo = uniqueHostsWrittenToInCurrentWindow.size();
+ fifteenMinuteWriteRateLatency = timerForThisWindow.getFifteenMinuteRate();
+ fiveMinuteWriteRateLatency = timerForThisWindow.getFiveMinuteRate();
+ oneMinuteWriteRateLatency = timerForThisWindow.getOneMinuteRate();
+ meanWriteRateLatency = timerForThisWindow.getMeanRate();
+ Metrics.Errors errors = session.getCluster().getMetrics().getErrorMetrics();
+ totalIgnoresInThisWindow = errors.getIgnores().getCount() - totalIgnoresSinceStart;
+ totalIgnoresSinceStart = errors.getIgnores().getCount();
+ totalWriteTimeoutsInThisWindow = errors.getWriteTimeouts().getCount() - totalWriteTimeoutsSinceStart;
+ totalWriteTimeoutsSinceStart = errors.getWriteTimeouts().getCount();
+ totalWriteRetriesInThisWindow = errors.getRetriesOnWriteTimeout().getCount() - totalWriteRetriesSinceStart;
+ totalWriteRetriesSinceStart = errors.getRetriesOnWriteTimeout().getCount();
+ try {
+ // we do not need any particular state and hence reusing the window id itself
+ windowDataManager.save(currentWindowId,currentWindowId);
+ } catch (IOException e) {
+ LOG.error("Error while persisting the current window state " + currentWindowId + " because " + e.getMessage());
+ throw new RuntimeException(e.getMessage());
+ }
+ }
+
+ /**
+ * Initializes cassandra cluster connections as specified by the Connection State manager.
+ * Aspects that are initialized here include Identifying primary key column names, non-primary key columns,
+ * collection type columns, counter columns
+ * It also queries the Keyspace and Table metadata for extracting the above information.
+ * It finally prepares all possible prepared statements that can be executed in the lifetime of the operator
+ * for various permutations like APPEND/REMOVE COLLECTIONS , LIST APPEND/PREPEND , set nulls, set TTLs etc
+ * @param context The apex framework context
+ */
+ @Override
+ public void activate(Context.OperatorContext context)
+ {
+ ConnectionStateManager.ConnectionBuilder connectionBuilder = withConnectionBuilder();
+ if (connectionBuilder == null) {
+ connectionBuilder = buildConnectionBuilderFromPropertiesFile();
+ }
+ checkNotNull(connectionBuilder, " Connection Builder cannot be null.");
+ connectionStateManager = connectionBuilder.initialize();
+ cluster = connectionStateManager.getCluster();
+ session = connectionStateManager.getSession();
+ checkNotNull(session, "Cassandra session cannot be null");
+ tuplePayloadClass = getPayloadPojoClass();
+ columnDefinitions = new HashMap<>();
+ counterColumns = new HashSet<>();
+ collectionColumns = new HashSet<>();
+ pkColumnNames = new HashSet<>();
+ listColumns = new HashSet<>();
+ mapColumns = new HashSet<>();
+ setColumns = new HashSet<>();
+ codecsForCassandraColumnNames = new HashMap<>();
+ userDefinedTypeColumns = new HashSet<>();
+ regularColumns = new HashSet<>();
+ colNamesMap = new HashMap<>();
+ getters = new HashMap<>();
+ userDefinedTypesClass = new HashMap<>();
+ uniqueHostsWrittenToInCurrentWindow = new HashSet<>();
+ registerCodecs();
+ KeyspaceMetadata keyspaceMetadata = cluster.getMetadata()
+ .getKeyspace(connectionStateManager.getKeyspaceName());
+ TableMetadata tableMetadata = keyspaceMetadata
+ .getTable(connectionStateManager.getTableName());
+ registerPrimaryKeyColumnDefinitions(tableMetadata);
+ registerNonPKColumnDefinitions(tableMetadata);
+ preparedStatementTypes = new HashMap<>();
+ generatePreparedStatements();
+ registerGettersForPayload();
+ isInSafeMode = false;
+ isInReconcilingMode = false;
+ reconcilingWindowId = Stateless.WINDOW_ID;
+ if ( (context.getValue(Context.OperatorContext.ACTIVATION_WINDOW_ID) != Stateless.WINDOW_ID) &&
+ context.getValue(Context.OperatorContext.ACTIVATION_WINDOW_ID) <
+ windowDataManager.getLargestCompletedWindow()) {
+ isInSafeMode = true;
+ reconcilingWindowId = windowDataManager.getLargestCompletedWindow() + 1;
+ isInReconcilingMode = false;
+ }
+ }
+
+ @Override
+ public void deactivate()
+ {
+ connectionStateManager.close();
+ }
+
+
+ @Override
+ public void committed(long windowId)
+ {
+ try {
+ windowDataManager.committed(windowId);
+ } catch (IOException e) {
+ LOG.error("Error while committing the window id " + windowId + " because " + e.getMessage());
+ throw new RuntimeException(e.getMessage());
+ }
+ }
+
+ @Override
+ public void beforeCheckpoint(long windowId)
+ {
+ // nothing to be done here. Prevent concrete implementations to be forced to implement this
+ }
+
+ @Override
+ public void checkpointed(long windowId)
+ {
+ // Nothing to be done here. Concrete operators can override if needed.
+ }
+
+ private ConnectionStateManager.ConnectionBuilder buildConnectionBuilderFromPropertiesFile()
+ {
+ ConnectionStateManager.ConnectionBuilder propFileBasedConnectionBuilder = null;
+ Properties config = new Properties();
+ try (InputStream cassandraPropsFile = getClass().getClassLoader().getResourceAsStream(
+ CASSANDRA_CONNECTION_PROPS_FILENAME)) {
+ config.load(cassandraPropsFile);
+ propFileBasedConnectionBuilder = new ConnectionStateManager.ConnectionBuilder();
+ return propFileBasedConnectionBuilder
+ .withClusterNameAs(config.getProperty(ConnectionStateManager.ConnectionBuilder.CLUSTER_NAME_IN_PROPS_FILE))
+ .withDCNameAs(config.getProperty(ConnectionStateManager.ConnectionBuilder.DC_NAME_IN_PROPS_FILE))
+ .withKeySpaceNameAs(config.getProperty(ConnectionStateManager.ConnectionBuilder.KEYSPACE_NAME_IN_PROPS_FILE))
+ .withTableNameAs(config.getProperty(ConnectionStateManager.ConnectionBuilder.TABLE_NAME_IN_PROPS_FILE))
+ .withSeedNodes(config.getProperty(ConnectionStateManager.ConnectionBuilder.SEEDNODES_IN_PROPS_FILE));
+ } catch (Exception ex) {
+ LOG.error("Error while trying to load cassandra config from properties file " +
+ CASSANDRA_CONNECTION_PROPS_FILENAME + " because " + ex.getMessage(), ex);
+ return null;
+ }
+ }
+
+ protected boolean isEligbleForPassivation(final UpsertExecutionContext tuple)
+ {
+ if (isInSafeMode) {
+ return false;
+ }
+ if (isInReconcilingMode) {
+ return reconcileRecord(tuple,currentWindowId);
+ }
+ return true;
+ }
+
+ private BoundStatement setDefaultsAndPrepareBoundStatement(UpsertExecutionContext tuple)
+ {
+ UpsertExecutionContext.NullHandlingMutationStyle nullHandlingMutationStyle = tuple.getNullHandlingMutationStyle();
+ if (UpsertExecutionContext.NullHandlingMutationStyle.UNDEFINED == nullHandlingMutationStyle) {
+ nullHandlingMutationStyle = UpsertExecutionContext.NullHandlingMutationStyle.SET_NULL_COLUMNS;
+ }
+ boolean setNulls = true;
+ if (nullHandlingMutationStyle != UpsertExecutionContext.NullHandlingMutationStyle.SET_NULL_COLUMNS) {
+ setNulls = false;
+ }
+ UpsertExecutionContext.CollectionMutationStyle collectionMutationStyle = tuple.getCollectionMutationStyle();
+ if ((collectionMutationStyle == null) ||
+ (collectionMutationStyle == UpsertExecutionContext.CollectionMutationStyle.UNDEFINED) ) {
+ tuple.setCollectionMutationStyle(UpsertExecutionContext.CollectionMutationStyle.ADD_TO_EXISTING_COLLECTION);
+ }
+ UpsertExecutionContext.ListPlacementStyle listPlacementStyle = tuple.getListPlacementStyle();
+ if ( (listPlacementStyle == null) || (listPlacementStyle == UpsertExecutionContext.ListPlacementStyle.UNDEFINED) ) {
+ tuple.setListPlacementStyle(UpsertExecutionContext.ListPlacementStyle.APPEND_TO_EXISTING_LIST);
+ }
+ PreparedStatement preparedStatement = resolvePreparedStatementForCurrentExecutionContext(tuple);
+ BoundStatement stmnt = processPayloadForExecution(preparedStatement, tuple, setNulls);
+ if ((tuple.isTtlOverridden()) || (connectionStateManager.isTTLSet())) {
+ int ttlToUse = connectionStateManager.getDefaultTtlInSecs();
+ if (tuple.isTtlOverridden()) {
+ ttlToUse = tuple.getOverridingTTL();
+ }
+ stmnt.setInt(CassandraPreparedStatementGenerator.TTL_PARAM_NAME, ttlToUse);
+ }
+ if (tuple.isOverridingConsistencyLevelSet()) {
+ ConsistencyLevel currentConsistencyLevel = tuple.getOverridingConsistencyLevel();
+ if (currentConsistencyLevel.isSerial()) {
+ stmnt.setSerialConsistencyLevel(tuple.getOverridingConsistencyLevel());
+ } else {
+ stmnt.setConsistencyLevel(tuple.getOverridingConsistencyLevel());
+ }
+ }
+ LOG.debug("Executing statement " + preparedStatement.getQueryString());
+ return stmnt;
+ }
+
+ private void updatePerRowMetric(ResultSet result)
+ {
+ uniqueHostsWrittenToInCurrentWindow.add(result.getExecutionInfo().getQueriedHost());
+ updateConsistencyLevelMetrics(result.getExecutionInfo().getAchievedConsistencyLevel());
+ successfullWrites += 1;
+ if (!result.wasApplied()) {
+ ignoredRequestsDuetoIfExistsCheck += 1;
+ }
+ }
+
+ private void updateConsistencyLevelMetrics(ConsistencyLevel resultConsistencyLevel)
+ {
+ if (resultConsistencyLevel == null) {
+ return;
+ }
+ switch (resultConsistencyLevel) {
+ case ALL:
+ writesWithConsistencyAll += 1;
+ break;
+ case ANY:
+ writesWithConsistencyAny += 1;
+ break;
+ case EACH_QUORUM:
+ writesWithConsistencyEachQuorum += 1;
+ break;
+ case LOCAL_ONE:
+ writesWithConsistencyLocalOne += 1;
+ break;
+ case LOCAL_QUORUM:
+ writesWithConsistencyLocalQuorum += 1;
+ break;
+ case LOCAL_SERIAL:
+ writeWithConsistencyLocalSerial += 1;
+ break;
+ case ONE:
+ writesWithConsistencyOne += 1;
+ break;
+ case QUORUM:
+ writesWithConsistencyQuorum += 1;
+ break;
+ case SERIAL:
+ writesWithConsistencySerial += 1;
+ break;
+ case THREE:
+ writesWithConsistencyThree += 1;
+ break;
+ case TWO:
+ writesWithConsistencyTwo += 1;
+ break;
+ default:
+ break;
+ }
+ }
+
+ /**
+ * Shortlists the prepared statement from a cache that is populated initially.
+ * @param tuple The execution context that is used to mutate the current cassandra row
+ * @return The prepared statement that is applicable for the current execution context
+ */
+ private PreparedStatement resolvePreparedStatementForCurrentExecutionContext(UpsertExecutionContext tuple)
+ {
+ EnumSet<OperationContext> operationContextValue = EnumSet.noneOf(OperationContext.class);
+
+ UpsertExecutionContext.CollectionMutationStyle collectionMutationStyle = tuple.getCollectionMutationStyle();
+ if (collectionMutationStyle != null) {
+ if (collectionMutationStyle == UpsertExecutionContext.CollectionMutationStyle.ADD_TO_EXISTING_COLLECTION) {
+ operationContextValue.add(OperationContext.COLLECTIONS_APPEND);
+ }
+ if (collectionMutationStyle == UpsertExecutionContext.CollectionMutationStyle.REMOVE_FROM_EXISTING_COLLECTION) {
+ operationContextValue.add(OperationContext.COLLECTIONS_REMOVE);
+ }
+ }
+ UpsertExecutionContext.ListPlacementStyle listPlacementStyle = tuple.getListPlacementStyle();
+ boolean isListContextSet = false;
+ if ((listPlacementStyle != null) && (collectionMutationStyle ==
+ UpsertExecutionContext.CollectionMutationStyle.ADD_TO_EXISTING_COLLECTION)) {
+ if (listPlacementStyle == UpsertExecutionContext.ListPlacementStyle.APPEND_TO_EXISTING_LIST) {
+ operationContextValue.add(OperationContext.LIST_APPEND);
+ isListContextSet = true;
+ }
+ if (listPlacementStyle == UpsertExecutionContext.ListPlacementStyle.PREPEND_TO_EXISTING_LIST) {
+ operationContextValue.add(OperationContext.LIST_PREPEND);
+ isListContextSet = true;
+ }
+ }
+ if (!isListContextSet) {
+ // use cases when remove is specified but we do not want to build separate prepared statments
+ operationContextValue.add(OperationContext.LIST_APPEND);
+ }
+ if ((connectionStateManager.isTTLSet()) || (tuple.isTtlOverridden())) {
+ operationContextValue.add(OperationContext.TTL_SET);
+ } else {
+ operationContextValue.add(OperationContext.TTL_NOT_SET);
+ }
+ if (tuple.isUpdateOnlyIfPrimaryKeyExists()) {
+ operationContextValue.add(OperationContext.IF_EXISTS_CHECK_PRESENT);
+ } else {
+ operationContextValue.add(OperationContext.IF_EXISTS_CHECK_ABSENT);
+ }
+ return preparedStatementTypes.get(CassandraPreparedStatementGenerator
+ .getSlotIndexForMutationContextPreparedStatement(operationContextValue));
+ }
+
+ /**
+ * Generates a Boundstatement that can be executed for the given incoming tuple. This boundstatement is then
+ * executed as a command
+ * @param ps The prepared statement that was shortlisted to execute the given tuple
+ * @param tuple The tuple that represents the current execution context
+ * @param setNulls This represents the value whether the columns in the prepared statement need to be ignored or
+ * considered
+ * @return The boundstatement appropriately built
+ */
+ @SuppressWarnings(value = "unchecked")
+ private BoundStatement processPayloadForExecution(final PreparedStatement ps, final UpsertExecutionContext tuple,
+ final boolean setNulls)
+ {
+ BoundStatement boundStatement = ps.bind();
+ Object pojoPayload = tuple.getPayload();
+ for (String cassandraColName : getters.keySet()) {
+ DataType dataType = columnDefinitions.get(cassandraColName);
+ CassandraPojoUtils.populateBoundStatementWithValue(boundStatement,getters,dataType,cassandraColName,
+ pojoPayload,setNulls,codecsForCassandraColumnNames);
+ }
+ return boundStatement;
+ }
+
+
+ /**
+ * Builds th map that manages the getters for a given cassandra column
+ * Aspects like case-insensitiveness , over-riding of column names to custom mappings
+ */
+ private void registerGettersForPayload()
+ {
+ Field[] classFields = tuplePayloadClass.getDeclaredFields();
+ Set<String> allColNames = new HashSet<>();
+ Map<String, DataType> dataTypeMap = new HashMap<>();
+ Map<String,String> overridingColnamesMap = getPojoFieldNameToCassandraColumnNameOverride();
+ if ( overridingColnamesMap == null) {
+ overridingColnamesMap = new HashMap<>();
+ }
+ allColNames.addAll(pkColumnNames);
+ allColNames.addAll(regularColumns);
+ Set<String> normalizedColNames = new HashSet<>();
+ Iterator<String> simpleColIterator = allColNames.iterator();
+ while (simpleColIterator.hasNext()) {
+ String aCol = simpleColIterator.next();
+ normalizedColNames.add(aCol.toLowerCase());
+ dataTypeMap.put(aCol.toLowerCase(), columnDefinitions.get(aCol));
+ colNamesMap.put(aCol.toLowerCase(), aCol);
+ codecsForCassandraColumnNames.put(aCol, complexTypeCodecs.get(aCol.toLowerCase()));
+ }
+ for (Field aField : classFields) {
+ String aFieldName = aField.getName();
+ if ( (normalizedColNames.contains(aFieldName.toLowerCase())) ||
+ (overridingColnamesMap.containsKey(aFieldName)) ) {
+
+ String getterExpr = aFieldName;
+ DataType returnDataTypeOfGetter = dataTypeMap.get(aFieldName.toLowerCase());
+ if (returnDataTypeOfGetter == null) {
+ returnDataTypeOfGetter = dataTypeMap.get(overridingColnamesMap.get(aFieldName));
+ }
+ Object getter = CassandraPojoUtils.resolveGetterForField(tuplePayloadClass,getterExpr,
+ returnDataTypeOfGetter,userDefinedTypesClass);
+ String resolvedColumnName = colNamesMap.get(aFieldName.toLowerCase());
+ if (overridingColnamesMap.containsKey(aFieldName)) {
+ resolvedColumnName = overridingColnamesMap.get(aFieldName);
+ }
+ getters.put(resolvedColumnName, getter);
+ }
+ }
+ }
+
+ private void registerCodecs()
+ {
+ complexTypeCodecs = getCodecsForUserDefinedTypes();
+ if (complexTypeCodecs != null) {
+ CodecRegistry registry = cluster.getConfiguration().getCodecRegistry();
+ if (cluster.getConfiguration().getProtocolOptions().getProtocolVersion().toInt() < 4) {
+ LOG.error("Custom codecs are not supported for protocol version < 4");
+ throw new RuntimeException("Custom codecs are not supported for protocol version < 4");
+ }
+ for (String typeCodecStr : complexTypeCodecs.keySet()) {
+ TypeCodec codec = complexTypeCodecs.get(typeCodecStr);
+ registry.register(codec);
+ userDefinedTypesClass.put(typeCodecStr, codec.getJavaType().getRawType());
+ }
+ } else {
+ complexTypeCodecs = new HashMap<>();
+ }
+ }
+
+ private void registerNonPKColumnDefinitions(final TableMetadata tableMetadata)
+ {
+ List<ColumnMetadata> colInfoForTable = tableMetadata.getColumns();
+ for (ColumnMetadata aColumnDefinition : colInfoForTable) {
+ if (aColumnDefinition.getType().isCollection()) {
+ collectionColumns.add(aColumnDefinition.getName());
+ }
+ if (!pkColumnNames.contains(aColumnDefinition.getName())) {
+ columnDefinitions.put(aColumnDefinition.getName(), aColumnDefinition.getType());
+ regularColumns.add(aColumnDefinition.getName());
+ }
+ parseForSpecialDataType(aColumnDefinition);
+ }
+ }
+
+ private void parseForSpecialDataType(final ColumnMetadata aColumnDefinition)
+ {
+ switch (aColumnDefinition.getType().getName()) {
+ case COUNTER:
+ counterColumns.add(aColumnDefinition.getName());
+ break;
+ case MAP:
+ mapColumns.add(aColumnDefinition.getName());
+ break;
+ case SET:
+ setColumns.add(aColumnDefinition.getName());
+ break;
+ case LIST:
+ listColumns.add(aColumnDefinition.getName());
+ break;
+ case UDT:
+ userDefinedTypeColumns.add(aColumnDefinition.getName());
+ break;
+ default:
+ break;
+ }
+ }
+
+ private void registerPrimaryKeyColumnDefinitions(final TableMetadata tableMetadata)
+ {
+ List<ColumnMetadata> primaryKeyColumns = tableMetadata.getPrimaryKey();
+ for (ColumnMetadata primaryColumn : primaryKeyColumns) {
+ columnDefinitions.put(primaryColumn.getName(), primaryColumn.getType());
+ pkColumnNames.add(primaryColumn.getName());
+ parseForSpecialDataType(primaryColumn);
+ }
+ }
+
+ private void generatePreparedStatements()
+ {
+ cassandraPreparedStatementGenerationUtil = new CassandraPreparedStatementGenerator(
+ pkColumnNames, counterColumns, listColumns,
+ mapColumns, setColumns, columnDefinitions);
+ cassandraPreparedStatementGenerationUtil.generatePreparedStatements(session, preparedStatementTypes,
+ connectionStateManager.getKeyspaceName(), connectionStateManager.getTableName());
+ }
+
+ public Map<String, DataType> getColumnDefinitions()
+ {
+ return columnDefinitions;
+ }
+
+ public void setColumnDefinitions(final Map<String, DataType> columnDefinitions)
+ {
+ this.columnDefinitions = columnDefinitions;
+ }
+
+ public Map<String, Class> getUserDefinedTypesClass()
+ {
+ return userDefinedTypesClass;
+ }
+
+ public void setUserDefinedTypesClass(final Map<String, Class> userDefinedTypesClass)
+ {
+ this.userDefinedTypesClass = userDefinedTypesClass;
+ }
+
+ public Set<String> getPkColumnNames()
+ {
+ return pkColumnNames;
+ }
+
+ public void setPkColumnNames(final Set<String> pkColumnNames)
+ {
+ this.pkColumnNames = pkColumnNames;
+ }
+
+ public Set<String> getCounterColumns()
+ {
+ return counterColumns;
+ }
+
+ public void setCounterColumns(final Set<String> counterColumns)
+ {
+ this.counterColumns = counterColumns;
+ }
+
+ public Set<String> getCollectionColumns()
+ {
+ return collectionColumns;
+ }
+
+ public void setCollectionColumns(final Set<String> collectionColumns)
+ {
+ this.collectionColumns = collectionColumns;
+ }
+
+ public Set<String> getListColumns()
+ {
+ return listColumns;
+ }
+
+ public void setListColumns(final Set<String> listColumns)
+ {
+ this.listColumns = listColumns;
+ }
+
+ public Set<String> getMapColumns()
+ {
+ return mapColumns;
+ }
+
+ public void setMapColumns(Set<String> mapColumns)
+ {
+ this.mapColumns = mapColumns;
+ }
+
+ public Set<String> getSetColumns()
+ {
+ return setColumns;
+ }
+
+ public void setSetColumns(Set<String> setColumns)
+ {
+ this.setColumns = setColumns;
+ }
+
+ public Set<String> getUserDefinedTypeColumns()
+ {
+ return userDefinedTypeColumns;
+ }
+
+ public void setUserDefinedTypeColumns(Set<String> userDefinedTypeColumns)
+ {
+ this.userDefinedTypeColumns = userDefinedTypeColumns;
+ }
+
+ public Set<String> getRegularColumns()
+ {
+ return regularColumns;
+ }
+
+ public void setRegularColumns(Set<String> regularColumns)
+ {
+ this.regularColumns = regularColumns;
+ }
+
+ public Map<Long, PreparedStatement> getPreparedStatementTypes()
+ {
+ return preparedStatementTypes;
+ }
+
+ public void setPreparedStatementTypes(Map<Long, PreparedStatement> preparedStatementTypes)
+ {
+ this.preparedStatementTypes = preparedStatementTypes;
+ }
+
+ public Map<String, Object> getGetters()
+ {
+ return getters;
+ }
+
+ public void setGetters(Map<String, Object> getters)
+ {
+ this.getters = getters;
+ }
+
+ public ConnectionStateManager getConnectionStateManager()
+ {
+ return connectionStateManager;
+ }
+
+ public void setConnectionStateManager(ConnectionStateManager connectionStateManager)
+ {
+ this.connectionStateManager = connectionStateManager;
+ }
+
+ public WindowDataManager getWindowDataManager()
+ {
+ return windowDataManager;
+ }
+
+ public void setWindowDataManager(WindowDataManager windowDataManager)
+ {
+ this.windowDataManager = windowDataManager;
+ }
+
+ /***
+ * Implementing concrete Operator instances define the Connection Builder properties by implementing this method
+ * Please refer to {@link com.datatorrent.contrib.cassandra.ConnectionStateManager.ConnectionBuilder} for
+ * an example implementation of the ConnectionStateManager instantiation.
+ * Note that if this method is returning null, the connection properties are
+ * fetched from a properties file loaded from the classpath.
+ * @return The connection state manager that is to be used for this Operator.
+ */
+ public ConnectionStateManager.ConnectionBuilder withConnectionBuilder()
+ {
+ return null;
+ }
+
+ /**
+ * The implementing concrete operator needs to implement this map. The key is the name of the cassandra column
+ * that this codec is used to map to. The TypeCode is used to represent the codec for that column in cassandra
+ * Please refer to test example UserUpsertOperator.java for implementation.
+ * Concrete implementations can return a null if there are no user defined types
+ * @return A map giving column name to the codec to be used
+ */
+ public abstract Map<String, TypeCodec> getCodecsForUserDefinedTypes();
+
+ /**
+ * Defines the Pojo class that is used to represent the row in the table that is set in the ConnectionStateManager
+ * instance. The Class that is returned here should match the template type of the execution context
+ * {@link UpsertExecutionContext}
+ * @return The class that is used as the payload of the Execution context.
+ */
+ public abstract Class getPayloadPojoClass();
+
+ /**
+ * Concrete implementations can override this method to provide a custom map of a POJO file name to the cassandra
+ * column name. This is useful when POJOs that are acting as payloads
+ * 1. Cannot comply with code conventions of POJO as opposed to cassandra column names Ex: Cassandra column names
+ * might have underscores and POJO fields might not be in that format.
+ * It may be noted case sensitivity is ignored when trying to match Cassandra Column names
+ * {@code
+ * @Override
+ protected Map<String, String> getPojoFieldNameToCassandraColumnNameOverride()
+ {
+ Map<String,String> overridingColMap = new HashMap<>();
+ overridingColMap.put("topScores","top_scores"); // topScores is POJO field name and top_scores is Cassandra col
+ return overridingColMap;
+ }
+ *
+ * }
+ * @return A map of the POJO field name as key and value as the Cassandra Column name
+ */
+ protected Map<String,String> getPojoFieldNameToCassandraColumnNameOverride()
+ {
+ return new HashMap<>();
+ }
+
+ /**
+ *
+ * Since Cassandra is not a strictly transactional system and if the Apex operator crashes when a window is in
+ * transit, we might be replaying the same data to be written to cassandra. In the event of such situations, we
+ * would like the control to the concrete operator instance to resolve if they want to let the write happen
+ * or simply skip it. Return true if the write needs to go through or return false to prevent the write
+ * from happening.
+ * Note that this check only happens for one window of data when an operator is resuming from a previous start
+ * In the case of a restart from a previously checkpointed window, the operator simply runs in a "safe mode"
+ * until it reaches the reconciliation window. This is the only window in which this method is called. Hence it
+ * might be okay if this method is "heavy". For example the implementor can choose to read from cassandra for the
+ * incoming record key entry and decide to let the write through or ignore it completely. This is on a per tuple
+ * basis just for the reconciliation window only. Post reconciliation window, the data simply flows through
+ * without this check.
+ * @param T
+ * @param windowId
+ * @return Whether the current POJO that is being passed in should be allowed to write into the cassandra row just for
+ * the reconciling window phase
+ ***/
+
+ abstract boolean reconcileRecord(Object T, long windowId);
+
+ enum OperationContext
+ {
+ UNDEFINED,
+ COLLECTIONS_APPEND,
+ COLLECTIONS_REMOVE,
+ LIST_APPEND,
+ LIST_PREPEND,
+ TTL_SET,
+ TTL_NOT_SET,
+ IF_EXISTS_CHECK_PRESENT,
+ IF_EXISTS_CHECK_ABSENT,
+ }
+}
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/664257b4/contrib/src/main/java/com/datatorrent/contrib/cassandra/CassandraPOJOInputOperator.java
----------------------------------------------------------------------
diff --git a/contrib/src/main/java/com/datatorrent/contrib/cassandra/CassandraPOJOInputOperator.java b/contrib/src/main/java/com/datatorrent/contrib/cassandra/CassandraPOJOInputOperator.java
index 9c56178..f43777f 100644
--- a/contrib/src/main/java/com/datatorrent/contrib/cassandra/CassandraPOJOInputOperator.java
+++ b/contrib/src/main/java/com/datatorrent/contrib/cassandra/CassandraPOJOInputOperator.java
@@ -345,7 +345,7 @@ public class CassandraPOJOInputOperator extends AbstractCassandraInputOperator<O
((Setter<Object, List<?>>)setters.get(i)).set(obj, list);
break;
case TIMESTAMP:
- final Date date = row.getDate(columnName);
+ final Date date = new Date(row.getDate(columnName).getMillisSinceEpoch());
((Setter<Object, Date>)setters.get(i)).set(obj, date);
break;
default:
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/664257b4/contrib/src/main/java/com/datatorrent/contrib/cassandra/CassandraPOJOOutputOperator.java
----------------------------------------------------------------------
diff --git a/contrib/src/main/java/com/datatorrent/contrib/cassandra/CassandraPOJOOutputOperator.java b/contrib/src/main/java/com/datatorrent/contrib/cassandra/CassandraPOJOOutputOperator.java
index 2d1fea3..a191bb0 100644
--- a/contrib/src/main/java/com/datatorrent/contrib/cassandra/CassandraPOJOOutputOperator.java
+++ b/contrib/src/main/java/com/datatorrent/contrib/cassandra/CassandraPOJOOutputOperator.java
@@ -295,7 +295,7 @@ public class CassandraPOJOOutputOperator extends AbstractCassandraTransactionabl
break;
case TIMESTAMP:
final Date date = ((Getter<Object, Date>)getters.get(i)).get(tuple);
- boundStmnt.setDate(i, date);
+ boundStmnt.setDate(i, LocalDate.fromMillisSinceEpoch(date.getTime()));
break;
default:
throw new RuntimeException("unsupported data type " + type.getName());
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/664257b4/contrib/src/main/java/com/datatorrent/contrib/cassandra/CassandraPojoUtils.java
----------------------------------------------------------------------
diff --git a/contrib/src/main/java/com/datatorrent/contrib/cassandra/CassandraPojoUtils.java b/contrib/src/main/java/com/datatorrent/contrib/cassandra/CassandraPojoUtils.java
new file mode 100644
index 0000000..b1f5f4a
--- /dev/null
+++ b/contrib/src/main/java/com/datatorrent/contrib/cassandra/CassandraPojoUtils.java
@@ -0,0 +1,316 @@
+/**
+ * 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 com.datatorrent.contrib.cassandra;
+
+import java.math.BigDecimal;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.UUID;
+
+import com.datastax.driver.core.BoundStatement;
+import com.datastax.driver.core.DataType;
+import com.datastax.driver.core.LocalDate;
+import com.datastax.driver.core.TypeCodec;
+import com.datatorrent.lib.util.PojoUtils;
+
+/***
+ * Used to manage simple data type based getters for given cassandra columns
+ */
+public class CassandraPojoUtils
+{
+ /***
+ * Resolves a getter that can be associated with the given field name in the Pojo matching to the given
+ * data type of cassandra
+ * @param tuplePayloadClass The tuple class that is used to build the getter from
+ * @param getterExpr The name of the field representing the getter that needs to be generated
+ * @param returnDataTypeOfGetter The Data type of the cassandra column
+ * @param userDefinedTypesClass A map that can provide for a UDT class given a column name
+ * @return The getter object that can be used to extract the value at runtime
+ */
+ public static Object resolveGetterForField(Class tuplePayloadClass, String getterExpr,
+ DataType returnDataTypeOfGetter, Map<String,Class> userDefinedTypesClass)
+ {
+ Object getter = null;
+ switch (returnDataTypeOfGetter.getName()) {
+ case INT:
+ getter = PojoUtils.createGetterInt(tuplePayloadClass, getterExpr);
+ break;
+ case BIGINT:
+ case COUNTER:
+ getter = PojoUtils.createGetterLong(tuplePayloadClass, getterExpr);
+ break;
+ case ASCII:
+ case TEXT:
+ case VARCHAR:
+ getter = PojoUtils.createGetter(tuplePayloadClass, getterExpr, String.class);
+ break;
+ case BOOLEAN:
+ getter = PojoUtils.createGetterBoolean(tuplePayloadClass, getterExpr);
+ break;
+ case FLOAT:
+ getter = PojoUtils.createGetterFloat(tuplePayloadClass, getterExpr);
+ break;
+ case DOUBLE:
+ getter = PojoUtils.createGetterDouble(tuplePayloadClass, getterExpr);
+ break;
+ case DECIMAL:
+ getter = PojoUtils.createGetter(tuplePayloadClass, getterExpr, BigDecimal.class);
+ break;
+ case SET:
+ getter = PojoUtils.createGetter(tuplePayloadClass, getterExpr, Set.class);
+ break;
+ case MAP:
+ getter = PojoUtils.createGetter(tuplePayloadClass, getterExpr, Map.class);
+ break;
+ case LIST:
+ getter = PojoUtils.createGetter(tuplePayloadClass, getterExpr, List.class);
+ break;
+ case TIMESTAMP:
+ getter = PojoUtils.createGetter(tuplePayloadClass, getterExpr, Date.class);
+ break;
+ case UUID:
+ getter = PojoUtils.createGetter(tuplePayloadClass, getterExpr, UUID.class);
+ break;
+ case UDT:
+ getter = PojoUtils.createGetter(tuplePayloadClass, getterExpr, userDefinedTypesClass.get(getterExpr));
+ break;
+ default:
+ getter = PojoUtils.createGetter(tuplePayloadClass, getterExpr, Object.class);
+ break;
+ }
+ return getter;
+ }
+
+ /***
+ * Populates a given bound statement column with a value give a POJO and the map representing the getters
+ * @param boundStatement The statement that needs to be populated with the value
+ * @param getters A map mapping the applicable getter for a given column name as key
+ * @param dataType The data type of the cassandra column name
+ * @param cassandraColName The name of the cassandra column
+ * @param pojoPayload The POJO from which the value needs to be extracted
+ * @param setNulls Whether nulls can be set explicitly
+ * @param codecsForCassandraColumnNames A map giving column name to codec with key as column name
+ */
+ @SuppressWarnings(value = "unchecked")
+ public static void populateBoundStatementWithValue(BoundStatement boundStatement, Map<String,Object> getters,
+ DataType dataType, String cassandraColName, Object pojoPayload, boolean setNulls,
+ Map<String,TypeCodec> codecsForCassandraColumnNames)
+ {
+ switch (dataType.getName()) {
+ case BOOLEAN:
+ PojoUtils.GetterBoolean<Object> boolGetter = ((PojoUtils.GetterBoolean<Object>)getters
+ .get(cassandraColName));
+ if (boolGetter != null) {
+ final boolean bool = boolGetter.get(pojoPayload);
+ boundStatement.setBool(cassandraColName, bool);
+ } else {
+ boundStatement.unset(cassandraColName);
+ }
+ break;
+ case INT:
+ PojoUtils.GetterInt<Object> inGetter = ((PojoUtils.GetterInt<Object>)getters.get(cassandraColName));
+ if (inGetter != null) {
+ final int intValue = inGetter.get(pojoPayload);
+ boundStatement.setInt(cassandraColName, intValue);
+ } else {
+ boundStatement.unset(cassandraColName);
+ }
+ break;
+ case BIGINT:
+ case COUNTER:
+ PojoUtils.GetterLong<Object> longGetter = ((PojoUtils.GetterLong<Object>)getters.get(cassandraColName));
+ if (longGetter != null) {
+ final long longValue = longGetter.get(pojoPayload);
+ boundStatement.setLong(cassandraColName, longValue);
+ } else {
+ boundStatement.unset(cassandraColName);
+ }
+ break;
+ case FLOAT:
+ PojoUtils.GetterFloat<Object> floatGetter = ((PojoUtils.GetterFloat<Object>)getters.get(cassandraColName));
+ if (floatGetter != null) {
+ final float floatValue = floatGetter.get(pojoPayload);
+ boundStatement.setFloat(cassandraColName, floatValue);
+ } else {
+ boundStatement.unset(cassandraColName);
+ }
+ break;
+ case DOUBLE:
+ PojoUtils.GetterDouble<Object> doubleGetter = ((PojoUtils.GetterDouble<Object>)getters
+ .get(cassandraColName));
+ if (doubleGetter != null) {
+ final double doubleValue = doubleGetter.get(pojoPayload);
+ boundStatement.setDouble(cassandraColName, doubleValue);
+ } else {
+ boundStatement.unset(cassandraColName);
+ }
+ break;
+ case DECIMAL:
+ PojoUtils.Getter<Object, BigDecimal> bigDecimalGetter = ((PojoUtils.Getter<Object, BigDecimal>)getters
+ .get(cassandraColName));
+ if (bigDecimalGetter != null) {
+ final BigDecimal decimal = bigDecimalGetter.get(pojoPayload);
+ if (decimal == null) {
+ if (!setNulls) {
+ boundStatement.unset(cassandraColName);
+ } else {
+ boundStatement.setDecimal(cassandraColName, null);
+ }
+ } else {
+ boundStatement.setDecimal(cassandraColName, decimal);
+ }
+ } else {
+ boundStatement.unset(cassandraColName);
+ }
+ break;
+ case UUID:
+ PojoUtils.Getter<Object, UUID> uuidGetter = ((PojoUtils.Getter<Object, UUID>)getters.get(cassandraColName));
+ if (uuidGetter != null) {
+ final UUID uuid = uuidGetter.get(pojoPayload);
+ if (uuid == null) {
+ if (!setNulls) {
+ boundStatement.unset(cassandraColName);
+ } else {
+ boundStatement.setUUID(cassandraColName, null);
+ }
+ } else {
+ boundStatement.setUUID(cassandraColName, uuid);
+ }
+ } else {
+ boundStatement.unset(cassandraColName);
+ }
+ break;
+ case ASCII:
+ case VARCHAR:
+ case TEXT:
+ PojoUtils.Getter<Object, String> stringGetter = ((PojoUtils.Getter<Object, String>)getters
+ .get(cassandraColName));
+ if (stringGetter != null) {
+ final String ascii = stringGetter.get(pojoPayload);
+ if (ascii == null) {
+ if (!setNulls) {
+ boundStatement.unset(cassandraColName);
+ } else {
+ boundStatement.setString(cassandraColName, null);
+ }
+ } else {
+ boundStatement.setString(cassandraColName, ascii);
+ }
+ } else {
+ boundStatement.unset(cassandraColName);
+ }
+ break;
+ case SET:
+ PojoUtils.Getter<Object, Set<?>> getterForSet = ((PojoUtils.Getter<Object, Set<?>>)getters
+ .get(cassandraColName));
+ if (getterForSet != null) {
+ final Set<?> set = getterForSet.get(pojoPayload);
+ if (set == null) {
+ if (!setNulls) {
+ boundStatement.unset(cassandraColName);
+ } else {
+ boundStatement.setSet(cassandraColName, null);
+ }
+ } else {
+ boundStatement.setSet(cassandraColName, set);
+ }
+ } else {
+ boundStatement.unset(cassandraColName);
+ }
+ break;
+ case MAP:
+ PojoUtils.Getter<Object, Map<?, ?>> mapGetter = ((PojoUtils.Getter<Object, Map<?, ?>>)getters
+ .get(cassandraColName));
+ if (mapGetter != null) {
+ final Map<?, ?> map = mapGetter.get(pojoPayload);
+ if (map == null) {
+ if (!setNulls) {
+ boundStatement.unset(cassandraColName);
+ } else {
+ boundStatement.setMap(cassandraColName, null);
+ }
+ } else {
+ boundStatement.setMap(cassandraColName, map);
+ }
+ } else {
+ boundStatement.unset(cassandraColName);
+ }
+ break;
+ case LIST:
+ PojoUtils.Getter<Object, List<?>> listGetter = ((PojoUtils.Getter<Object, List<?>>)getters
+ .get(cassandraColName));
+ if (listGetter != null) {
+ final List<?> list = listGetter.get(pojoPayload);
+ if (list == null) {
+ if (!setNulls) {
+ boundStatement.unset(cassandraColName);
+ } else {
+ boundStatement.setList(cassandraColName, null);
+ }
+ } else {
+ boundStatement.setList(cassandraColName, list);
+ }
+ } else {
+ boundStatement.unset(cassandraColName);
+ }
+ break;
+ case TIMESTAMP:
+ PojoUtils.Getter<Object, Date> dateGetter = ((PojoUtils.Getter<Object, Date>)getters.get(cassandraColName));
+ if (dateGetter != null) {
+ final Date date = dateGetter.get(pojoPayload);
+ if (date == null) {
+ if (!setNulls) {
+ boundStatement.unset(cassandraColName);
+ } else {
+ boundStatement.setMap(cassandraColName, null);
+ }
+ } else {
+ boundStatement.setDate(cassandraColName, LocalDate.fromMillisSinceEpoch(date.getTime()));
+ }
+ } else {
+ boundStatement.unset(cassandraColName);
+ }
+ break;
+ case UDT:
+ PojoUtils.Getter<Object, Object> udtGetter = ((PojoUtils.Getter<Object, Object>)getters
+ .get(cassandraColName));
+ if (udtGetter != null) {
+ final Object udtPayload = udtGetter.get(pojoPayload);
+ if (udtPayload == null) {
+ if (!setNulls) {
+ boundStatement.unset(cassandraColName);
+ } else {
+ boundStatement.setUDTValue(cassandraColName, null);
+ }
+ } else {
+ boundStatement.set(cassandraColName, udtPayload, codecsForCassandraColumnNames
+ .get(cassandraColName).getJavaType().getRawType());
+ }
+ } else {
+ boundStatement.unset(cassandraColName);
+ }
+ break;
+ default:
+ throw new RuntimeException("Type not supported for " + dataType.getName());
+ }
+ }
+}
+
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/664257b4/contrib/src/main/java/com/datatorrent/contrib/cassandra/CassandraPreparedStatementGenerator.java
----------------------------------------------------------------------
diff --git a/contrib/src/main/java/com/datatorrent/contrib/cassandra/CassandraPreparedStatementGenerator.java b/contrib/src/main/java/com/datatorrent/contrib/cassandra/CassandraPreparedStatementGenerator.java
new file mode 100644
index 0000000..355fccf
--- /dev/null
+++ b/contrib/src/main/java/com/datatorrent/contrib/cassandra/CassandraPreparedStatementGenerator.java
@@ -0,0 +1,287 @@
+/**
+ * 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 com.datatorrent.contrib.cassandra;
+
+
+import java.util.EnumSet;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.datastax.driver.core.DataType;
+import com.datastax.driver.core.PreparedStatement;
+import com.datastax.driver.core.Session;
+
+/***
+ * Used to generate CQL strings that can be used to generate prepared statements.
+ */
+public class CassandraPreparedStatementGenerator
+{
+
+ private Set<String> pkColumnNames;
+
+ private Set<String> counterColumns;
+
+ private Set<String> listColumns;
+
+ private Set<String> mapColumns;
+
+ private Set<String> setColumns;
+
+ private Map<String, DataType> columnDefinitions;
+
+ private static final transient Logger LOG = LoggerFactory.getLogger(CassandraPreparedStatementGenerator.class);
+
+ public static final String TTL_PARAM_NAME = "ttl";
+
+ public CassandraPreparedStatementGenerator(Set<String> pkColumnNames, Set<String> counterColumns,
+ Set<String> listColumns, Set<String> mapColumns, Set<String> setColumns,
+ Map<String, DataType> columnDefinitions)
+ {
+ this.pkColumnNames = pkColumnNames;
+ this.counterColumns = counterColumns;
+ this.listColumns = listColumns;
+ this.mapColumns = mapColumns;
+ this.setColumns = setColumns;
+ this.columnDefinitions = columnDefinitions;
+ }
+
+ public void generatePreparedStatements(Session session,Map<Long, PreparedStatement> preparedStatementTypes,
+ String keyspaceName,String tableName)
+ {
+
+ Map<Long, String> stringsWithoutPKAndExistsClauses = generatePreparedStatementsQueryStrings(keyspaceName,tableName);
+ String ifExistsClause = " IF EXISTS";
+ Map<Long, String> finalSetOfQueryStrings = new HashMap<>();
+ for (Long currentIndexPos : stringsWithoutPKAndExistsClauses.keySet()) {
+ StringBuilder aQueryStub = new StringBuilder(stringsWithoutPKAndExistsClauses.get(currentIndexPos));
+ buildWhereClauseForPrimaryKeys(aQueryStub);
+ finalSetOfQueryStrings.put(currentIndexPos +
+ getSlotIndexForMutationContextPreparedStatement(EnumSet.of(
+ AbstractUpsertOutputOperator.OperationContext.IF_EXISTS_CHECK_ABSENT)),
+ aQueryStub.toString());
+ if (counterColumns.size() == 0) {
+ // IF exists cannot be used for counter column tables
+ finalSetOfQueryStrings.put(currentIndexPos +
+ getSlotIndexForMutationContextPreparedStatement(EnumSet.of(
+ AbstractUpsertOutputOperator.OperationContext.IF_EXISTS_CHECK_PRESENT)),
+ aQueryStub.toString() + ifExistsClause);
+ }
+ }
+ for (Long currentIndexPos : finalSetOfQueryStrings.keySet()) {
+ String currentQueryStr = finalSetOfQueryStrings.get(currentIndexPos);
+ LOG.info("Registering query support for " + currentQueryStr);
+ PreparedStatement preparedStatementForThisQuery = session.prepare(currentQueryStr);
+ preparedStatementTypes.put(currentIndexPos, preparedStatementForThisQuery);
+ }
+ }
+
+ private void buildWhereClauseForPrimaryKeys(final StringBuilder queryExpression)
+ {
+ queryExpression.append(" WHERE ");
+ int count = 0;
+ for (String pkColName : pkColumnNames) {
+ if (count > 0) {
+ queryExpression.append(" AND ");
+ }
+ count += 1;
+ queryExpression.append(" ").append(pkColName).append(" = :").append(pkColName);
+ }
+ }
+
+
+ private void buildQueryStringForTTLSetCollectionsAppendAndListPrepend(StringBuilder updateQueryRoot,
+ String ttlSetString, Map<Long,String> queryStrings)
+ {
+ // TTL set , Collections Append , List prepend
+ StringBuilder queryExpTTLSetCollAppendListPrepend = new StringBuilder(updateQueryRoot.toString());
+ queryExpTTLSetCollAppendListPrepend.append(ttlSetString);
+ buildNonPKColumnsExpression(queryExpTTLSetCollAppendListPrepend,
+ UpsertExecutionContext.ListPlacementStyle.PREPEND_TO_EXISTING_LIST,
+ UpsertExecutionContext.CollectionMutationStyle.ADD_TO_EXISTING_COLLECTION);
+ queryStrings.put(getSlotIndexForMutationContextPreparedStatement(EnumSet.of(
+ AbstractUpsertOutputOperator.OperationContext.TTL_SET,
+ AbstractUpsertOutputOperator.OperationContext.COLLECTIONS_APPEND,
+ AbstractUpsertOutputOperator.OperationContext.LIST_PREPEND
+ )), queryExpTTLSetCollAppendListPrepend.toString());
+ }
+
+ private void buildQueryStringForTTLSetCollectionsAppendAndListAppend(StringBuilder updateQueryRoot,
+ String ttlSetString,Map<Long,String> queryStrings)
+ {
+ // TTL set , Collections Append , List append
+ StringBuilder queryExpTTLSetCollAppendListAppend = new StringBuilder(updateQueryRoot.toString());
+ queryExpTTLSetCollAppendListAppend.append(ttlSetString);
+ buildNonPKColumnsExpression(queryExpTTLSetCollAppendListAppend,
+ UpsertExecutionContext.ListPlacementStyle.APPEND_TO_EXISTING_LIST,
+ UpsertExecutionContext.CollectionMutationStyle.ADD_TO_EXISTING_COLLECTION);
+ queryStrings.put(getSlotIndexForMutationContextPreparedStatement(EnumSet.of(
+ AbstractUpsertOutputOperator.OperationContext.TTL_SET,
+ AbstractUpsertOutputOperator.OperationContext.COLLECTIONS_APPEND,
+ AbstractUpsertOutputOperator.OperationContext.LIST_APPEND
+ )), queryExpTTLSetCollAppendListAppend.toString());
+ }
+
+ private void buildQueryStringForTTLSetCollectionsRemove(StringBuilder updateQueryRoot,
+ String ttlSetString,Map<Long,String> queryStrings)
+ {
+ // TTL set , Collections Remove
+ StringBuilder queryExpTTLSetCollRemove = new StringBuilder(updateQueryRoot.toString());
+ queryExpTTLSetCollRemove.append(ttlSetString);
+ buildNonPKColumnsExpression(queryExpTTLSetCollRemove,
+ UpsertExecutionContext.ListPlacementStyle.APPEND_TO_EXISTING_LIST, // Just in case user sets it
+ UpsertExecutionContext.CollectionMutationStyle.REMOVE_FROM_EXISTING_COLLECTION);
+ queryStrings.put(getSlotIndexForMutationContextPreparedStatement(EnumSet.of(
+ AbstractUpsertOutputOperator.OperationContext.TTL_SET,
+ AbstractUpsertOutputOperator.OperationContext.COLLECTIONS_REMOVE,
+ AbstractUpsertOutputOperator.OperationContext.LIST_APPEND
+ )), queryExpTTLSetCollRemove.toString());
+ }
+
+ private void buildQueryStringForTTLNotSetCollectionsAppendWithListPrepend(StringBuilder updateQueryRoot,
+ Map<Long,String> queryStrings)
+ {
+ // TTL Not set , Collections Append , List prepend
+ StringBuilder queryExpTTLNotSetCollAppendListPrepend = new StringBuilder(updateQueryRoot.toString());
+ queryExpTTLNotSetCollAppendListPrepend.append(" SET ");
+ buildNonPKColumnsExpression(queryExpTTLNotSetCollAppendListPrepend,
+ UpsertExecutionContext.ListPlacementStyle.PREPEND_TO_EXISTING_LIST,
+ UpsertExecutionContext.CollectionMutationStyle.ADD_TO_EXISTING_COLLECTION);
+ queryStrings.put(getSlotIndexForMutationContextPreparedStatement(EnumSet.of(
+ AbstractUpsertOutputOperator.OperationContext.TTL_NOT_SET,
+ AbstractUpsertOutputOperator.OperationContext.COLLECTIONS_APPEND,
+ AbstractUpsertOutputOperator.OperationContext.LIST_PREPEND
+ )), queryExpTTLNotSetCollAppendListPrepend.toString());
+ }
+
+ private void buildQueryStringForTTLNotSetCollectionsAppendWithListAppend(StringBuilder updateQueryRoot,
+ Map<Long,String> queryStrings)
+ {
+ // TTL Not set , Collections Append , List append
+ StringBuilder queryExpTTLNotSetCollAppendListAppend = new StringBuilder(updateQueryRoot.toString());
+ queryExpTTLNotSetCollAppendListAppend.append(" SET ");
+ buildNonPKColumnsExpression(queryExpTTLNotSetCollAppendListAppend,
+ UpsertExecutionContext.ListPlacementStyle.APPEND_TO_EXISTING_LIST,
+ UpsertExecutionContext.CollectionMutationStyle.ADD_TO_EXISTING_COLLECTION);
+ queryStrings.put(getSlotIndexForMutationContextPreparedStatement(EnumSet.of(
+ AbstractUpsertOutputOperator.OperationContext.TTL_NOT_SET,
+ AbstractUpsertOutputOperator.OperationContext.COLLECTIONS_APPEND,
+ AbstractUpsertOutputOperator.OperationContext.LIST_APPEND
+ )), queryExpTTLNotSetCollAppendListAppend.toString());
+ }
+
+
+ private void buildQueryStringForTTLNotSetCollectionsRemove(StringBuilder updateQueryRoot,
+ Map<Long,String> queryStrings)
+ {
+ // TTL Not set , Collections Remove
+ StringBuilder queryExpTTLNotSetCollRemove = new StringBuilder(updateQueryRoot.toString());
+ queryExpTTLNotSetCollRemove.append(" SET ");
+ buildNonPKColumnsExpression(queryExpTTLNotSetCollRemove,
+ UpsertExecutionContext.ListPlacementStyle.APPEND_TO_EXISTING_LIST, // Just in case user sets it
+ UpsertExecutionContext.CollectionMutationStyle.REMOVE_FROM_EXISTING_COLLECTION);
+ queryStrings.put(getSlotIndexForMutationContextPreparedStatement(EnumSet.of(
+ AbstractUpsertOutputOperator.OperationContext.TTL_NOT_SET,
+ AbstractUpsertOutputOperator.OperationContext.COLLECTIONS_REMOVE,
+ AbstractUpsertOutputOperator.OperationContext.LIST_APPEND
+ )), queryExpTTLNotSetCollRemove.toString());
+ }
+
+ private Map<Long, String> generatePreparedStatementsQueryStrings(String keyspaceName,String tableName)
+ {
+ Map<Long, String> queryStrings = new HashMap<>();
+ //UPDATE keyspace_name.table_name USING option AND option SET assignment, assignment, ... WHERE row_specification
+ StringBuilder updateQueryRoot = new StringBuilder(" UPDATE " + keyspaceName +
+ "." + tableName + " ");
+ String ttlSetString = " USING ttl :" + TTL_PARAM_NAME + " SET ";
+ buildQueryStringForTTLSetCollectionsAppendAndListPrepend(updateQueryRoot,ttlSetString,queryStrings);
+ buildQueryStringForTTLSetCollectionsAppendAndListAppend(updateQueryRoot,ttlSetString,queryStrings);
+ buildQueryStringForTTLSetCollectionsRemove(updateQueryRoot,ttlSetString,queryStrings);
+ buildQueryStringForTTLNotSetCollectionsAppendWithListPrepend(updateQueryRoot,queryStrings);
+ buildQueryStringForTTLNotSetCollectionsAppendWithListAppend(updateQueryRoot,queryStrings);
+ buildQueryStringForTTLNotSetCollectionsRemove(updateQueryRoot,queryStrings);
+ return queryStrings;
+ }
+
+
+ public static long getSlotIndexForMutationContextPreparedStatement(
+ final EnumSet<AbstractUpsertOutputOperator.OperationContext> context)
+ {
+ Iterator<AbstractUpsertOutputOperator.OperationContext> itrForContexts = context.iterator();
+ long indexValue = 0;
+ while (itrForContexts.hasNext()) {
+ AbstractUpsertOutputOperator.OperationContext aContext = itrForContexts.next();
+ indexValue += Math.pow(10, aContext.ordinal());
+ }
+ return indexValue;
+ }
+
+ private void buildNonPKColumnsExpression(final StringBuilder queryExpression,
+ UpsertExecutionContext.ListPlacementStyle listPlacementStyle,
+ UpsertExecutionContext.CollectionMutationStyle collectionMutationStyle)
+ {
+ int count = 0;
+ for (String colNameEntry : columnDefinitions.keySet()) {
+ if (pkColumnNames.contains(colNameEntry)) {
+ continue;
+ }
+ if (count > 0) {
+ queryExpression.append(",");
+ }
+ count += 1;
+ if (counterColumns.contains(colNameEntry)) {
+ queryExpression.append(" " + colNameEntry + " = " + colNameEntry + " + :" + colNameEntry);
+ continue;
+ }
+ DataType dataType = columnDefinitions.get(colNameEntry);
+ if ((!dataType.isCollection()) && (!counterColumns.contains(colNameEntry))) {
+ queryExpression.append(" " + colNameEntry + " = :" + colNameEntry);
+ continue;
+ }
+ if ((dataType.isCollection()) && (!dataType.isFrozen())) {
+ if (collectionMutationStyle == UpsertExecutionContext.CollectionMutationStyle.REMOVE_FROM_EXISTING_COLLECTION) {
+ queryExpression.append(" " + colNameEntry + " = " + colNameEntry + " - :" + colNameEntry);
+ }
+ if (collectionMutationStyle == UpsertExecutionContext.CollectionMutationStyle.ADD_TO_EXISTING_COLLECTION) {
+ if ((setColumns.contains(colNameEntry)) || (mapColumns.contains(colNameEntry))) {
+ queryExpression.append(" " + colNameEntry + " = " + colNameEntry + " + :" + colNameEntry);
+ }
+ if ((listColumns.contains(colNameEntry)) &&
+ (listPlacementStyle == UpsertExecutionContext.ListPlacementStyle.APPEND_TO_EXISTING_LIST)) {
+ queryExpression.append(" " + colNameEntry + " = " + colNameEntry + " + :" + colNameEntry);
+ }
+ if ((listColumns.contains(colNameEntry)) &&
+ (listPlacementStyle == UpsertExecutionContext.ListPlacementStyle.PREPEND_TO_EXISTING_LIST)) {
+ queryExpression.append(" " + colNameEntry + " = :" + colNameEntry + " + " + colNameEntry);
+ }
+ }
+ } else {
+ if ((dataType.isCollection()) && (dataType.isFrozen())) {
+ queryExpression.append(" " + colNameEntry + " = :" + colNameEntry);
+ }
+ }
+ }
+ }
+
+}
+