Question

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)
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top