카테고리 보관물: Apache Project/Cassandra

org.apache.cassandra.db.marshal.MarshalException: cannot parse ‘xxx’ as hex bytes 에러시..

오랜만에 cassandra-cli로 키스페이스 만들고, 컬럼에 데이터 넣고..테스트를 하는데

org.apache.cassandra.db.marshal.MarshalException: cannot parse ‘xxx’ as hex bytes

위에처럼, 컬럼 이름(xxx)을 파싱할 수 없다고 한다. 헉..^^;;모지… 모지.. 찾아보니..
요기, 요기를 살펴보세요.. ^^

위 링크를 살펴보니, key_validation_class=UTF8Type <– 이거 추가해줘야 하네요..

카산드라가 너무 자주 업데이트 되니, 따라가는 것도 벅찰때가 있네요.. ^^;;
결론은, 아래처럼 validation class로 utf8type을 줘서 컬럼 패밀리를 만든다고 하네요. ^^

create column family Users with comparator=UTF8Type 
and default_validation_class=UTF8Type and key_validation_class=UTF8Type;

Cassandra.Client 풀링하기..

Cassandra.Client는 org.apache.cassandra.thrift 패키지의 클래스이고, 결국, Client는 Cassandra의 Inner 클래스가 됩니다..그리고, 위 패키지는 Thrift라는 데이타 serialize/deserialize 라이브러리(Google Protocol Buffers랑 비슷)를 통해서 전송될 데이타를 만들고, 전송된 데이타를 처리하고 있겠죵.. Cassandra의 언어별 클라이언트 라이브러리는 http://wiki.apache.org/cassandra/ClientOptions 페이지에 자세히 기술이 되어 있습니다.. 클라이언트 라이브러리들은 보통 Cassandra 서버에 붙는 Connection에 대한 풀링을 제공하는데, Connection 풀링은 org.apache.cassandra.thrift 패키지의 Cassandra.Client를 풀링해서 구현 할 수 있습니다. 
아래 코드는 Cassandra.Client의 풀링을 통한 Cassandra 서버의 연결에 대한 풀링기능을 제공하고 있습니다..^^

* CassandraClientArrayFactory.java



package net.sjava.cassandra.test;


import java.util.Random;
import java.util.concurrent.ConcurrentLinkedQueue;


import org.apache.cassandra.thrift.Cassandra;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;


/**
 * Cassandra.Client 풀링 클래스
 *
 * @author mcsong@gmail.com
 * @since 2010/11/30
 *
 */
public class CassandraClientArrayFactory {
 /**
  * host array
  */
 private static String[] hosts = “127.0.0.1,127.0.0.1”.split(“,”) ;
 /**
  * port
  */
 private static int port = 9160;
 /**
  * pooing count
  */
 private static int count = 10;
 
 //
 private static ConcurrentLinkedQueue<Cassandra.Client> clients = new  ConcurrentLinkedQueue<Cassandra.Client>();
 
 /**
  * Cassandra.Client를 생성한다.
  * @return
  * @throws Exception
  */
 private static Cassandra.Client createCassandraClient() throws Exception {
  int index = new Random().nextInt(hosts.length);
  TTransport socket = new TSocket(hosts[index], port);
  socket.open();
  return new Cassandra.Client(new TBinaryProtocol(socket, false, false));
 }


 /**
  * Cassandra.Client를 풀에서 가져온다.
  * @return
  * @throws Exception
  */
 public static Cassandra.Client poll() throws Exception {
  if (!clients.isEmpty() && clients.peek() != null)
   return clients.poll();
  
  return createCassandraClient();
 }
 
 /**
  * Cassandra.Client를 풀에 넣는다.
  * @param client
  * @throws Exception
  */
 public static void push(Cassandra.Client client) throws Exception {
  System.out.println(“channel size : ” + clients.size());
  if (clients.size() >= count) {
   client = null; // garbage
   return;
  }
  
  clients.offer(client);
 }
}




* CassandraClientArrayFactoryTest.java 



package net.sjava.cassandra.test;


import org.apache.cassandra.thrift.Cassandra;
import org.apache.cassandra.thrift.Column;
import org.apache.cassandra.thrift.ColumnPath;
import org.apache.cassandra.thrift.ConsistencyLevel;


import static net.sjava.cassandra.test.CassandraClientArrayFactory.poll;
import static net.sjava.cassandra.test.CassandraClientArrayFactory.push;


public class CassandraClientArrayFactoryTest {


 public static void main(String[] args) {
  
  String keyspace = “Keyspace1”;
  String columnFamily = “Standard2”;
  String key = “key1”;
  long timestamp = System.currentTimeMillis();
  
  Cassandra.Client client = null;
  
  try {
   client = poll();   
   for(int i=0; i < 15; i++)
    push(client);
     
   String value =”bbbbbbbbbbbbbbbbbbbbbbbbbbbbb”;
   ColumnPath cPath = new ColumnPath(columnFamily);
   cPath.setColumn(“name”.getBytes(“utf-8”));
   client.insert(keyspace, key, cPath, value.getBytes(“utf-8”), timestamp, ConsistencyLevel.ONE);
   Column col = client.get(keyspace, key, cPath, ConsistencyLevel.ONE).getColumn();
   System.out.println(“Column name: ” + new String(col.name, “utf-8”)); 
   System.out.println(“Column value: ” + new String(col.value, “utf-8”)); 
  
   value =”ccccccccccccccccccccccccccccccccccccccc”;
   long time2 = System.currentTimeMillis();
   client.insert(keyspace, key, cPath, value.getBytes(“utf-8”), time2, ConsistencyLevel.ONE);
   Column col2 = client.get(keyspace, key, cPath, ConsistencyLevel.ONE).getColumn();
   System.out.println(“Column2 name: ” + new String(col2.name, “utf-8”)); 
   System.out.println(“Column2 value: ” + new String(col2.value, “utf-8”)); 
  } catch(Exception e) {
   e.printStackTrace();
  } finally {
   if(client != null)
    try {
     push(client);
    } catch (Exception e) {
     e.printStackTrace();
    }
  }
 }


}


Cassandra simple insert/get 하기..

Client 객체는 기본적으로, 카산드라 클라이언트(Cassandra.Client) 생성 예제를 같이 사용하고 있습니다.
가장 기초적인 1개의 Column에 데이타 insert/get 하는 예제입니다..

package net.sjava.cassandra.test;import org.apache.cassandra.thrift.Column;
import org.apache.cassandra.thrift.ColumnPath;
import org.apache.cassandra.thrift.ConsistencyLevel;

public class CassandraTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

CassandraClientFactory factory = CassandraClientFactory.getInstance();
String keyspace = “Keyspace1”;
String columnFamily = “Standard2”;
String key = “key1″;
long timestamp = System.currentTimeMillis();

String value =”aaaaaaaaaaaaaaaaaaa_bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb”;
try {
ColumnPath cPath = new ColumnPath(columnFamily);
cPath.setColumn(“name”.getBytes(“utf-8”));
factory.getClient().insert(keyspace, key, cPath, value.getBytes(“utf-8”), timestamp, ConsistencyLevel.ONE);
Column col = factory.getClient().get(keyspace, key, cPath, ConsistencyLevel.ONE).getColumn();
System.out.println(“Column name: ” + new String(col.name, “utf-8”));
System.out.println(“Column value: ” + new String(col.value, “utf-8″));

value =”aaaaaaaaaa”;

long time2 = System.currentTimeMillis();
factory.getClient().insert(keyspace, key, cPath, value.getBytes(“utf-8”), time2, ConsistencyLevel.ONE);
Column col2 = factory.getClient().get(keyspace, key, cPath, ConsistencyLevel.ONE).getColumn();
System.out.println(“Column2 name: ” + new String(col2.name, “utf-8”));
System.out.println(“Column2 value: ” + new String(col2.value, “utf-8”));
} catch(Exception e) {
e.printStackTrace();
}
}
}

 

Cassandra에 대한 좋은 설명과 예제들..

Cassandra에 대한 좋은 설명과 예제들..

1. Getting Started With Cassandra <– 강추..

2. http://nosql.mypopescu.com/tagged/cassandra

3. http://blog.evanweaver.com/articles/2009/07/06/up-and-running-with-cassandra/

4. http://www.sodeso.nl/?cat=10 <– 여기도 강추..
4.1 Installing and using Apache Cassandra With Java Part 1 (Installation)
4.2 Installing and using Apache Cassandra With Java Part 2 (Data model)
4.3 Installing and using Apache Cassandra With Java Part 3 (Data model 2)
4.4 Installing and using Apache Cassandra With Java Part 4 (Thrift Client)
4.5 Installing and using Apache Cassandra With Java Part 5 (Thrift Client 2)