添加数据
package com.guagua.test; import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer; import java.util.List; import org.apache.cassandra.thrift.Cassandra; import org.apache.cassandra.thrift.Column; import org.apache.cassandra.thrift.ColumnOrSuperColumn; import org.apache.cassandra.thrift.ColumnParent; import org.apache.cassandra.thrift.ColumnPath; import org.apache.cassandra.thrift.ConsistencyLevel; import org.apache.cassandra.thrift.InvalidRequestException; import org.apache.cassandra.thrift.NotFoundException; import org.apache.cassandra.thrift.SlicePredicate; import org.apache.cassandra.thrift.SliceRange; import org.apache.cassandra.thrift.TimedOutException; import org.apache.cassandra.thrift.UnavailableException; import org.apache.thrift.TException; import org.apache.thrift.protocol.TBinaryProtocol; import org.apache.thrift.protocol.TProtocol; import org.apache.thrift.transport.TFramedTransport; import org.apache.thrift.transport.TSocket; import org.apache.thrift.transport.TTransport; public class CClient { public static void main(String[] args) throws TException, InvalidRequestException, UnavailableException, UnsupportedEncodingException, NotFoundException, TimedOutException { //建立连接 TTransport tr = new TFramedTransport(new TSocket("192.168.100.108", 9160)); TProtocol proto = new TBinaryProtocol(tr); Cassandra.Client client = new Cassandra.Client(proto); tr.open(); String key_user_id = "1"; // insert data long timestamp = System.currentTimeMillis(); //相当于DB NAME client.set_keyspace("wyqTest"); //相当于DB Table ColumnParent parent = new ColumnParent("userprofile"); //字段名 Column nameColumn = new Column(toByteBuffer("name")); //字段值 nameColumn.setValue(toByteBuffer("Chris Goffinet")); //插入时间 nameColumn.setTimestamp(timestamp); //将数据添加到cassandra client.insert(toByteBuffer(key_user_id), parent, nameColumn, ConsistencyLevel.ONE); //字段名 Column ageColumn = new Column(toByteBuffer("age")); //字段值 ageColumn.setValue(toByteBuffer("24")); //插入时间 ageColumn.setTimestamp(timestamp); //将数据添加到cassandra client.insert(toByteBuffer(key_user_id), parent, ageColumn, ConsistencyLevel.ONE); //得到相当于DB Table ColumnPath path = new ColumnPath("userprofile"); // read single column path.setColumn(toByteBuffer("name")); System.out.println(client.get(toByteBuffer(key_user_id), path, ConsistencyLevel.ONE)); // read entire row SlicePredicate predicate = new SlicePredicate(); SliceRange sliceRange = new SliceRange(toByteBuffer(""), toByteBuffer(""), false, 10); predicate.setSlice_range(sliceRange); List<columnorsupercolumn> results = client.get_slice(toByteBuffer(key_user_id), parent, predicate, ConsistencyLevel.ONE); for (ColumnOrSuperColumn result : results) { Column column = result.column; System.out.println(toString(column.name) + " -> " + toString(column.value)); } tr.close(); } public static ByteBuffer toByteBuffer(String value) throws UnsupportedEncodingException { return ByteBuffer.wrap(value.getBytes("UTF-8")); } public static String toString(ByteBuffer buffer) throws UnsupportedEncodingException { byte[] bytes = new byte[buffer.remaining()]; buffer.get(bytes); return new String(bytes, "UTF-8"); } }?
得到数据
package com.test; import java.util.HashMap; import java.util.Map; import me.prettyprint.cassandra.model.AllOneConsistencyLevelPolicy; import me.prettyprint.cassandra.serializers.StringSerializer; import me.prettyprint.cassandra.service.FailoverPolicy; 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.exceptions.HectorException; import me.prettyprint.hector.api.factory.HFactory; public class MyTest { /** * @param args */ @SuppressWarnings("unchecked") public static void main(String[] args) { String keySpace = "wyqTest";//相当于DB NAME String columnFamily = "userprofile";//相当于DB Table Cluster cluster = HFactory.getOrCreateCluster("Test Cluster", "192.168.100.108:9160"); Map accessMap = new HashMap(); accessMap.put("username", "wyq"); accessMap.put("password", "123456"); Keyspace ksp = HFactory.createKeyspace(keySpace, cluster, new AllOneConsistencyLevelPolicy(), FailoverPolicy.ON_FAIL_TRY_ALL_AVAILABLE, accessMap); ColumnFamilyTemplate<String, String> template = new ThriftColumnFamilyTemplate<String, String>( ksp, columnFamily, StringSerializer.get(), StringSerializer .get()); ColumnFamilyUpdater<String, String> updater = template.createUpdater("u_1"); // 以下name,email,time相当于字段 updater.setString("name", "wyqa"); updater.setString("email", "anotherbug@163.com"); updater.setLong("time", System.currentTimeMillis()); try { template.update(updater); System.out.println("update ok."); } catch (HectorException e) { e.printStackTrace(); } try { ColumnFamilyResult<String, String> res = template .queryColumns("u_1"); ColumnFamilyResult<String, String> rest = template .queryColumns("1"); String name = res.getString("name"); String email = res.getString("email"); long time = res.getLong("time"); System.out.println("read u_1 name:" + name); System.out.println("read u_1 email:" + email); System.out.println("read u_1 time:" + time); System.out.println("age:" + rest.getString("age")); System.out.println("name:" + rest.getString("name")); } catch (HectorException e) { e.printStackTrace(); } } }
?