NoSQL/cassandra

Injecting dependencies Cassandra higher-level client Hector for Spring framework.

jeeyong 2011. 11. 10. 12:38
-- 2011. 11. 15. 사용자인증 프로세스 추가 

spring framework3 에서 Cassandra의  higher-level client  Hector 사용하기 위한 셋팅
이 문서는 기존에 Spring faramework에서 RDBMS에서 어노테이션의 @Autowired  통해 datasource를 가져다 쓰는 방법과 비슷하게 만드는것에 그목적이 있다. 아직 Nosql에 트렌젝션에 대한 지식이 없는 관계로 그러한 결과에 대한 셋팅이나 처리방법은 기술해 놓지않는다. 

spring3-jdbc.xml 소스

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:lang="http://www.springframework.org/schema/lang"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
    
    <bean id="cassandraHostConfigurator" class="me.prettyprint.cassandra.service.CassandraHostConfigurator">
        <constructor-arg value="localhost:9160"/>
    </bean>

    <bean id="cluster" class="me.prettyprint.cassandra.service.ThriftCluster">
    	<constructor-arg value="Test Cluster"/>
    	<constructor-arg ref="cassandraHostConfigurator"/>
    </bean>

    <bean id="consistencyLevelPolicy" class="me.prettyprint.cassandra.model.ConfigurableConsistencyLevel">
        <property name="defaultReadConsistencyLevel" value="ONE"/>
    </bean>

    <bean id="keyspaceHotelier" class="me.prettyprint.hector.api.factory.HFactory" factory-method="createKeyspace">
        <constructor-arg value="Hotelier"/>
        <constructor-arg ref="cluster"/>
        <constructor-arg ref="consistencyLevelPolicy"/>
<constructor-arg> <util:constant static-field="me.prettyprint.cassandra.service.FailoverPolicy.FAIL_FAST"/> </constructor-arg> <constructor-arg> <util:map> <entry key="username" value="계정입력"/> <entry key="password" value="패스워드입력"/> </util:map> </constructor-arg> </bean> </beans>
spring3-servlet.xml 소스
아래 코드를 입력해줘야 service단이나 controller단에서 @Autowired 를 통해 객체를 가져올수있다.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:lang="http://www.springframework.org/schema/lang"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

	<context:annotation-config />
	<context:component-scan  base-package="com.toz.controller" />
	<context:component-scan  base-package="com.toz.dao.cassandra" />
	

	<tx:annotation-driven />
	
    <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
    
    <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:messages" />
        <property name="defaultEncoding" value="UTF-8" />
    </bean>
</beans>

위에 설정은 객체를 사용하기위한 Spring 셋팅이고 이제 위에 셋팅된 상태에서 어떻게 가져다 쓰는지에 대해서 간단하게 소스로 설명하겠다. (service단은 생략하고 그냥 DAO에서 Keyspace 객체만 얻어서 사용하는 방법이 되겠다.)

HotelierCassandraDAO.java
package com.toz.dao.cassandra;

public interface HotelierCassandraDAO {
	public String getKeyspace();
}

HotelierCassandraDAOImpl.java
package com.toz.dao.cassandra;

import me.prettyprint.hector.api.Keyspace;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class HotelierCassandraDAOImpl implements HotelierCassandraDAO{
	@Autowired Keyspace keyspaceHotelier;

	@Override
	public String getKeyspace() {
		//
		return keyspaceHotelier.getKeyspaceName();
	}
	
}

이제 keyspace객체를 얻었으니 Hector의 API를 사용할수있다. 물론 이소스는 하니의 cluster에 대한 하나의 keyspace설정밖에 없지만 위의 spring3-jdbc.xml 소스에서 <bean id="keyspaceHotelier"> 를 복사해서 다른 ID로 다른 keyspace를 사용한다면 여러개의 keyspace에 대한 객체를 사용할 수 있다.
 

위의 interface HotelierCassandraDAO를 service단에서 기존에 RDBMS형태로 가져다가 쓰면 된다.
 
참조 문서
1.

me.prettyprint.cassandra.service.FailoverPolicy 

http://www.jarvana.com/jarvana/view/me/prettyprint/hector-core/0.7.0-27/hector-core-0.7.0-27-javadoc.jar!/me/prettyprint/cassandra/service/FailoverPolicy.html
https://github.com/rantav/hector/blob/master/core/src/main/java/me/prettyprint/cassandra/service/FailoverPolicy.java
Keyspace생성시 3번째 인자값으로 FailoverPolicy객체의 상수값이 들어간다. 위의 API를 참고해서 넣을것.

2.
spring xml설정중에 static 상수 값을 입력하는 방법
http://www.unicon.net/node/601 

3.
spring xml설정중에 map 입력 방법
http://www.roseindia.net/spring/calling-constructor-spring.shtml

4.
Hector에서 유저정보를 통해 Keyspace를 생성해서 사용하는 방법
http://blog.gmane.org/gmane.comp.db.hector.user/month=20110601 

'NoSQL > cassandra' 카테고리의 다른 글

아파치 분산 데이타 베이스 Cassandra  (0) 2011.04.19