六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 152|回复: 0

Cassandra

[复制链接]

升级  38.67%

28

主题

28

主题

28

主题

秀才

Rank: 2

积分
108
 楼主| 发表于 2013-2-7 20:00:28 | 显示全部楼层 |阅读模式
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");//keyspacefinal String keyspaceName = "ksTest1";//column familyfinal String cfUser = "User";//column pwdfinal String cUser_Pwd = "Pwd";//column realnamefinal String cUser_RealName = "RealName";try{KeyspaceDefinition keyspaceDef = cluster.describeKeyspace(keyspaceName);if (keyspaceDef == null){//setup schema if not existColumnFamilyDefinition 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 objectKeyspace ksp = HFactory.createKeyspace(keyspaceName, cluster);ColumnFamilyTemplate<String, String> template =                 new ThriftColumnFamilyTemplate<String, String>(ksp, cfUser, StringSerializer.get(), StringSerializer.get());try {//a keyfinal 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
 
 
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表