Hector 入门
导入jar包
为了能够使用hector操作Cassandra数据库,首先导入hector的jar包,注意根据实际情况修改版本号
<dependency>
<groupId>me.prettyprint</groupId>
<artifactId>hector-core</artifactId>
<version>1.0-2</version>
</dependency>
初始化一个集群
我们首先创建一个代表Cassandra集群的集群对象,值得注意的是集群的名字仅仅是一个集群标示,和真正的Cassandra集群没有关系。为了使代码更清晰,我们也要导入整个API包。
import me.prettyprint.hector.api.*;
....
Cluster myCluster = HFactory.getOrCreateCluster("test-cluster","localhost:9160");
我们来创建一个Schema
ColumnFamilyDefinition cfDef = HFactory.createColumnFamilyDefinition("MyKeyspace",
"ColumnFamilyName",
ComparatorType.BYTESTYPE);
KeyspaceDefinition newKeyspace = HFactory.createKeyspaceDefinition("MyKeyspace",
ThriftKsDef.DEF_STRATEGY_CLASS,
replicationFactor,
Arrays.asList(cfDef));
// 添加schema到集群
// 第二个参数为true意味着hector会阻塞直到所有的节点感知到操作
cluster.addKeyspace(newKeyspace, true);
一旦创建了Schema,前面的调用将抛出一个异常,表示我们正在尝试创建的Keyspace已经存在。为了解决这个问题,您可以用一个名为“createSchema()”的方法包装前面的代码;然后加上以下几行:
KeyspaceDefinition keyspaceDef = cluster.describeKeyspace("MyKeyspace");
// 如果Keyspace不存在且columnFamily不存在,则创建
if (keyspaceDef == null) {
createSchema();
}
最后我们创建Keyspace,它是一个长生命周期的组件,代表了我们要执行操作的cassandra的keyspace。
Keyspace ksp = HFactory.createKeyspace("MyKeyspace", myCluster);
你必须首先在Cassandra里面创建名为"MyKeyspace" 的Keyspace,才能执行上述操作。
创建一个 template
template和Keyspace一样也是一个长生命周期组件,理想情况下,您希望将模板对象保存在DAO中,以方便访问您的业务模型。
mport me.prettyprint.cassandra.service.template.ColumnFamilyTemplate;
......
ColumnFamilyTemplate<String, String> template =
new ThriftColumnFamilyTemplate<String, String>(ksp,
columnFamily,
StringSerializer.get(),
StringSerializer.get());
更新数据
// <String, String> correspond to key and Column name.
ColumnFamilyUpdater<String, String> updater = template.createUpdater("a key");
updater.setString("domain", "www.datastax.com");
updater.setLong("time", System.currentTimeMillis());
try {
template.update(updater);
} catch (HectorException e) {
// do something ...
}
读数据
try {
ColumnFamilyResult<String, String> res = template.queryColumns("a key");
String value = res.getString("domain");
// value should be "www.datastax.com" as per our previous insertion.
} catch (HectorException e) {
// do something ...
}
删除数据
try {
template.deleteColumn("key", "column name");
} catch (HectorException e) {
// do something
}
遍历Cloumn
SliceQuery<String, String, String> query = HFactory.createSliceQuery(ksp, StringSerializer.get(),
StringSerializer.get(), StringSerializer.get()).
setKey("a key").setColumnFamily(columnFamily);
ColumnSliceIterator<String, String, String> iterator =
new ColumnSliceIterator<String, String, String>(query, null, "\uFFFF", false);
while (iterator.hasNext()) {
// do something
}
翻译自:https://github.com/hector-client/hector/wiki/Getting-started-(5-minutes)
Hector 入门的更多相关文章
- SLAM入门必收藏的资料
搜集了各大网络,请教了SLAM大神,终于把SLAM的入门资料搜集全了!在分享资料前,我们先来看看,SLAM技术入门前需要具备哪些知识?首先学习SLAM需要会C和C++,网上很多代码还用了11标准的C+ ...
- Angular2入门系列教程7-HTTP(一)-使用Angular2自带的http进行网络请求
上一篇:Angular2入门系列教程6-路由(二)-使用多层级路由并在在路由中传递复杂参数 感觉这篇不是很好写,因为涉及到网络请求,如果采用真实的网络请求,这个例子大家拿到手估计还要自己写一个web ...
- ABP入门系列(1)——学习Abp框架之实操演练
作为.Net工地搬砖长工一名,一直致力于挖坑(Bug)填坑(Debug),但技术却不见长进.也曾热情于新技术的学习,憧憬过成为技术大拿.从前端到后端,从bootstrap到javascript,从py ...
- Oracle分析函数入门
一.Oracle分析函数入门 分析函数是什么?分析函数是Oracle专门用于解决复杂报表统计需求的功能强大的函数,它可以在数据中进行分组然后计算基于组的某种统计值,并且每一组的每一行都可以返回一个统计 ...
- Angular2入门系列教程6-路由(二)-使用多层级路由并在在路由中传递复杂参数
上一篇:Angular2入门系列教程5-路由(一)-使用简单的路由并在在路由中传递参数 之前介绍了简单的路由以及传参,这篇文章我们将要学习复杂一些的路由以及传递其他附加参数.一个好的路由系统可以使我们 ...
- Angular2入门系列教程5-路由(一)-使用简单的路由并在在路由中传递参数
上一篇:Angular2入门系列教程-服务 上一篇文章我们将Angular2的数据服务分离出来,学习了Angular2的依赖注入,这篇文章我们将要学习Angualr2的路由 为了编写样式方便,我们这篇 ...
- Angular2入门系列教程4-服务
上一篇文章 Angular2入门系列教程-多个组件,主从关系 在编程中,我们通常会将数据提供单独分离出来,以免在编写程序的过程中反复复制粘贴数据请求的代码 Angular2中提供了依赖注入的概念,使得 ...
- wepack+sass+vue 入门教程(三)
十一.安装sass文件转换为css需要的相关依赖包 npm install --save-dev sass-loader style-loader css-loader loader的作用是辅助web ...
- wepack+sass+vue 入门教程(二)
六.新建webpack配置文件 webpack.config.js 文件整体框架内容如下,后续会详细说明每个配置项的配置 webpack.config.js直接放在项目demo目录下 module.e ...
随机推荐
- Mathtype使用技巧
1. 打开/关闭MathType窗口 Alt+Ctrl+q:插入inline公式 Ctrl+S:更新公式到Word相应位置 Alt+F4:保存并关闭MathType窗口,返回Word. 2. 公式 ...
- Codeforces 660A. Co-prime Array 最大公约数
A. Co-prime Array time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- 跨页传值c#
Application (4)URL地址中的参数 (5)通过隐藏字段来传递数据 (6)Server.Transfer (7)通过序列化对象 (8)........ 下面就分别一一介绍: (1)使用Se ...
- Linux编程规范
1)在使用C语言进行编程时,源文件都必须加---文件头 /******************************************************** *文件名:test.c *创 ...
- mysql的UseAffectedRows问题 以及其他常见配置说明
遇到MySQL中on duplicate key update语句返回值不正确: 在server5.1.*的返回分别为insert=1,update=3,nochange=2 在server5.5.* ...
- 马婕 2014年MBA,mpacc备考 报刊宣读1 中国的电子商务(转)
http://blog.sina.com.cn/s/blog_3e66af4601015fxi.html 中国电子商务蓄势待发 Chinese e-commerce中国电子商务Pity the par ...
- [VC++入门]指针一
俗话说没有搞清楚指针就没有学会C/C++,所以指针是一个相当重要的东东,相当年在用 C#调用C++写的动态链接库时,以为C++中的指针就是C#中的引用类型(ref),但是看了一下却不是这样.指针当然和 ...
- Series转成list
直接list(series)就可以的 最佳的方式是将列表转换成Python中的科学计算包numpy包的array类型,再进行加减. 1 2 3 4 import numpy as np a = np. ...
- STL中的内存与效率
STL中的内存与效率 1. 使用reserve()函数提前设定容量大小,避免多次容量扩充操作导致效率低下. 关于STL容器,最令人称赞的特性之一就是是只要不超过它们的最大大小,它们就可以自动增长到足 ...
- hdu 5012 模拟+bfs
http://acm.hdu.edu.cn/showproblem.php?pid=5012 模拟出骰子四种反转方式,bfs,最多不会走超过6步 #include <cstdio> #in ...