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 ...
随机推荐
- Codeforces 599C. Day at the Beach 模拟
Day at the Beach time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- FTP 搭建
FTP 搭建 FTP 是 File Transfer Protocol(文件传输协议)的英文简称,它工作在 0SI 模型的第七层,TCP 模型的第四屋上,即应用层. 一.FTP 简介 FTP 会话时包 ...
- 优质产品需求文档(PRD)写作三大原则
在上一篇文章中有介绍,产品经理的两项主要职责包括:对产品机会进行评估,以及对开发的产品进行评估.而定义即将开发上线的产品,则需要借助产品需求文档,来进行产品的特征和功能描述.PRD文档的写作会因公司. ...
- 创建cookie
cookie的创建using System;using System.Collections.Generic;using System.Linq;using System.Web;using Syst ...
- msys2 命令行添加镜像地址
sed -i "1iServer = https://mirrors.tuna.tsinghua.edu.cn/msys2/mingw/i686" /etc/pacman.d/mi ...
- jar 包 的用处 ,dozer、poi、itext 、jxl 、jbarcode 、itextrenderer jquery 效果
1.dozer 做类型转换的, 新建 xml 文件 描述两个实体的对应关系 ,DozerBeanMapper mapper =new DozerBeanMapper().addMappingFiles ...
- exit--进程退出;wait--进程等待;execl--执行程序
函数原型:void exit(int status) 参数说明:退出状态. 函数原型:pid_t wait(int *status) 头文件:#include<sys/types.h>,# ...
- DataStage 八、清除日志
DataStage序列文章 DataStage 一.安装 DataStage 二.InfoSphere Information Server进程的启动和停止 DataStage 三.配置ODBC Da ...
- 2018.10.02 bzoj4009: [HNOI2015]接水果(整体二分)
传送门 整体二分好题. 考虑水果被盘子接住的条件. 不妨设水果表示的路径为(x1,y1)(x_1,y_1)(x1,y1),盘子表示的为(x2,y2)(x_2,y_2)(x2,y2) 不妨设df ...
- UVa 11367 Full Tank? (DP + Dijkstra)
题意:n个城市有m条道路.每个城市的油价不一样,给出起点s和终点t,以及汽车的油箱的容量,求从城市s到城市 t 的最便宜路径. 析:dp[u][i] 表示在第 u 个城市,还剩下 i L升油,一开始用 ...