Repository: cassandra Updated Branches: refs/heads/trunk 3ea35bda6 -> ce6296bea
Updated trigger example patch by Mike Adamson; reviewed by Aleksey Yeschenko for CASSANDRA-10257 Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/fb463c79 Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/fb463c79 Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/fb463c79 Branch: refs/heads/trunk Commit: fb463c79e0cb63114a2f220182e7c7ed3e0f4205 Parents: d766f4f Author: Mike Adamson <madam...@datastax.com> Authored: Tue Oct 20 11:18:37 2015 +0100 Committer: Aleksey Yeschenko <alek...@apache.org> Committed: Tue Nov 10 15:35:13 2015 +0000 ---------------------------------------------------------------------- CHANGES.txt | 1 + examples/triggers/README.txt | 41 ++++++---- examples/triggers/conf/AuditTrigger.properties | 19 +++++ examples/triggers/conf/InvertedIndex.properties | 19 ----- .../apache/cassandra/triggers/AuditTrigger.java | 71 ++++++++++++++++++ .../cassandra/triggers/InvertedIndex.java | 79 -------------------- 6 files changed, 119 insertions(+), 111 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cassandra/blob/fb463c79/CHANGES.txt ---------------------------------------------------------------------- diff --git a/CHANGES.txt b/CHANGES.txt index 24e42c0..b8a65fd 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,4 +1,5 @@ 3.0.1 + * Updated trigger example (CASSANDRA-10257) Merged from 2.2: * (Hadoop) ensure that Cluster instances are always closed (CASSANDRA-10058) Merged from 2.1: http://git-wip-us.apache.org/repos/asf/cassandra/blob/fb463c79/examples/triggers/README.txt ---------------------------------------------------------------------- diff --git a/examples/triggers/README.txt b/examples/triggers/README.txt index a99fa98..e5f1ecf 100644 --- a/examples/triggers/README.txt +++ b/examples/triggers/README.txt @@ -1,11 +1,8 @@ -Cassandra Trigger's Example: -========================= +Cassandra Trigger 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. +The AuditTrigger class will create a basic audit of +activity on a table. Installation: ============ @@ -13,9 +10,27 @@ change directory to <cassandra_src_dir>/examples/triggers run "ant jar" Copy build/trigger-example.jar to <cassandra_conf>/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" - USING 'org.apache.cassandra.triggers.InvertedIndex'; -Start inserting data to the column family which has the triggers. + +Create the keyspace and table configured in AuditTrigger.properties: + CREATE KEYSPACE test WITH REPLICATION = + { 'class' : 'SimpleStrategy', 'replication_factor' : '1' }; + CREATE TABLE test.audit (key timeuuid, keyspace_name text, + table_name text, primary_key text, PRIMARY KEY(key)); + +Create a table to add the trigger to: + CREATE TABLE test.test (key text, value text, PRIMARY KEY(key)); + Note: The example currently only handles non-composite partition keys + +Configure the trigger on the table: + CREATE TRIGGER test1 ON test.test + USING 'org.apache.cassandra.triggers.AuditTrigger'; + +Start inserting data to the table that has the trigger. For each +partition added to the table an entry should appear in the 'audit' table: + INSERT INTO test.test (key, value) values ('1', '1'); + SELECT * FROM test.audit; + + key | keyspace_name | primary_key | table_name + --------------------------------------+---------------+-------------+------------ + 7dc75b60-770f-11e5-9019-033d8af33e6f | test | 1 | test + http://git-wip-us.apache.org/repos/asf/cassandra/blob/fb463c79/examples/triggers/conf/AuditTrigger.properties ---------------------------------------------------------------------- diff --git a/examples/triggers/conf/AuditTrigger.properties b/examples/triggers/conf/AuditTrigger.properties new file mode 100644 index 0000000..7f122de --- /dev/null +++ b/examples/triggers/conf/AuditTrigger.properties @@ -0,0 +1,19 @@ +# 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. + +keyspace=test +table=audit http://git-wip-us.apache.org/repos/asf/cassandra/blob/fb463c79/examples/triggers/conf/InvertedIndex.properties ---------------------------------------------------------------------- diff --git a/examples/triggers/conf/InvertedIndex.properties b/examples/triggers/conf/InvertedIndex.properties deleted file mode 100644 index ea49a86..0000000 --- a/examples/triggers/conf/InvertedIndex.properties +++ /dev/null @@ -1,19 +0,0 @@ -# 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. - -keyspace=Keyspace1 -table=InvertedIndex http://git-wip-us.apache.org/repos/asf/cassandra/blob/fb463c79/examples/triggers/src/org/apache/cassandra/triggers/AuditTrigger.java ---------------------------------------------------------------------- diff --git a/examples/triggers/src/org/apache/cassandra/triggers/AuditTrigger.java b/examples/triggers/src/org/apache/cassandra/triggers/AuditTrigger.java new file mode 100644 index 0000000..7739450 --- /dev/null +++ b/examples/triggers/src/org/apache/cassandra/triggers/AuditTrigger.java @@ -0,0 +1,71 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.cassandra.triggers; + +import java.io.InputStream; +import java.util.Collection; +import java.util.Collections; +import java.util.Properties; + +import org.apache.cassandra.config.Schema; +import org.apache.cassandra.db.Mutation; +import org.apache.cassandra.db.RowUpdateBuilder; +import org.apache.cassandra.db.partitions.Partition; +import org.apache.cassandra.io.util.FileUtils; +import org.apache.cassandra.utils.FBUtilities; +import org.apache.cassandra.utils.UUIDGen; + +public class AuditTrigger implements ITrigger +{ + private Properties properties = loadProperties(); + + public Collection<Mutation> augment(Partition update) + { + String auditKeyspace = properties.getProperty("keyspace"); + String auditTable = properties.getProperty("table"); + + RowUpdateBuilder audit = new RowUpdateBuilder(Schema.instance.getCFMetaData(auditKeyspace, auditTable), + FBUtilities.timestampMicros(), + UUIDGen.getTimeUUID()); + + audit.add("keyspace_name", update.metadata().ksName); + audit.add("table_name", update.metadata().cfName); + audit.add("primary_key", update.metadata().getKeyValidator().getString(update.partitionKey().getKey())); + + return Collections.singletonList(audit.build()); + } + + private static Properties loadProperties() + { + Properties properties = new Properties(); + InputStream stream = AuditTrigger.class.getClassLoader().getResourceAsStream("AuditTrigger.properties"); + try + { + properties.load(stream); + } + catch (Exception e) + { + throw new RuntimeException(e); + } + finally + { + FileUtils.closeQuietly(stream); + } + return properties; + } +} http://git-wip-us.apache.org/repos/asf/cassandra/blob/fb463c79/examples/triggers/src/org/apache/cassandra/triggers/InvertedIndex.java ---------------------------------------------------------------------- diff --git a/examples/triggers/src/org/apache/cassandra/triggers/InvertedIndex.java b/examples/triggers/src/org/apache/cassandra/triggers/InvertedIndex.java deleted file mode 100644 index 2053387..0000000 --- a/examples/triggers/src/org/apache/cassandra/triggers/InvertedIndex.java +++ /dev/null @@ -1,79 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.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.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import org.apache.cassandra.db.Cell; -import org.apache.cassandra.db.ColumnFamily; -import org.apache.cassandra.db.Mutation; -import org.apache.cassandra.io.util.FileUtils; - -public class InvertedIndex implements ITrigger -{ - private static final Logger logger = LoggerFactory.getLogger(InvertedIndex.class); - private Properties properties = loadProperties(); - - public Collection<Mutation> augment(ByteBuffer key, ColumnFamily update) - { - List<Mutation> mutations = new ArrayList<>(update.getColumnCount()); - - String indexKeySpace = properties.getProperty("keyspace"); - String indexColumnFamily = properties.getProperty("table"); - for (Cell cell : update) - { - // Skip the row marker and other empty values, since they lead to an empty key. - if (cell.value().remaining() > 0) - { - Mutation mutation = new Mutation(indexKeySpace, cell.value()); - mutation.add(indexColumnFamily, cell.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; - } -}