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/
- 浏览: 2542756 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
发表评论
-
MySQL HA Solution 2019(4)MaxScale
2019-08-03 21:19 788MySQL HA Solution 2019(4)MaxSca ... -
MySQL HA Solution 2019(3)MyCat
2019-07-30 11:45 369MySQL HA Solution 2019(3)MyCat ... -
MySQL HA Solution 2019(2)ProxySQL
2019-07-29 09:39 484MySQL HA Solution 2019(2)ProxyS ... -
MySQL HA Solution 2019(1)Master Slave on MySQL 5.7
2019-07-27 22:26 514MySQL HA Solution 2019(1)Master ... -
Zeppelin and Spark Library Dependency
2019-07-09 03:20 457Zeppelin and Spark Library Depe ... -
PostgreSQL 2018(3)Trigger
2018-10-30 00:12 486PostgreSQL 2018(3)Trigger Some ... -
PostgreSQL 2018(2)Dump the Data to CSV
2018-05-05 23:09 565PostgreSQL 2018(2)Dump the Data ... -
PostgreSQL 2018(1)Installation and Introduction
2018-05-04 01:45 631PostgreSQL 2018(1)Installation ... -
SOLR Performance and SolrJ(1)Client Compare Java VS PHP
2017-03-15 06:10 673SOLR Performance and SolrJ(1)Cl ... -
Regis Memory and Cleanup
2017-02-22 06:44 652Regis Memory and Cleanup Get t ... -
Mysql Database Backup and Dump
2016-12-22 06:06 700Mysql Database Backup and Dump ... -
Mysql Database Event and Procedure(3)Stored Procedure
2016-12-07 03:54 676Mysql Database Event and Proced ... -
Mysql Database Event and Procedure(2)Store Procedure
2016-12-07 03:38 583Mysql Database Event and Proced ... -
Mysql Fails on MAC
2015-10-20 03:06 783Mysql Fails on MAC Today, I pl ... -
FIPS and County Code Lookup
2015-10-13 05:19 985FIPS and County Code Lookup Th ... -
MAC OS with Latest Mysql
2015-01-07 12:35 966MAC OS with Latest Mysql C ... -
Cassandra Database(8)Upgrade to 2.1.2 and OpsCenter
2014-12-26 13:12 1243Cassandra Database(8)Upgrade t ... -
Kafka(6)How to Monitor Kafka
2014-08-23 04:19 2247Kafka(6)How to Monitor Kafka1. ... -
neo4j(4)Java Sample/Scala Client and Routing
2014-08-21 23:15 1396neo4j(4)Java Sample/Scala Clie ... -
neo4j(3)Setup neo4j Server and Know Concepts
2014-08-20 01:08 764neo4j(3)Setup neo4j Server and ...
相关推荐
Java NoSQL Cassandra Hector详解 在当今大数据时代,非关系型数据库(NoSQL)因其灵活性、高可扩展性和高性能,越来越受到开发者的青睐。Cassandra,作为NoSQL数据库家族中的重要一员,尤其在大规模分布式存储系统...
在Java编程环境中,Cassandra数据库是一个高性能、分布式、NoSQL型的数据存储系统。它由Apache Software Foundation维护,设计用于处理大规模数据,具有高可用性和可扩展性。本篇将深入探讨如何使用Java来操作...
在Java编程环境中,连接Cassandra数据库并实现基本的增、删、查操作是常见的任务。Cassandra是一款分布式NoSQL数据库,常用于处理大规模数据。在这个示例中,我们将探讨如何通过Java来操作Cassandra数据库。 首先,...
2. **创建Cluster实例**: 连接到Cassandra集群的第一步是创建一个`Cluster`实例。这需要提供至少一个Cassandra节点的地址,以及可选的配置参数: ```java Cluster cluster = Cluster.builder() .addContactPoint...
2. **编写客户端**:说明如何创建第一个Cassandra客户端,连接到Cassandra集群。 3. **会话和CQL语句**:描述如何使用会话(Session)对象来执行CQL(Cassandra Query Language)语句。CQL是与Cassandra通信的主要...
2. **遵循JDBC规范**:Cassandra JDBC Driver严格遵循Java Database Connectivity (JDBC) API规范,使得熟悉JDBC的开发者能够轻松地将Cassandra集成到他们的Java应用中,利用已有的JDBC知识进行数据存取操作。...
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数据是一项常见的任务,用于备份、迁移或者数据分析。Cassandra是一个分布式NoSQL数据库系统,被广泛应用于大数据场景。本教程将详细讲解如何使用Java API来实现Cassandra数据的导出。 首先,...
《Cassandra Java客户端详解》 在分布式数据库领域,Apache Cassandra以其高可扩展性和容错性而备受关注。针对Java开发者,Cassandra提供了专门的Java客户端,使得与Cassandra数据库的交互变得更加便捷。本文将深入...
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
官方离线安装包,亲测可用
2. **遵循 JDBC 规范**:该驱动程序按照 JDBC(Java Database Connectivity)规范设计,提供了一个标准化的接口,使得开发人员能够在不深入了解 Cassandra 本身的底层机制的情况下,使用熟悉的 JDBC 代码来操作 ...
此外,Cassandra还提供了Java驱动、Python驱动等多语言的客户端库,方便开发者进行数据交互。 ### 6. 安全与监控 Cassandra支持基本的身份验证和授权,可以通过SSL加密通信以提高安全性。监控方面,可以通过JMX、...
Datastax Java 驱动程序是 Cassandra 官方推荐的 Java 开发工具,它提供了高效、健壮且易于使用的 API,使得 Java 开发人员能够轻松地与 Cassandra 数据库进行通信。下面,我们将详细介绍如何利用这些组件来构建和...
cassandra-thrift cassandra-thrift cassandra-thrift cassandra-thrift cassandra-thrift cassandra-thrift
这个示例集主要针对Cassandra版本1.1.7,而Hector库是版本1.1-2,它是一个流行的客户端库,为Java开发者提供了方便的API来操作Cassandra。 Cassandra是一种分布式NoSQL数据库系统,常用于处理大规模数据存储和检索...