Add trigger examples

patch by Vijay; reviewed by jbellis for CASSANDRA-5574
This commit is contained in:
Vijay Parthasarathy 2013-07-11 21:36:30 -07:00
parent b320cd9cdb
commit 829688f9eb
4 changed files with 106 additions and 0 deletions

View File

@ -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 <cassandra_src_dir>/examples/triggers
run "ant jar"
Copy build/trigger-example.jar to <cassandra_home>/triggers/
Copy conf/* to <cassandra_home>/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.

View File

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<project default="jar" name="trigger-example">
<property name="cassandra.dir" value="../.." />
<property name="cassandra.dir.lib" value="${cassandra.dir}/lib" />
<property name="cassandra.classes" value="${cassandra.dir}/build/classes/main" />
<property name="build.src" value="${basedir}/src" />
<property name="build.dir" value="${basedir}/build" />
<property name="build.classes" value="${build.dir}/classes" />
<property name="final.name" value="trigger-example" />
<path id="build.classpath">
<fileset dir="${cassandra.dir.lib}">
<include name="**/*.jar" />
</fileset>
<fileset dir="${cassandra.dir}/build/lib/jars">
<include name="**/*.jar" />
</fileset>
<pathelement location="${cassandra.classes}" />
</path>
<target name="init">
<mkdir dir="${build.classes}" />
</target>
<target name="build" depends="init">
<javac destdir="${build.classes}" debug="true" includeantruntime="false">
<src path="${build.src}" />
<classpath refid="build.classpath" />
</javac>
</target>
<target name="jar" depends="build">
<jar jarfile="${build.dir}/${final.name}.jar">
<fileset dir="${build.classes}" />
</jar>
</target>
<target name="clean">
<delete dir="${build.dir}" />
</target>
</project>

View File

@ -0,0 +1,2 @@
keyspace=Keyspace1
columnfamily=InvertedIndex

View File

@ -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<RowMutation> augment(ByteBuffer key, ColumnFamily update) {
List<RowMutation> mutations = new ArrayList<RowMutation>();
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;
}
}