`
sillycat
  • 浏览: 2542629 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

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/







分享到:
评论

相关推荐

    java NoSql Cassandra hector

    Java NoSQL Cassandra Hector详解 在当今大数据时代,非关系型数据库(NoSQL)因其灵活性、高可扩展性和高性能,越来越受到开发者的青睐。Cassandra,作为NoSQL数据库家族中的重要一员,尤其在大规模分布式存储系统...

    JAVA操作cassandra数据库

    在Java编程环境中,Cassandra数据库是一个高性能、分布式、NoSQL型的数据存储系统。它由Apache Software Foundation维护,设计用于处理大规模数据,具有高可用性和可扩展性。本篇将深入探讨如何使用Java来操作...

    Java连接cassandra实现简单的增删查demo

    在Java编程环境中,连接Cassandra数据库并实现基本的增、删、查操作是常见的任务。Cassandra是一款分布式NoSQL数据库,常用于处理大规模数据。在这个示例中,我们将探讨如何通过Java来操作Cassandra数据库。 首先,...

    cassandra数据库 java链接 jar包

    2. **创建Cluster实例**: 连接到Cassandra集群的第一步是创建一个`Cluster`实例。这需要提供至少一个Cassandra节点的地址,以及可选的配置参数: ```java Cluster cluster = Cluster.builder() .addContactPoint...

    cassandra javaDriver21.pdf

    2. **编写客户端**:说明如何创建第一个Cassandra客户端,连接到Cassandra集群。 3. **会话和CQL语句**:描述如何使用会话(Session)对象来执行CQL(Cassandra Query Language)语句。CQL是与Cassandra通信的主要...

    Cassandra JDBC Driver

    2. **遵循JDBC规范**:Cassandra JDBC Driver严格遵循Java Database Connectivity (JDBC) API规范,使得熟悉JDBC的开发者能够轻松地将Cassandra集成到他们的Java应用中,利用已有的JDBC知识进行数据存取操作。...

    Mastering.Apache.Cassandra.2nd.Edition.1784392618

    The book is aimed at intermediate developers with an understanding of core database concepts and want to become a master implementing Cassandra for their application. Table of Contents Chapter 1. ...

    java导出cassandra数据

    在Java中导出Cassandra数据是一项常见的任务,用于备份、迁移或者数据分析。Cassandra是一个分布式NoSQL数据库系统,被广泛应用于大数据场景。本教程将详细讲解如何使用Java API来实现Cassandra数据的导出。 首先,...

    Cassandra Java Client.zip

    《Cassandra Java客户端详解》 在分布式数据库领域,Apache Cassandra以其高可扩展性和容错性而备受关注。针对Java开发者,Cassandra提供了专门的Java客户端,使得与Cassandra数据库的交互变得更加便捷。本文将深入...

    NoSQL Web Development with Apache Cassandra(2015)

    Apache Cassandra is the most commonly used NoSQL database written in Java and is renowned in the industry as the only NoSQL solution that can accommodate the complex requirements of today’s modern ...

    Cassandra实战 java NoSql

    Cassandra实战 java NoSql

    sclo-cassandra3-cassandra-java-driver-3.1.4-2.el7.noarch.rpm

    官方离线安装包,亲测可用

    Cassandra JDBC Driver 0.8.2

    2. **遵循 JDBC 规范**:该驱动程序按照 JDBC(Java Database Connectivity)规范设计,提供了一个标准化的接口,使得开发人员能够在不深入了解 Cassandra 本身的底层机制的情况下,使用熟悉的 JDBC 代码来操作 ...

    存储数据(cassandra)

    此外,Cassandra还提供了Java驱动、Python驱动等多语言的客户端库,方便开发者进行数据交互。 ### 6. 安全与监控 Cassandra支持基本的身份验证和授权,可以通过SSL加密通信以提高安全性。监控方面,可以通过JMX、...

    cassandra-java-example:一堆使用 Apache Cassandra 和 Datastax Java 驱动程序的 Apache Cassandra 示例

    Datastax Java 驱动程序是 Cassandra 官方推荐的 Java 开发工具,它提供了高效、健壮且易于使用的 API,使得 Java 开发人员能够轻松地与 Cassandra 数据库进行通信。下面,我们将详细介绍如何利用这些组件来构建和...

    cassandra-thrift

    cassandra-thrift cassandra-thrift cassandra-thrift cassandra-thrift cassandra-thrift cassandra-thrift

    Cassandra-examples

    这个示例集主要针对Cassandra版本1.1.7,而Hector库是版本1.1-2,它是一个流行的客户端库,为Java开发者提供了方便的API来操作Cassandra。 Cassandra是一种分布式NoSQL数据库系统,常用于处理大规模数据存储和检索...

Global site tag (gtag.js) - Google Analytics