Creating Keyspace, Column Family, Columns and Super Columns using Hector API in Cassandra



The Below Example Show how to Creates a Keyspace, a Column Family, Columns and Super Columns using Hector Api. Lets say we have:
Keyspace Name:                TestKeyspace
Column Family Name:     TestColumnFamily
Column Name:                   TestColumn
SuperColumn:                    TestSuperColumn


The Code also demonstrates, creating an index named TestColumn_idx on TestColumn.


1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import java.util.List;
import java.util.List;

import me.prettyprint.cassandra.model.BasicColumnDefinition;
import me.prettyprint.cassandra.model.BasicColumnFamilyDefinition;
import me.prettyprint.cassandra.serializers.StringSerializer;
import me.prettyprint.cassandra.service.ThriftCfDef;
import me.prettyprint.hector.api.Cluster;
import me.prettyprint.hector.api.ddl.*;
import me.prettyprint.hector.api.exceptions.HectorException;
import me.prettyprint.hector.api.factory.HFactory;

public class SchemaManipulation {
    
    private static final String TEST_KEYSPACE = "TestKeyspace";
    private static final String TEST_CF= "TestColumnFamily";
    private static final String TEST_SUPER= "TestSuperColumn";

    private static StringSerializer stringSerializer = StringSerializer.get();
    
    public static void main(String[] args) throws Exception {
        
        Cluster cluster = HFactory.getOrCreateCluster("TestCluster", "xxx.xxx.xxx.xxx:9160");
                
        try {
            if ( cluster.describeKeyspace(TEST_KEYSPACE ) != null ) {
              cluster.dropKeyspace(TEST_KEYSPACE );
            }
            
            BasicColumnDefinition columnDefinition = new BasicColumnDefinition();
            columnDefinition.setName(stringSerializer.toByteBuffer("TestColumn"));
            columnDefinition.setIndexName("TestColumn_idx ");
            columnDefinition.setIndexType(ColumnIndexType.KEYS);
            columnDefinition. setValidationClass(ComparatorType.LONGTYPE.getClassName());
            
            BasicColumnFamilyDefinition columnFamilyDefinition = new BasicColumnFamilyDefinition();
            columnFamilyDefinition.setKeyspaceName(TEST_KEYSPACE );
            columnFamilyDefinition.setName(TEST_CF);
            columnFamilyDefinition.addColumnDefinition(columnDefinition);
            
            BasicColumnFamilyDefinition superCfDefinition = new BasicColumnFamilyDefinition();
            superCfDefinition.setKeyspaceName(TEST_KEYSPACE );
            superCfDefinition.setName(TEST_SUPER);
            superCfDefinition.setColumnType(ColumnType.SUPER);
            
            
            
            ColumnFamilyDefinition cfDefStandard = new ThriftCfDef(columnFamilyDefinition);
            ColumnFamilyDefinition cfDefSuper = new ThriftCfDef(superCfDefinition);
            
            KeyspaceDefinition keyspaceDefinition = 
                HFactory.createKeyspaceDefinition(TEST_KEYSPACE , "org.apache.cassandra.locator.SimpleStrategy", 
                    1, Arrays.asList(cfDefStandard, cfDefSuper));
                                               
            cluster.addKeyspace(keyspaceDefinition);
            
           /* Below Code show your Keyspace Schema */
            
            List<KeyspaceDefinition> keyspaces = cluster.describeKeyspaces();
            for (KeyspaceDefinition kd : keyspaces) {
                if ( kd.getName().equals(TEST_KEYSPACE ) ) {
                    System.out.println("Name: " +kd.getName());
                    System.out.println("RF: " +kd.getReplicationFactor());
                    System.out.println("strategy class: " +kd.getStrategyClass());
                    List<ColumnFamilyDefinition> cfDefs = kd.getCfDefs();
                    for (ColumnFamilyDefinition def : cfDefs) {
                      System.out.println("  CF Type: " +def.getColumnType());
                      System.out.println("  CF Name: " +def.getName());
                      System.out.println("  CF Metadata: " +def.getColumnMetadata());  
                    }
                    
                    
                } 
            }
            
            
        } catch (HectorException he) {
            he.printStackTrace();
        }
        cluster.getConnectionManager().shutdown(); 
    }

}


Reference: github


Find Comments below or Add one

Post a Comment