2017년 9월 20일 수요일

Using spring data cassandra

You can use cassandra by using spring data cassandra.

First of all, you should set the access infomation to application.yml file matched your cassandra setting.

CREATE KEYSPACE myspace WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 3};

CREATE TABLE users (
    id UUID,
    name varchar,
    PRIMARY KEY (id)
);

spring:
  data:
    cassandra:
      contact-points: {server host}
      username: {username}
      password: {password}
      keyspace-name: myspace

And then, you can define the entity for your cassandra table.

package org.blog.test.user.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.cassandra.mapping.PrimaryKey;
import org.springframework.data.cassandra.mapping.Table;

import java.util.UUID;

@Table(value = "users")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {

    @PrimaryKey
    private UUID id;

    private String name;

}

After making repository, service layer, I set the command to add user when tomcat starting.

select * from users;

 id                                   | name
--------------------------------------+----------
 339bc454-91ae-48f7-bce7-f00e62a00d7f | testUser


You can download the example as below.

https://gitlab.com/shashaka/cassandra-project

댓글 없음 :

댓글 쓰기