Cassandra Database(2)Hector Java Class

Cassandra Database(2)Hector Java Class

1. Build Cassandra from Sources
Learn from the codes we already have. First get the latest cassandra source codes from git.
>git clone http://git-wip-us.apache.org/repos/asf/cassandra.git
>git branch -a
>git checkout cassandra-1.2
>ant

Build the binary file and deploy them to my local MAVEN.
>ant publish

2. Hector
2.1 Get the Latest Version
>git clone https://github.com/hector-client/hector.git
>cd hector
>mvn clean install
>mvn eclipse:eclipse

I would like to get to know more about the source codes, I am happy that this project is based on MAVEN.

I will smile that the hector-example is also built in MAVEN
>git clone https://github.com/zznate/hector-examples.git
>cd hector-example
>mvn clean install
>mvn eclipse:eclipse

2.2 Tools for cassandra
There are several tools
chiton
https://github.com/driftx/chiton

cassandra-gui
https://code.google.com/a/apache-extras.org/p/cassandra-gui/?redir=1

cassandra-cluster-admin
https://github.com/sebgiroux/Cassandra-Cluster-Admin

helenos
https://github.com/tomekkup/helenos

I prefer the cassandra-gui which is written by Java.

2.3 Examples of Java
package com.riptano.cassandra.hector.example;

import java.util.Arrays;
import java.util.List;

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.Keyspace;
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.ColumnType;
import me.prettyprint.hector.api.ddl.ComparatorType;
import me.prettyprint.hector.api.ddl.KeyspaceDefinition;
import me.prettyprint.hector.api.exceptions.HectorException;
import me.prettyprint.hector.api.factory.HFactory;
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 SchemaManipulation {
   
    private static final String DYN_KEYSPACE = "Keyspace1";
    private static final String DYN_CF = "User1";
    private static final String CF_SUPER = "Super1";
   
    private static StringSerializer stringSerializer = StringSerializer.get();
   
    public static void main(String[] args) throws Exception {
       
        Cluster cluster = HFactory.getOrCreateCluster("TestCluster", "localhost:9160");
               
        try {
            if ( cluster.describeKeyspace(DYN_KEYSPACE) != null ) {
              cluster.dropKeyspace(DYN_KEYSPACE);
            }
           
            BasicColumnFamilyDefinition columnFamilyDefinition = new BasicColumnFamilyDefinition();
            columnFamilyDefinition.setKeyspaceName(DYN_KEYSPACE);
            columnFamilyDefinition.setName(DYN_CF);
           
            columnFamilyDefinition.setKeyValidationClass("UTF8Type");
            columnFamilyDefinition.setComparatorType(ComparatorType.UTF8TYPE);
            columnFamilyDefinition.setDefaultValidationClass("UTF8Type");
           
            BasicColumnFamilyDefinition superCfDefinition = new BasicColumnFamilyDefinition();
            superCfDefinition.setKeyspaceName(DYN_KEYSPACE);
            superCfDefinition.setName(CF_SUPER);
           
            superCfDefinition.setColumnType(ColumnType.SUPER);
            superCfDefinition.setKeyValidationClass("UTF8Type");
            superCfDefinition.setComparatorType(ComparatorType.UTF8TYPE);
            superCfDefinition.setDefaultValidationClass("UTF8Type");
           
            ColumnFamilyDefinition cfDefStandard = new ThriftCfDef(columnFamilyDefinition);
            ColumnFamilyDefinition cfDefSuper = new ThriftCfDef(superCfDefinition);
           
            KeyspaceDefinition keyspaceDefinition =
                    HFactory.createKeyspaceDefinition(DYN_KEYSPACE, "org.apache.cassandra.locator.SimpleStrategy",
                        1, Arrays.asList(cfDefStandard,cfDefSuper));
                                              
            cluster.addKeyspace(keyspaceDefinition);
           
            // insert some data
           
            List<KeyspaceDefinition> keyspaces = cluster.describeKeyspaces();
            for (KeyspaceDefinition kd : keyspaces) {
                if ( kd.getName().equals(DYN_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()); 
                    }
                   
                   
                }
            }
           
            Keyspace keyspaceOperator = HFactory.createKeyspace(DYN_KEYSPACE, cluster);
           
            Mutator<String> mutator1 = HFactory.createMutator(keyspaceOperator, StringSerializer.get());
            mutator1.insert("1", DYN_CF, HFactory.createStringColumn("First", "Carl"));
            mutator1.insert("1", DYN_CF, HFactory.createStringColumn("Last", "Luo"));
           
            ColumnQuery<String, String, String> columnQuery = HFactory.createStringColumnQuery(keyspaceOperator);
            //columnQuery.setColumnFamily(DYN_CF).setKey("1").setName("First");
            columnQuery.setColumnFamily(DYN_CF).setKey("1").setName("First");
            QueryResult<HColumn<String, String>> result1 = columnQuery.execute();
           
            System.out.println("Read HColumn from cassandra: " + result1.get());           
            System.out.println("Verify on CLI with:  get User1['1']; ");
           
            Mutator<String> mutator2 = HFactory.createMutator(keyspaceOperator, stringSerializer);
            mutator2.insert("1", CF_SUPER, HFactory.createSuperColumn("sillycat",
                    Arrays.asList(HFactory.createStringColumn("First", "Carl"), HFactory.createStringColumn("Last", "Luo")),
                    stringSerializer, stringSerializer, stringSerializer));
           
            SuperColumnQuery<String, String, String, String> superColumnQuery =
                HFactory.createSuperColumnQuery(keyspaceOperator, stringSerializer, stringSerializer,
                        stringSerializer, stringSerializer);
            superColumnQuery.setColumnFamily(CF_SUPER).setKey("1").setSuperName("sillycat");

            QueryResult<HSuperColumn<String, String, String>> result2 = superColumnQuery.execute();

            System.out.println("Read HSuperColumn from cassandra: " + result2.get());           
            System.out.println("Verify on CLI with:  get Super1['1']['sillycat']; ");
           
        } catch (HectorException he) {
            he.printStackTrace();
        }
        cluster.getConnectionManager().shutdown();
    }
}


Tips
1. Error Message:
org.apache.cassandra.db.marshal.MarshalException: cannot parse 'key1' as hex bytes 
org.apache.cassandra.db.marshal.MarshalException: cannot parse 'password' as hex bytes
 
Solution:
columnFamilyDefinition.setKeyValidationClass("UTF8Type");columnFamilyDefinition.setComparatorType(ComparatorType.UTF8TYPE);columnFamilyDefinition.setDefaultValidationClass("UTF8Type");

And sometimes, we need to run these commands in the cassandra-cli
>assume User1 keys as UTF8Type;

References:
Hector
http://hector-client.github.io/hector/build/html/index.html
https://github.com/zznate/hector-examples
https://github.com/hector-client/hector

http://blog.csdn.net/redvalley/article/details/7291658

How to build cassandra
http://wiki.apache.org/cassandra/HowToBuild

kryo
https://code.google.com/p/kryo/







猜你喜欢

转载自sillycat.iteye.com/blog/2011524