Hector + Cassandra:获取列族列表

时间:2011-08-24 22:56:31

标签: list cassandra hector

我正在尝试获取我正在使用的当前键空间中的所有列系列,因为我想摆脱错误:

InvalidRequestException(why:[column family] already exists in keyspace)

我的逻辑是将所有列族都放在当前的密钥空间中。检查返回列表中是否显示特定列族。所以,我试试:

KeyspaceDefinition keyspaceDef = HFactory.createKeyspaceDefinition("test");

...

List<ColumnFamilyDefinition> lsCf = keyspaceDef.getCfDefs();

创建

似乎有问题

List<ColumnFamilyDefinition> lsCf = keyspaceDef.getCfDefs();

我做了System.out.println(keyspaceDef.getCfDefs())并返回了

[]

一个空列表 - 这是我的预期。我无法理解的是为什么List<ColumnFamilyDefinition> lsCf = keyspaceDef.getCfDefs();不正确。 Eclipse不同意此行的“List”部分。除此之外,似乎他的代码是正确的。有人可以帮助我理解为什么这条线路错了或我的方法是否已关闭?

以下是完整的代码段:

package org.cassandra.examples;

import me.prettyprint.cassandra.model.BasicColumnFamilyDefinition;
import me.prettyprint.cassandra.service.CassandraHostConfigurator;
import me.prettyprint.cassandra.service.ThriftCfDef;
import me.prettyprint.cassandra.service.ThriftCluster;
import me.prettyprint.hector.api.Cluster;
import me.prettyprint.hector.api.Keyspace;
import me.prettyprint.hector.api.exceptions.HectorException;
import me.prettyprint.hector.api.factory.HFactory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;

import me.prettyprint.cassandra.serializers.StringSerializer;
import me.prettyprint.cassandra.service.*;
import me.prettyprint.cassandra.model.BasicColumnDefinition;
import me.prettyprint.cassandra.model.BasicColumnFamilyDefinition;
import me.prettyprint.cassandra.model.thrift.ThriftConverter;

import org.apache.cassandra.db.ColumnFamily;
import org.apache.cassandra.thrift.Cassandra;
import me.prettyprint.cassandra.service.ThriftKsDef;
import me.prettyprint.hector.api.*;
import me.prettyprint.hector.api.beans.HColumn;
import me.prettyprint.hector.api.beans.HSuperColumn;
import me.prettyprint.hector.api.ddl.ColumnFamilyDefinition;
import me.prettyprint.hector.api.ddl.ColumnIndexType;
import me.prettyprint.hector.api.ddl.ComparatorType;
import me.prettyprint.hector.api.ddl.KeyspaceDefinition;
import me.prettyprint.hector.api.factory.HFactory;
import me.prettyprint.hector.api.mutation.Mutator;
import me.prettyprint.cassandra.service.template.ColumnFamilyTemplate;
import me.prettyprint.cassandra.service.template.ColumnFamilyUpdater;
import me.prettyprint.cassandra.service.template.ThriftColumnFamilyTemplate;
import me.prettyprint.hector.api.mutation.Mutator;
import me.prettyprint.hector.api.query.ColumnQuery;
import me.prettyprint.hector.api.query.QueryResult;
import me.prettyprint.hector.api.query.SuperColumnQuery;

public class HectorTest {
    private static String keyspaceName = "test3";
    private static KeyspaceDefinition newKeyspaceDef;
    private static Cluster cluster;
    private static Keyspace ksp;

    public static void main(String[] args) {

        cluster = HFactory.getOrCreateCluster("test cluster", "xxx.xxx.x.xx:9160");
        newKeyspaceDef = HFactory.createKeyspaceDefinition(keyspaceName);
        ColumnFamilyDefinition cfDef = HFactory.createColumnFamilyDefinition("MyKeyspace",                              
                "ColumnFamilyName", 
                ComparatorType.BYTESTYPE);
        List<ColumnFamilyDefinition> lCf = newKeyspaceDef.getCfDefs(); //= new ArrayList<ColumnFamilyDefinition>();
        if((cluster.describeKeyspace(keyspaceName)) == null){
            createSchema();
        }

        ksp = HFactory.createKeyspace(keyspaceName, cluster);
        //Articles art = new Articles(cluster, newKeyspaceDef);
        //cluster.dropColumnFamily(keyspaceName, "Articles");


    }

    public static void createSchema(){
        cluster.addKeyspace(newKeyspaceDef,true);
    }
}

错误:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    List cannot be resolved to a type

    at org.cassandra.examples.HectorTest.main(HectorTest.java:55)

1 个答案:

答案 0 :(得分:3)

添加

import java.util.List; 

给你进口。在eclipse中,CTRL-SHIFT-O将为您组织导入,并添加任何缺失的内容。