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/
- 浏览: 2486717 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
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 714MySQL HA Solution 2019(4)MaxSca ... -
MySQL HA Solution 2019(3)MyCat
2019-07-30 11:45 326MySQL HA Solution 2019(3)MyCat ... -
MySQL HA Solution 2019(2)ProxySQL
2019-07-29 09:39 450MySQL HA Solution 2019(2)ProxyS ... -
MySQL HA Solution 2019(1)Master Slave on MySQL 5.7
2019-07-27 22:26 413MySQL HA Solution 2019(1)Master ... -
Zeppelin and Spark Library Dependency
2019-07-09 03:20 425Zeppelin and Spark Library Depe ... -
PostgreSQL 2018(3)Trigger
2018-10-30 00:12 461PostgreSQL 2018(3)Trigger Some ... -
PostgreSQL 2018(2)Dump the Data to CSV
2018-05-05 23:09 538PostgreSQL 2018(2)Dump the Data ... -
PostgreSQL 2018(1)Installation and Introduction
2018-05-04 01:45 588PostgreSQL 2018(1)Installation ... -
SOLR Performance and SolrJ(1)Client Compare Java VS PHP
2017-03-15 06:10 631SOLR Performance and SolrJ(1)Cl ... -
Regis Memory and Cleanup
2017-02-22 06:44 618Regis Memory and Cleanup Get t ... -
Mysql Database Backup and Dump
2016-12-22 06:06 669Mysql Database Backup and Dump ... -
Mysql Database Event and Procedure(3)Stored Procedure
2016-12-07 03:54 640Mysql Database Event and Proced ... -
Mysql Database Event and Procedure(2)Store Procedure
2016-12-07 03:38 547Mysql Database Event and Proced ... -
Mysql Fails on MAC
2015-10-20 03:06 745Mysql Fails on MAC Today, I pl ... -
FIPS and County Code Lookup
2015-10-13 05:19 953FIPS and County Code Lookup Th ... -
MAC OS with Latest Mysql
2015-01-07 12:35 919MAC OS with Latest Mysql C ... -
Cassandra Database(8)Upgrade to 2.1.2 and OpsCenter
2014-12-26 13:12 1208Cassandra Database(8)Upgrade t ... -
Kafka(6)How to Monitor Kafka
2014-08-23 04:19 2218Kafka(6)How to Monitor Kafka1. ... -
neo4j(4)Java Sample/Scala Client and Routing
2014-08-21 23:15 1371neo4j(4)Java Sample/Scala Clie ... -
neo4j(3)Setup neo4j Server and Know Concepts
2014-08-20 01:08 731neo4j(3)Setup neo4j Server and ...
相关推荐
java NoSql Cassandra hector 做的实例,拿出来分享
JAVA操作cassandra数据库,包括java代码及操作cassandra数据库总结文档。
java导出cassandra数据
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. ...
比较全面的cassandra java driver。全面解析,通过java如何操作cassandra,新增、删除、修改、查询数据。有很详细的例子。
cassandra数据库 java链接需要 jar包。 整合一起方便下载使用。
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-thrift cassandra-thrift cassandra-thrift cassandra-thrift cassandra-thrift cassandra-thrift
java连接cassandra数据库实现简单的增删查,数据库表看实体类,很简单,改的东西在application.properties里面
Cassandra is a distributed database that stands out thanks to its robust feature set and intuitive interface, while providing high availability and scalability of a distributed data store. This book ...
Apache Cassandra is a massively scalable, peer-to-peer database designed for 100 percent uptime, with deployments in the tens of thousands of nodes, all supporting petabytes of data. This book offers ...
Cassandra Java 示例 这个 repo 展示了一堆使用 Apache Cassandra 和 Datastax Java Driver for Apache Cassandra 的例子
on guide, you'll get all the details and practical examples you need to understand Cassandra's non-relational database design and put it to work in a production environment., Author Eben Hewitt (Java ...
Cassandra Java Client.zip
As Java, PHP, Python, and JavaScript are the most commonly used programming/scripting languages, author Vivek Mishra includes complete coverage of accessing Cassandra database with these languages ...
Cassandra The Definitive Guide(2nd) 英文azw3 第2版 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除