赫克托+ Cassandra:获取列族列表

发布于 2024-12-01 01:38:31 字数 4110 浏览 3 评论 0原文

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

InvalidRequestException(why:[column family]已经存在于键空间中)

我的逻辑是获取当前键空间中的所有列族&检查特定的列族是否出现在返回的列表中。所以,我尝试:

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

...

List; lsCf = keyspaceDef.getCfDefs();

似乎存在问题

创建ListlsCf = keyspaceDef.getCfDefs();

我做了一个 System.out.println(keyspaceDef.getCfDefs()) 它返回了

[]

一个空列表 -这正是我所期望的。我不明白的是为什么 List? 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)

I am trying to get all the column families in the current keyspace I am using because I want to get rid of the error:

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

My logic is to get all the Column Families in the current key space & check whether or not a particular column family appears in the returned list. So, I try:

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

...

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

There seems to be a problem with creating the

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

I did a System.out.println(keyspaceDef.getCfDefs()) and it returned

[]

an empty list - which is what I expected. What I cannot understand is why List<ColumnFamilyDefinition> lsCf = keyspaceDef.getCfDefs(); is incorrect. Eclipse disagrees with the "List" portion of this line. Other than that, it appears that he code is right. Can some one please help me understand why this line is wrong or whether my approach is off?

Here's the full code snippet:

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

Error:

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鸠书 2024-12-08 01:38:31

添加

import java.util.List; 

到您的进口。在 Eclipse 中,CTRL-SHIFT-O 将为您组织导入,并添加任何缺少的内容。

Add

import java.util.List; 

to you imports. In eclipse, CTRL-SHIFT-O will organize your imports for you, and add anything that is missing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文