카 산 드 라 Example 의 한 가지 이해 와 의문

더 읽 기
드디어 시간 이 나 면 카 산 드 라 를 보고 기록 해 보 세 요.잘못 이해 할 수 있 는 부분 이 많 으 니 지적 해 주세요.
package com.demo;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
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.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.SuperColumn;
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.TSocket;
import org.apache.thrift.transport.TTransport;

public class ThriftExample {

	public static final String UTF8 = "UTF8";

	public static void main(String[] args) throws UnsupportedEncodingException, InvalidRequestException,
			UnavailableException, TimedOutException, TException, NotFoundException {
		TTransport tr = new TSocket("localhost", 9160);
		TProtocol proto = new TBinaryProtocol(tr);
		Cassandra.Client client = new Cassandra.Client(proto);//        
		tr.open();

		//         ,         keyspace,keyspace storage-conf   
		String keyspace = "Keyspace1";

		// columnFamily,      ,       row,         column,      column ,     column     
		// ,column     :         
		// struct Column {
		// 1: binary name,
		// 2: binary value,
		// 3: i64 timestamp,
		// }
		//   ColumnFamily         ,
		String table = "Standard1";
		// key,        
		String keyUserID = "1";
		String keyUserID2 = "2";
		// insert data
		long timestamp = System.currentTimeMillis();

		// ColumnPath        ,   columnFamily,    
		ColumnPath colPathName = new ColumnPath(table);

		//   column,   fullName
		colPathName.setColumn("fullName".getBytes(UTF8));

		//     ,       :
		// keyspace:         
		// keyUserID:  key,    key         
		// colPathName:     ,       Standard1  ,   fullName
		// "Chris Goffinet".getBytes(UTF8) :      
		// timestamp:  wiki            
		// ConsistencyLevel.ONE:     ,one                 1    commit      ;            
		client.insert(keyspace, keyUserID, colPathName, "  · ".getBytes(UTF8), timestamp,
				ConsistencyLevel.ONE);

		ColumnPath colPathAge = new ColumnPath(table);
		//   column,   fullName
		colPathAge.setColumn("age".getBytes(UTF8));

		client.insert(keyspace, keyUserID, colPathAge, "24".getBytes(UTF8), timestamp, ConsistencyLevel.ONE);
		//       cassandra        ,           ,      key:keyUserID
		//     ,  key    column,      superColumn

		// read single column
		//           
		//          ,        ,       :keyspace,   key:keyUserID
		//      column:colPathName,        ColumnOrSuperColumn
		// ColumnOrSuperColumn    ,ColumnOrSuperColumn   column   super_column    ,         
		//     column
		Column col = client.get(keyspace, keyUserID, colPathName, ConsistencyLevel.ONE).column;

		System.out.println("column name: " + new String(col.name, UTF8));
		System.out.println("column name: " + new String(col.value, UTF8));
		System.out.println("column timestamp: " + new Date(col.timestamp));

		//      
		SlicePredicate predicate = new SlicePredicate();
		SliceRange sliceRange = new SliceRange();
		sliceRange.setStart(new byte[0]);
		sliceRange.setFinish(new byte[0]);
		predicate.setSlice_range(sliceRange);
		System.out.println("
:"); // ColumnParent is used when selecting groups of columns from the same ColumnFamily // In directory structure terms, imagine * ColumnParent as ColumnPath + '/../' ColumnParent parent = new ColumnParent(table); // client.get_slice , List // keyspace: // keyUserID: key // parent : // predicate: // ConsistencyLevel.ONE : List results = client.get_slice(keyspace, keyUserID, parent, predicate, ConsistencyLevel.ONE); for (ColumnOrSuperColumn result : results) { Column column = result.column; System.out.println(new String(column.name, UTF8) + " -> " + new String(column.value, UTF8)); } System.out.println("
:"); List keys = new ArrayList(); keys.add(keyUserID2); keys.add(keyUserID); ColumnPath columnPath = new ColumnPath(table); columnPath.setColumn("fullName".getBytes(UTF8)); Map resultsMap = client.multiget(keyspace, keys, columnPath, ConsistencyLevel.ONE); for (String key : resultsMap.keySet()) { ColumnOrSuperColumn columnOrSuperColumn = resultsMap.get(key); Column column = columnOrSuperColumn.column; printColumn(column); } // // ColumnPath removecolumnPath = new ColumnPath(table); // client.remove(keyspace, keyUserID, removecolumnPath, timestamp, ConsistencyLevel.ONE); tr.close(); } public static void printSuperColumn(SuperColumn superColumn) { List list = superColumn.columns; for (Column c : list) { byte[] name = c.name; byte[] value = c.value; try { System.out.println(new String(name, UTF8) + " : " + new String(value, UTF8)); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } } public static void printColumn(Column c) { byte[] name = c.name; byte[] value = c.value; try { System.out.println(new String(name, UTF8) + " : " + new String(value, UTF8)); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } }

이것 은 간단 한 demo 일 뿐 입 니 다. 아직도 많은 부분 을 이해 하지 못 했 습 니 다. 예 를 들 어 SuperColumn, 왜 Column Family 에서 SuperColumn 이나 Column 만 존재 할 수 있 고 동시에 존재 하지 못 하 는 지, 내재 적 인 원 리 를 이해 하지 못 했 습 니 다. 그리고 어떻게 하 는 지 조회 도 있 습 니 다. key - value 는 key 를 통 해서 만 value 를 얻 을 수 있 습 니 다. 많은 경우 에 우 리 는 value 가 그 key 를 찾 고 싶다 는 것 을 알 게 되 었 습 니 다.이렇게 하면 우 리 는 우리 가 조회 하고 자 하 는 value 를 key 로 만 들 수 있 습 니 다. 조회 할 key 를 value 로 삽입 하여 나중에 조회 할 수 있 도록 합 니 다. 그러면 얼마나 번 거 로 운 지, 때로는 조회 조건 이 확실 하지 않 을 때 도 있 습 니 다.지금 루 셀 로 색인 을 만 들 수 있 을 지 없 을 지 모 르 겠 어 요.여러분, 누가 실제 프로젝트 를 만들어 서 나 와 서 말씀 해 보 세 요.
  • cassandra6.rar (7 KB)
  • 다운로드 횟수: 34
  • 좋은 웹페이지 즐겨찾기