【问题标题】:Cassandra-Hector InvalidRequestExceptionCassandra-Hector InvalidRequestException
【发布时间】:2015-06-03 01:28:19
【问题描述】:

我正在尝试根据我的发现创建一个 Cassandra 数据库 here.

但无论我做什么,我都会不断收到此错误

原因: me.prettyprint.hector.api.exceptions.HInvalidRequestException: InvalidRequestException(why:Keyspace 名称必须不区分大小写唯一(“myKeyspace”与“myKeyspace”冲突)

这是我的代码:

public static void setSerializedMap(int index,String serializedVector){
    Cluster cluster = HFactory.getOrCreateCluster("TestCluster", "localhost:9160");

    ColumnFamilyDefinition columnFamilyDefinition=HFactory.createColumnFamilyDefinition("myKeyspace", "user", ComparatorType.BYTESTYPE);
    KeyspaceDefinition keyspaceDefinition=HFactory.createKeyspaceDefinition("myKeyspace",ThriftKsDef.DEF_STRATEGY_CLASS,3,Arrays.asList(columnFamilyDefinition));
    cluster.addKeyspace(keyspaceDefinition,true);

    Keyspace keyspace=HFactory.createKeyspace("Keyspace", cluster);

    Mutator<String> mutator = HFactory.createMutator(keyspace, me.prettyprint.cassandra.serializers.StringSerializer.get());
    try{
        mutator.addInsertion("cluster", "user", HFactory.createStringColumn("cluster-" + index, serializedVector));
    }catch(HectorException e){
        e.printStackTrace();
    }

}

关于如何修复它的任何建议?

【问题讨论】:

    标签: java cassandra hector


    【解决方案1】:

    在尝试创建之前检查“myKeyspace”是否存在,如果它已经存在,则无法创建它。

    if (cluster.describeKeyspace("myKeyspace") == null) { ...
    

    如果在创建时从多个位置/节点调用它,它可能会在您的代码中创建竞争条件。如果您可以升级,这将在 CQL 中解决。

    如果使用 CQL,您可以使用“CREATE KEYSPACE IF NOT EXISTS blarg WITH ...”。虽然如果使用 cql 可能要考虑从 hector 转移到为其设计的库之一。

    【讨论】:

      猜你喜欢
      • 2011-03-09
      • 1970-01-01
      • 2012-07-03
      • 2011-11-12
      • 2014-08-03
      • 2011-10-17
      • 2012-03-12
      • 1970-01-01
      • 2017-12-30
      相关资源
      最近更新 更多