Cassandra

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

 

 

你可能感兴趣的:(java,apache,html,cassandra)