如何使用Hector API创建超级列?

时间:2013-05-15 08:51:25

标签: cassandra hector

我的代码:

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

1 个答案:

答案 0 :(得分:1)

问题是您正在创建键空间列族,它将多次存储超级列。您无法做到这一点,键空间列族必须具有唯一名称。这就是异常告诉你的事情:

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

因此,就您的代码而言,您可以正确创建 supercolumn 。尝试使用Cassandra CLI进行验证。