您的位置:首页 > 编程语言 > Java开发

Configuring Cassandra with Spring Boot using Kundera

2018-02-12 17:35 471 查看
转自

The Apache Cassandra database is the great choice when you need scalability and high adaptability without affecting performance.

As Cassandra is very scalable databse while working with springboot and its Benchmark Scalability was found on AWS was over a million writes per second.

Kundera is a “Polyglot Object Mapper” with a JPA interface. The thought behind Kundera is to influence working with NoSQL Databases to drop dead straightforward and fun. Kundera is being created with following goals:

To make working with NoSQL as straightforward as working with SQL

To fill in as JPA Compliant mapping answer for NoSQL Datastores.

To help engineers, to overlook the many-sided quality of NoSQL stores and concentrate on Domain Model.

To make exchanging crosswise over information stores as simple as changing an arrangement.

Configuration of Kundera with SpringBoot:

1.Add Maven Dependency in your pom.xml file:

<dependency>
<groupId>com.impetus.kundera.client</groupId>
<artifactId>kundera-cassandra</artifactId>
<version>3.10.1</version>
</dependency>
<dependency>
<groupId>com.impetus.kundera.client</groupId>
<artifactId>kundera-cassandra-ds-driver</artifactId>
<version>3.10.1</version>
</dependency>

<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-core</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-mapping</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-extras</artifactId>
<version>3.3.0</version>
</dependency>


2.Application Properties File:

spring.data.cassandra.keyspace-name=KunderaExamples
spring.data.cassandra.contact-points=localhost
spring.data.cassandra.port= 9042
spring.data.cassandra.cluster-name=Test Cluster
spring.data.cassandra.username=cassandra
spring.data.cassandra.password=cassandra


3.Persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"> <persistence-unit name="cassandra_pu">
<provider>com.impetus.kundera.KunderaPersistence</provider>
<!--         <class>com.paritytrading.parity.client.domain.User</class>  -->
<class>com.paritytrading.parity.client.domain.UserOrder</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="kundera.nodes" value="127.0.0.1" />
<property name="kundera.port" value="9042" />
<property name="kundera.keyspace" value="KunderaExamples" />
<property name="kundera.dialect" value="cassandra" />
<property name="kundera.ddl.auto.prepare" value="create" />
<property name="kundera.client.lookup.class"
value="com.impetus.kundera.client.cassandra.dsdriver.DSClientFactory" />
</properties>
</persistence-unit>
</persistence>


4.Entity Class of Kundera

@Entity
@Table(name ="UserOrder", schema = "KunderaExamples@cassandra_pu")
public class UserOrder implements Serializable  {

private static final long serialVersionUID = 8020522006409395867L;

@Id
@GeneratedValue(strategy = GenerationType.TABLE)
@PrimaryKey
private Integer id;

@Column(nullable = false)
private Integer userId;

@Column(nullable = false, unique = true)
private String orderId;

@Column(nullable = false)
@Enumerated(EnumType.STRING)
private OrderType orderType;

@Column(nullable = false)
private Integer quantity;

@Column(nullable = false)
private String instrument;

@Column(nullable = false)
private Integer price;

@Column(nullable = false)
@Enumerated(EnumType.STRING)
private OrderStatus orderStatus = OrderStatus.OPEN;

@Column(nullable = false)
private Boolean isLimitOrder = false;

public String getOrderId() {
return orderId;
}

public Integer getUserId() {
return userId;
}

public void setUserId(Integer userId) {
this.userId = userId;
}

public void setOrderId(String orderId) {
this.orderId = orderId;
}

public OrderType getOrderType() {
return orderType;
}

public void setOrderType(OrderType orderType) {
this.orderType = orderType;
}

public String getInstrument() {
return instrument;
}

public void setInstrument(String instrument) {
this.instrument = instrument;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public Integer getQuantity() {
return quantity;
}

public void setQuantity(Integer integer) {
this.quantity = integer;
}

public Integer getPrice() {
return price;
}

public void setPrice(Integer price) {
this.price = price;
}

public OrderStatus getOrderStatus() {
return orderStatus;
}

public void setOrderStatus(OrderStatus orderStatus) {
this.orderStatus = orderStatus;
}

public static long getSerialversionuid() {
return serialVersionUID;
}

public Boolean getIsLimitOrder() {
return isLimitOrder;
}

public void setIsLimitOrder(Boolean isLimitOrder) {
this.isLimitOrder = isLimitOrder;
}

@Override
public String toString() {
return "{ userId : " + userId + ", orderId : " + orderId + ", orderType : " + orderType + ", quantity : "
+ quantity + ", instrument : " + instrument + ", price : " + price + ", orderStatus : " + orderStatus
+ ",  isLimitOrder : " + isLimitOrder + "}";
}

}


5.Create a key space for your application.

CREATE KEYSPACE KeySpaceName
WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' :
ReplicationFactor};


官方参考资料地址
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: