diff --git a/examples/triggers/README.txt b/examples/triggers/README.txt new file mode 100644 index 0000000000..974d29cf96 --- /dev/null +++ b/examples/triggers/README.txt @@ -0,0 +1,21 @@ +Cassandra Trigger's Example: +========================= + +InvertedIndex class will create a inverted index of +RowKey:ColumnName:Value to Value:ColumnName:RowKey + +NOTE: This example is limited to append-only workloads, + doesn't delete indexes on deletes. + +Installation: +============ +change directory to /examples/triggers +run "ant jar" +Copy build/trigger-example.jar to /triggers/ +Copy conf/* to /conf/ +Create column family configured in InvertedIndex.properties + Example: Keyspace1.InvertedIndex as configured in InvertedIndex.properties +Configure trigger on the table. + Example: CREATE TRIGGER test1 ON "Keyspace1"."Standard1" + EXECUTE ('org.apache.cassandra.triggers.InvertedIndex'); +Start inserting data to the column family which has the triggers. diff --git a/examples/triggers/build.xml b/examples/triggers/build.xml new file mode 100644 index 0000000000..55fcccefc9 --- /dev/null +++ b/examples/triggers/build.xml @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/triggers/conf/InvertedIndex.properties b/examples/triggers/conf/InvertedIndex.properties new file mode 100644 index 0000000000..6db6d61b6d --- /dev/null +++ b/examples/triggers/conf/InvertedIndex.properties @@ -0,0 +1,2 @@ +keyspace=Keyspace1 +columnfamily=InvertedIndex \ No newline at end of file diff --git a/examples/triggers/src/org/apache/cassandra/triggers/InvertedIndex.java b/examples/triggers/src/org/apache/cassandra/triggers/InvertedIndex.java new file mode 100644 index 0000000000..6eb0cbdf34 --- /dev/null +++ b/examples/triggers/src/org/apache/cassandra/triggers/InvertedIndex.java @@ -0,0 +1,43 @@ +package org.apache.cassandra.triggers; + +import java.io.InputStream; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Properties; + +import org.apache.cassandra.db.ColumnFamily; +import org.apache.cassandra.db.RowMutation; +import org.apache.cassandra.io.util.FileUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class InvertedIndex implements ITrigger { + private static final Logger logger = LoggerFactory.getLogger(InvertedIndex.class); + private Properties properties = loadProperties(); + + public Collection augment(ByteBuffer key, ColumnFamily update) { + List mutations = new ArrayList(); + for (ByteBuffer name : update.getColumnNames()) { + RowMutation mutation = new RowMutation(properties.getProperty("keyspace"), update.getColumn(name).value()); + mutation.add(properties.getProperty("columnfamily"), name, key, System.currentTimeMillis()); + mutations.add(mutation); + } + return mutations; + } + + private static Properties loadProperties() { + Properties properties = new Properties(); + InputStream stream = InvertedIndex.class.getClassLoader().getResourceAsStream("InvertedIndex.properties"); + try { + properties.load(stream); + } catch (Exception e) { + throw new RuntimeException(e); + } finally { + FileUtils.closeQuietly(stream); + } + logger.info("loaded property file, InvertedIndex.properties"); + return properties; + } +}