cassandra 3.x触发器示例错误

时间:2016-05-26 07:50:24

标签: java xml triggers cassandra

嘿,我想实现这个例子here 但它不起作用,我不知道如何解决它。

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;
    }
}

和xml代码:

  <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="conf.dir" value="${basedir}/conf" />
    <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}" />
            <fileset dir="${conf.dir}">
                <include name="**/*.properties" />
            </fileset>
        </jar>
    </target>

    <target name="clean">
        <delete dir="${build.dir}" />
    </target>
    <dependencies>
<dependency>
    <groupId>org.apache.cassandra</groupId>
    <artifactId>cassandra-all</artifactId>
    <version>0.8.0-rc1</version>
</dependency>
</dependencies>
</project>

我得到此错误: 实例无法解析或不是字段 当我想创建一个新的RowUpdateBuilder对象。 有人可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

Cassandra触发器的ClassLoader是不同的。因此,您应该将方法loadProperties提取到另一个类并打包到$CASSANDRA/lib

答案 1 :(得分:0)

pom中的

 <dependencies>
    <dependency>
        <groupId>org.apache.cassandra</groupId>
        <artifactId>cassandra-all</artifactId>
        <version>3.6</version>
    </dependency>
</dependencies>

并且代码必须是这样的,它对我有用

import java.util.*;
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.triggers.ITrigger;
import org.apache.cassandra.utils.FBUtilities;
import static org.apache.cassandra.cql3.CQL3Type.logger;

public class CassandraTrigger implements ITrigger{
    public Collection<Mutation> augment(Partition update) {
        //Hard code properties, You can do that however you want
        String auditKeyspace = "test_networktopology";
        String auditTable = "timestamptest";
        RowUpdateBuilder audit = new RowUpdateBuilder(Schema.instance.getCFMetaData(auditKeyspace, auditTable),
                FBUtilities.timestampMicros(),
                update.partitionKey());
        Calendar calendar = Calendar.getInstance();
        audit.add("time", calendar.getTime());
        return Collections.singletonList(audit.build());
    }
  }