簡體   English   中英

我無法在Java中使用hector創建超級列

[英]I can't create a super column with hector in java

我對cassandra和hector還是陌生的,並試圖創建一個超級列。 我已經做了很多研究,但是到目前為止,仍然沒有任何效果。 在研究stackoverflow的過程中,我在這里發現了這個問題,這似乎對我有所幫助。 因此,我嘗試為示例插入代碼,但出現異常。

這是我的代碼(如果有hector,應該是可復制/可粘貼的)-很抱歉,它可能無法完美讀取,在詢問以下內容之前,我做了很多嘗試和嘗試:

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”類,但找不到。 您對我的代碼修改有什么想法或建議嗎? 我會很高興的。

非常感謝您與我分享的每一個想法。

我找到了問題的答案,並希望與他人分享(也許以后會對某人有所幫助)。 我認為解決方案非常簡單。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM