如何使用hector api创建超级列?

r7xajy2e  于 2021-06-15  发布在  Cassandra
关注(0)|答案(1)|浏览(348)

我的代码:

public class InsertSuperColumn {

    private static StringSerializer stringSerializer = StringSerializer.get();

    public static void main(String[] args) throws Exception {

        Cluster cluster = HFactory.getOrCreateCluster("TestCluster", "localhost:9160");

        Keyspace keyspaceOperator = HFactory.createKeyspace("Keyspace1", cluster);

        try {
            Mutator<String> mutator = HFactory.createMutator(keyspaceOperator, stringSerializer);
            mutator.insert("billing", "Super1", HFactory.createSuperColumn("jsmith", 
                    Arrays.asList(HFactory.createStringColumn("first", "John")), 
                    stringSerializer, stringSerializer, stringSerializer));

            SuperColumnQuery<String, String, String, String> superColumnQuery = 
                HFactory.createSuperColumnQuery(keyspaceOperator, stringSerializer, stringSerializer, 
                        stringSerializer, stringSerializer);
            superColumnQuery.setColumnFamily("Super1").setKey("billing").setSuperName("jsmith");

            QueryResult<HSuperColumn<String, String, String>> result = superColumnQuery.execute();

            System.out.println("Read HSuperColumn from cassandra: " + result.get());            
            System.out.println("Verify on CLI with:  get Keyspace1.Super1['billing']['jsmith'] ");

        } catch (HectorException e) {
            e.printStackTrace();
        } 
        cluster.getConnectionManager().shutdown();  

    }
}

我得到这个错误:

Exception in thread "main" me.prettyprint.hector.api.exceptions.HInvalidRequestException: 
InvalidRequestException(why:Keyspace names must be case-insensitively unique ("new2" conflicts with "new2"))
px9o7tmv

px9o7tmv1#

问题是您正在创建keyspace和column族,它将多次存储超级列。不能这样做,键空间和列族必须具有唯一的名称。这就是例外告诉你的:

why:Keyspace names must be case-insensitively unique 
("new2" conflicts with "new2")

因此,就您的代码而言,您可以正确地创建超级列。尝试使用cassandra cli进行验证。

相关问题