[Spring] Cassandra 연동 및 Insert
안녕하세요. yeTi입니다.
오늘은 Spring에서 Cassandra를 연동해보려고 합니다.
설치 환경
- Spring Boot : 1.5.4.RELEASE
- Mybatis : 1.3.2
1. Cassandra의 SDK를 설치합니다. (저는 Gradle 활용)
- compile group: 'com.datastax.cassandra', name: 'cassandra-driver-core', version: '3.5.1'
compile group: 'com.datastax.cassandra', name: 'cassandra-driver-mapping', version: '3.5.1'
compile group: 'com.datastax.cassandra', name: 'cassandra-driver-extras', version: '3.5.1'
2. Mapper Object를 생성합니다.
- @Table(keyspace = "[Keyspace 명]", name = "[Table 명]",
readConsistency = "QUORUM",
writeConsistency = "QUORUM",
caseSensitiveKeyspace = false,
caseSensitiveTable = false)
public class TestVO {
@PartitionKey
@Column(name = "storySeq")
private int storySeq;
@Column(name = "type")
private String type;
}
2. Cassnadra API를 사용합니다.
- PoolingOptions poolingOptions = new PoolingOptions();
Cluster cluster = null;
try {
cluster = Cluster.builder()
.addContactPoint("[서버 IP 주소]")
.withPoolingOptions(poolingOptions)
.build();
Session session = cluster.connect("[Keyspace]");
MappingManager manager = new MappingManager(session);
Mapper<TestVO> mapper = manager.mapper(TestVO.class);
TestVO testVO= new TestVO();
testVO.setStorySeq(123);
testVO.setType("test");
mapper.save(testVO);
} finally {
if (cluster != null) cluster.close();
}