cassandra 插入superColumn

阅读更多
找了很久没找到插入superColumn的api,后来发现有个batch_insert的方法,试了试果然可以,哈哈。
package com.cassandra.demo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.cassandra.thrift.Cassandra;
import org.apache.cassandra.thrift.Column;
import org.apache.cassandra.thrift.ColumnOrSuperColumn;
import org.apache.cassandra.thrift.ColumnPath;
import org.apache.cassandra.thrift.ConsistencyLevel;
import org.apache.cassandra.thrift.SuperColumn;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;

public class SuperColumnDemo {
	public String keyspace = "Keyspace1";
	public String columnFamily = "Super1";
	public String keyUserID = "1";
	public long time = System.currentTimeMillis();

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		SuperColumnDemo demo = new SuperColumnDemo();
		demo.insertSuperColumn();
		demo.getSuper();
	}

	/**
	 * 读取SuperColumn
	 * 
	 * @throws Exception
	 */
	public void getSuper() throws Exception {
		TTransport tt = new TSocket("localhost", 9160);
		TProtocol proto = new TBinaryProtocol(tt);
		Cassandra.Client client = new Cassandra.Client(proto);
		tt.open();
		ColumnPath path = new ColumnPath(columnFamily);
		path.setSuper_column("address".getBytes());
		ColumnOrSuperColumn s = client.get(keyspace, keyUserID, path, ConsistencyLevel.ONE);
		System.out.println(new String(s.super_column.columns.get(0).name, "utf8"));
		System.out.println(new String(s.super_column.columns.get(0).value, "utf8"));
		System.out.println(new String(s.super_column.columns.get(1).name, "utf8"));
		System.out.println(new String(s.super_column.columns.get(1).value, "utf8"));
	}

	/**
	 * 插入SuperColumn
	 * 
	 * @throws Exception
	 */
	public void insertSuperColumn() throws Exception {
		TTransport tt = new TSocket("localhost", 9160);
		TProtocol proto = new TBinaryProtocol(tt);
		Cassandra.Client client = new Cassandra.Client(proto);
		tt.open();

		Map> cfmap = getMap();
		client.batch_insert(keyspace, keyUserID, cfmap, ConsistencyLevel.ONE);
	}

	private Map> getMap() {
		Map> cfmap = new HashMap>();
		List cslist = new ArrayList();
		ColumnOrSuperColumn cos = new ColumnOrSuperColumn();

		List columnList = new ArrayList();
		Column city = new Column();
		city.setName("city".getBytes());
		city.setValue("杭州".getBytes());
		city.setTimestamp(time);
		Column province = new Column();
		province.setName("province".getBytes());
		province.setValue("浙江".getBytes());
		province.setTimestamp(time);
		columnList.add(city);
		columnList.add(province);

		SuperColumn sc = new SuperColumn();
		sc.setColumns(columnList);
		sc.setName("address".getBytes());
		cos.super_column = sc;

		cslist.add(cos);
		cfmap.put(columnFamily, cslist);
		return cfmap;
	}
}

  • cassandra.rar (4.7 KB)
  • 下载次数: 47

你可能感兴趣的:(Cassandra,Apache)