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)

原文地址:https://www.cnblogs.com/senlinyang/p/8182502.html