0

我对 cassandra 和 hector 很陌生,并试图创建一个超级专栏。我已经做了很多研究,但不知何故,到目前为止没有任何效果。在我对 stackoverflow 的研究期间,我在这里发现了这个问题,这对我来说似乎很有帮助。因此,我尝试为我的示例插入代码,但出现异常。

这是我的代码(如果你有赫克托,应该是复制/粘贴) - 对不起,如果它可能不是完全可读,我在这里问之前做了很多尝试和错误:

import java.util.Arrays;
import me.prettyprint.cassandra.serializers.StringSerializer;
import me.prettyprint.cassandra.service.ThriftKsDef;
import me.prettyprint.cassandra.service.template.SuperCfTemplate;
import me.prettyprint.cassandra.service.template.SuperCfUpdater;
import me.prettyprint.cassandra.service.template.ThriftSuperCfTemplate;
import me.prettyprint.hector.api.Cluster;
import me.prettyprint.hector.api.Keyspace;
import me.prettyprint.hector.api.ddl.ColumnFamilyDefinition;
import me.prettyprint.hector.api.ddl.ComparatorType;
import me.prettyprint.hector.api.ddl.KeyspaceDefinition;
import me.prettyprint.hector.api.factory.HFactory;

public class DatabaseDataImporter {

    private Cluster myCluster;
    private KeyspaceDefinition keyspaceDefinition;
    private Keyspace keyspace;
    private SuperCfTemplate<String, String, String> template;

    final static StringSerializer ss = StringSerializer.get();

    public DatabaseDataImporter() {

        initializeCluster();

        SuperCfTemplate<String, String, String> template = new ThriftSuperCfTemplate<String, String, String>(
                keyspace, "Nodes", ss, ss, ss);
        SuperCfUpdater<String, String, String> updater = template
                .createUpdater("key", "newcf");
        updater.setString("subname", "1");
        template.update(updater);
    }

    private void initializeCluster() {
        // get Cluster
        myCluster = HFactory.getOrCreateCluster("Test Cluster",
                "localhost:9160");

        keyspaceDefinition = myCluster.describeKeyspace("Graphs");
        // If keyspace does not exist, the CFs don't exist either. => create
        // them.
        if (keyspaceDefinition == null) {
            createSchema();
        }

        keyspace = HFactory.createKeyspace("Graphs", myCluster);
    }

    private void createSchema() {
        // get Cluster
        Cluster myCluster = HFactory.getOrCreateCluster("Test Cluster",
                "localhost:9160");

        // add Schema
        int replicationFactor = 1;

        ColumnFamilyDefinition cfDef = HFactory.createColumnFamilyDefinition(
                "Graphs", "Nodes", ComparatorType.BYTESTYPE);

        KeyspaceDefinition newKeyspace = HFactory.createKeyspaceDefinition(
                "Graphs", ThriftKsDef.DEF_STRATEGY_CLASS, replicationFactor,
                Arrays.asList(cfDef));
        // Add the schema to the cluster.
        // "true" as the second param means that Hector will block until all
        // nodes see the change.
        myCluster.addKeyspace(newKeyspace, true);
    }

    public static void main(String[] args) {
        new DatabaseDataImporter();
    }

}

我得到的例外是:

Exception in thread "main" me.prettyprint.hector.api.exceptions.HInvalidRequestException: InvalidRequestException(why:supercolumn parameter is invalid for standard CF Nodes)
    at me.prettyprint.cassandra.service.ExceptionsTranslatorImpl.translate(ExceptionsTranslatorImpl.java:52)
    at me.prettyprint.cassandra.connection.HConnectionManager.operateWithFailover(HConnectionManager.java:260)
    at me.prettyprint.cassandra.model.ExecutingKeyspace.doExecuteOperation(ExecutingKeyspace.java:113)
    at me.prettyprint.cassandra.model.MutatorImpl.execute(MutatorImpl.java:243)
    at me.prettyprint.cassandra.service.template.AbstractColumnFamilyTemplate.executeBatch(AbstractColumnFamilyTemplate.java:115)
    at me.prettyprint.cassandra.service.template.AbstractColumnFamilyTemplate.executeIfNotBatched(AbstractColumnFamilyTemplate.java:159)
    at me.prettyprint.cassandra.service.template.SuperCfTemplate.update(SuperCfTemplate.java:203)
    at algorithms.DatabaseDataImporter.<init>(DatabaseDataImporter.java:43)
    at algorithms.DatabaseDataImporter.main(DatabaseDataImporter.java:87)
Caused by: InvalidRequestException(why:supercolumn parameter is invalid for standard CF Nodes)
    at org.apache.cassandra.thrift.Cassandra$batch_mutate_result.read(Cassandra.java:20833)
    at org.apache.thrift.TServiceClient.receiveBase(TServiceClient.java:78)
    at org.apache.cassandra.thrift.Cassandra$Client.recv_batch_mutate(Cassandra.java:964)
    at org.apache.cassandra.thrift.Cassandra$Client.batch_mutate(Cassandra.java:950)
    at me.prettyprint.cassandra.model.MutatorImpl$3.execute(MutatorImpl.java:246)
    at me.prettyprint.cassandra.model.MutatorImpl$3.execute(MutatorImpl.java:243)
    at me.prettyprint.cassandra.service.Operation.executeAndSetResult(Operation.java:104)
    at me.prettyprint.cassandra.connection.HConnectionManager.operateWithFailover(HConnectionManager.java:253)
    ... 7 more

我可以理解我以某种方式做了一件坏事,因为我试图将一个超级列插入标准列族。(来源在这里)。所以也许这是在创建过程中一切都被破坏的代码:

ColumnFamilyDefinition cfDef = HFactory.createColumnFamilyDefinition(
        "Graphs", "Nodes", ComparatorType.BYTESTYPE);

这是我不知道如何进行的地方。我试图找到一个“SuperColumnFamilyDefinition”类,但我找不到它。您有什么想法或建议我需要更改以修复我的代码吗?我会很高兴的。

非常感谢您与我分享的每一个想法。

4

1 回答 1

1

我找到了我的问题的答案并想分享(也许它对未来的人有帮助)。正如我认为的解决方案很简单。

ColumnFamilyDefinition cfDef = HFactory.createColumnFamilyDefinition(
                "Graphs", "Nodes", ComparatorType.BYTESTYPE);

需要扩展到

ColumnFamilyDefinition cfDef = HFactory.createColumnFamilyDefinition(
        "Graphs", "Nodes", ComparatorType.BYTESTYPE);
// defines it as super column
((ThriftCfDef) cfDef).setColumnType(ColumnType.SUPER);
于 2013-07-01T18:59:17.010 回答