1.数据模型图
http://www.ibm.com/developerworks/cn/opensource/os-cn-cassandra/ 看那个keyspace图1
http://wiki.apache.org/cassandra/DataModel
2. Hector client example
package test.cassandra;
import java.util.Arrays;
import me.prettyprint.cassandra.serializers.StringSerializer;
import me.prettyprint.cassandra.service.ThriftKsDef;
import me.prettyprint.cassandra.service.template.ColumnFamilyResult;
import me.prettyprint.cassandra.service.template.ColumnFamilyTemplate;
import me.prettyprint.cassandra.service.template.ColumnFamilyUpdater;
import me.prettyprint.cassandra.service.template.ThriftColumnFamilyTemplate;
import me.prettyprint.hector.api.Cluster;
import me.prettyprint.hector.api.Keyspace;
import me.prettyprint.hector.api.ddl.ColumnFamilyDefinition;
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;
/**
*
* the code from https://github.com/rantav/hector/wiki/Getting-started-%285-minutes%29
* @author
*
*/
public class TestHector {
public static void main(String[] args) {
Cluster cluster = HFactory.getOrCreateCluster("test-cluster","172.16.42.56:9160");
//keyspace
final String keyspaceName = "ksTest1";
//column family
final String cfUser = "User";
//column pwd
final String cUser_Pwd = "Pwd";
//column realname
final String cUser_RealName = "RealName";
try{
KeyspaceDefinition keyspaceDef = cluster.describeKeyspace(keyspaceName);
if (keyspaceDef == null){
//setup schema if not exist
ColumnFamilyDefinition cfDef = HFactory.createColumnFamilyDefinition(keyspaceName,
cfUser,
ComparatorType.UTF8TYPE);
KeyspaceDefinition newKeyspace = HFactory.createKeyspaceDefinition(keyspaceName,
ThriftKsDef.DEF_STRATEGY_CLASS,
1,
Arrays.asList(cfDef));
cluster.addKeyspace(newKeyspace, true);
}
//create a keyspace object
Keyspace ksp = HFactory.createKeyspace(keyspaceName, cluster);
ColumnFamilyTemplate<String, String> template =
new ThriftColumnFamilyTemplate<String, String>(ksp, cfUser, StringSerializer.get(), StringSerializer.get());
try {
//a key
final String keyUser1 = "zhang";
//read
ColumnFamilyResult<String, String> res = template.queryColumns(keyUser1);
if (res.hasResults()){
System.out.println("get data: " + keyUser1);
String value = res.getString(cUser_Pwd);
System.out.println(cUser_Pwd + ": " + value);
value = res.getString(cUser_RealName);
System.out.println(cUser_RealName + ": " + value);
//delete
try {
System.out.println("del data");
template.deleteRow(keyUser1);
//template.deleteColumn(keyRotuon, cUser_Pwd);
} catch (HectorException e) {
e.printStackTrace();
}
}
else{
//update
System.out.println("no data, add one");
// <String, String> correspond to key and Column name.
ColumnFamilyUpdater<String, String> updater = template.createUpdater(keyUser1);
updater.setString(cUser_Pwd, "123456");
updater.setString(cUser_RealName, "zhangg");
try {
template.update(updater);
} catch (HectorException e) {
e.printStackTrace();
}
}
} catch (HectorException e) {
e.printStackTrace();
}
}
catch (Exception e) {
e.printStackTrace();
}
cluster.getConnectionManager().shutdown();
}
}
参考
http://cassandra.apache.org/
http://database.51cto.com/art/201006/203344.htm
http://www.fly3q.com/2010/11/cassandra-java-api-to-use.html
http://www.allthingsdistributed.com/
http://database.51cto.com/art/201005/202061.htm
分享到:
相关推荐
scratch少儿编程逻辑思维游戏源码-减压游戏.zip
少儿编程scratch项目源代码文件案例素材-英勇之鸟.zip
少儿编程scratch项目源代码文件案例素材-羊羊繁殖V2.5.zip
scratch少儿编程逻辑思维游戏源码-加特林混战 多人游戏.zip
少儿编程scratch项目源代码文件案例素材-西装英雄.zip
在使用 R 语言进行科研绘图时,颜色的选择是一件让人特别纠结的事情。本系列文章介绍了 R 语言科研绘图时常用的一些配色。本资源给大家提供了文章对应的 PPT。
scratch少儿编程逻辑思维游戏源码-海洋战争.zip
少儿编程scratch项目源代码文件案例素材-万圣夜惊魂.zip
HarmonyOS安全开发赋能,含有HarmonyOS多个场景的安全策略赋能
平台的服务端和客户端源码 整理出来的的游戏源码
scratch少儿编程逻辑思维游戏源码-街头霸王.zip
少儿编程scratch项目源代码文件案例素材-小船.zip
scratch少儿编程逻辑思维游戏源码-几何冲刺(4).zip
少儿编程scratch项目源代码文件案例素材-消灭病毒大作战.zip
scratch少儿编程逻辑思维游戏源码-功夫猫.zip
scratch少儿编程逻辑思维游戏源码-节奏积木 马里奥.zip
scratch少儿编程逻辑思维游戏源码-可爱的超级马里奥.zip
scratch少儿编程逻辑思维游戏源码-解压.zip
scratch少儿编程逻辑思维游戏源码-怪怪物种大作战.zip
少儿编程scratch项目源代码文件案例素材-跳舞的线-风暴.zip