当前位置: 首页 > 工具软件 > Kundera > 使用案例 >

Configuring Cassandra with Spring Boot using Kundera

吕永嘉
2023-12-01

转自

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:

  1. To make working with NoSQL as straightforward as working with SQL
  2. To fill in as JPA Compliant mapping answer for NoSQL Datastores.
  3. To help engineers, to overlook the many-sided quality of NoSQL stores and concentrate on Domain Model.
  4. 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};

官方参考资料地址

 类似资料:

相关阅读

相关文章

相关问答