`
sillycat
  • 浏览: 2527626 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Apache Cassandra 2019 and WebFlux and WebMVC

 
阅读更多
Apache Cassandra 2019 and WebFlux and WebMVC

Create the Table in Cassandra
> CREATE KEYSPACE jobsmonitor WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 2 };

> use jobsmonitor;

> CREATE TABLE jobcounthistory(
               ... source_id text,
               ... record_date TIMESTAMP,
               ... new_active_count int,
               ... new_admin_count int,
               ... old_active_count int,
               ... old_admin_count int,
               ... PRIMARY KEY ( source_id, record_date )
               ... ) WITH CLUSTERING ORDER BY (record_date DESC);

The query will be
> select * from jobcounthistory where source_id = 'id1';
> select * from jobcounthistory where source_id = 'id1' and record_date > '2019-06-01';

Create the first example of WebMVC
Add pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-cassandra</artifactId>
</dependency>

The POJO Domain Class
package com.sillycat.mvclatency.domain;

import java.util.Date;

import org.springframework.data.cassandra.core.cql.Ordering;
import org.springframework.data.cassandra.core.cql.PrimaryKeyType;
import org.springframework.data.cassandra.core.mapping.Column;
import org.springframework.data.cassandra.core.mapping.PrimaryKeyColumn;
import org.springframework.data.cassandra.core.mapping.Table;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Setter
@Getter
@ToString
@Table("jobcounthistory")
public class JobCountHistory {

    @PrimaryKeyColumn(name = "source_id", ordinal = 0, type = PrimaryKeyType.PARTITIONED)
    private String sourceID;

    @PrimaryKeyColumn(name = "record_date", ordinal = 1, type = PrimaryKeyType.CLUSTERED, ordering = Ordering.DESCENDING)
    private Date recordDate;

    @Column("new_active_count")
    private Integer newActiveCount;

    @Column("new_admin_count")
    private Integer newAdminCount;

    @Column("old_active_count")
    private Integer oldActiveCount;

    @Column("old_admin_count")
    private Integer oldAdminCount;

}

The Repository Interface Class
package com.sillycat.mvclatency.repository;

import java.util.Date;
import java.util.List;

import org.springframework.data.cassandra.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import com.sillycat.mvclatency.domain.JobCountHistory;

@Repository
public interface JobCountHistoryRepositoryCassandra extends CrudRepository<JobCountHistory, String> {

    @Query("select * from jobcounthistory where source_id=?0")
    public List<JobCountHistory> findBySourceID(String sourceID);

    @Query("select * from jobcounthistory where source_id=?0 and record_date>?1")
    public List<JobCountHistory> findBySourceIDNearTime(String sourceID, Date recordDate);

    @Query("delete from jobcounthistory where source_id=?0")
    public void deleteBySourceID(String sourceID);

}

Configuration to add the Cassandra connection application.xml
spring:
data:
  cassandra:
   keyspace-name: jobsmonitor
   contact-points: ubuntu-master, ubuntu-dev2

Unit Class
package com.sillycat.mvclatency.repository;

import java.util.Date;
import java.util.List;
import java.util.Random;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.sillycat.mvclatency.domain.JobCountHistory;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class JobCountHistoryCassandraTest {

    @Autowired
    JobCountHistoryRepositoryCassandra jobCountHistoryRepositoryCassandra;

    @Test
    public void testBeanNotNull() {
        log.info("init the bean jobCountHistoryRepositoryCassandra");
        Assert.assertNotNull(jobCountHistoryRepositoryCassandra);
    }

    @Test
    public void save() throws Exception {
        JobCountHistory item1 = new JobCountHistory();
        item1.setNewActiveCount(new Random().nextInt(10));
        item1.setNewAdminCount(new Random().nextInt(100));
        item1.setOldActiveCount(new Random().nextInt(10));
        item1.setOldAdminCount(new Random().nextInt(100));
        item1.setSourceID("9527");
        item1.setRecordDate(new Date());
        jobCountHistoryRepositoryCassandra.save(item1);

        List<JobCountHistory> result = jobCountHistoryRepositoryCassandra.findBySourceID("9527");
        Assert.assertNotNull(result);
    }

}

Add the Controller to test the performance
package com.sillycat.mvclatency.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import com.sillycat.mvclatency.domain.JobCountHistory;
import com.sillycat.mvclatency.repository.JobCountHistoryRepositoryCassandra;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(value = "/jobcounthistory/")
@RestController
public class JobCountHistoryController {

    @ApiOperation(value = "fetch job by sourceID", response = String.class)
    @GetMapping("/jobcounthistory/{sourceID}")
    public List<JobCountHistory> findBySourceID(@PathVariable String sourceID) {
        List<JobCountHistory> results = jobCountHistoryRepositoryCassandra.findBySourceID(sourceID);
        return results;
    }

    @Autowired
    JobCountHistoryRepositoryCassandra jobCountHistoryRepositoryCassandra;

}


Install WRK if needed on Ubuntu
> sudo apt-get install build-essential libssl-dev git -y
> git clone https://github.com/wg/wrk.git wrk
> cd wrk/
> make
> sudo cp wrk /usr/local/bin

Check the Performance Differences
> wrk -t4 -c1000 -d60s --timeout 10s http://ubuntu-master:8081/jobcounthistory/9527 --latency

The strange thing is that I get better performance in WebMVC, close but worse performance in WebFlux.

Here are some changes in WebFlux
Package dependencies pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-cassandra</artifactId>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>

The JobCountHistory POJO are the same as previous project.

The repository interface class is different
package com.sillycat.webfluxlatency.repository;

import java.util.Date;

import org.springframework.data.cassandra.repository.Query;
import org.springframework.data.cassandra.repository.ReactiveCassandraRepository;
import org.springframework.stereotype.Repository;

import com.sillycat.webfluxlatency.domain.JobCountHistory;

import reactor.core.publisher.Flux;

@Repository
public interface JobCountHistoryRepositoryCassandra extends ReactiveCassandraRepository<JobCountHistory, String>{

    @Query("select * from jobcounthistory where source_id=?0")
    public Flux<JobCountHistory> findBySourceID(String sourceID);
   
    @Query("select * from jobcounthistory where source_id=?0 and record_date>=?1")
    public Flux<JobCountHistory> findBySourceIDNearTime(String sourceID, Date recordDate);
   
}

The WebFlux Controller is as follow:
package com.sillycat.webfluxlatency.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import com.sillycat.webfluxlatency.domain.JobCountHistory;
import com.sillycat.webfluxlatency.repository.JobCountHistoryRepositoryCassandra;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import reactor.core.publisher.Flux;

@Api(value = "/jobcounthistory/")
@RestController
public class JobCountHistoryController {

    @ApiOperation(value = "fetch job by sourceID", response = JobCountHistory.class)
    @GetMapping("/jobcounthistory/{sourceID}")
    public Flux<JobCountHistory> findBySourceID(@PathVariable String sourceID) {
        Flux<JobCountHistory> results = jobCountHistoryRepositoryCassandra.findBySourceID(sourceID);
        return results;
    }

    @Autowired
    JobCountHistoryRepositoryCassandra jobCountHistoryRepositoryCassandra;

}

The configuration are similar, but the unit test is different.
package com.sillycat.webfluxlatency.repository;

import java.util.Date;
import java.util.Random;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.sillycat.webfluxlatency.domain.JobCountHistory;

import lombok.extern.slf4j.Slf4j;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class JobCountHistoryCassandraTest {

    @Autowired
    JobCountHistoryRepositoryCassandra jobCountHistoryRepositoryCassandra;

    @Test
    public void testBeanNotNull() {
        log.info("init the bean jobCountHistoryRepositoryCassandra");
        Assert.assertNotNull(jobCountHistoryRepositoryCassandra);
    }

    @Test
    public void save() throws Exception {
        JobCountHistory item1 = new JobCountHistory();
        Date now = new Date();
        item1.setNewActiveCount(new Random().nextInt(10));
        item1.setNewAdminCount(new Random().nextInt(100));
        item1.setOldActiveCount(new Random().nextInt(10));
        item1.setOldAdminCount(new Random().nextInt(100));
        item1.setSourceID("9527");
        item1.setRecordDate(now);
        item1.setRecordDate(new Date());
        Mono<JobCountHistory> result1 = jobCountHistoryRepositoryCassandra.save(item1);
        StepVerifier.create(result1).expectNext(item1).verifyComplete();

        Flux<JobCountHistory> result2 = jobCountHistoryRepositoryCassandra.findBySourceIDNearTime("9527", now);
        StepVerifier.create(result2).expectNextCount(1l).verifyComplete();

    }

}

I used wrk to compare the performance, it seems that in this situation WebMVC is faster than WebFlux. I need more testing on that to figure out.

References:
https://www.baeldung.com/spring-data-cassandra-reactive
https://github.com/eugenp/tutorials/tree/master/persistence-modules/spring-data-cassandra
https://github.com/eugenp/tutorials/tree/master/persistence-modules/spring-data-cassandra-reactive

WebMVC with Cassandra
https://www.baeldung.com/spring-data-cassandra-tutorial
https://www.baeldung.com/spring-data-cassandratemplate-cqltemplate


分享到:
评论

相关推荐

    NoSQL Web Development with Apache Cassandra(2015)

    In NOSQL WEB DEVELOPMENT WITH APACHE CASSANDRA, you will learn about all aspects of using Cassandra in web applications–including accessing the Cassandra database using the common programming/...

    Beginning Apache Cassandra Development

    Beginning Apache Cassandra Development introduces you to one of the most robust and best-performing NoSQL database platforms on the planet. Apache Cassandra is a document database following the JSON ...

    Spring Data for Apache Cassandra API(Spring Data for Apache Cassandra 开发文档).CHM

    Spring Data for Apache Cassandra API。 Spring Data for Apache Cassandra 开发文档

    Mastering.Apache.Cassandra.2nd.Edition.1784392618

    Title: Mastering Apache Cassandra, 2nd Edition Author: Nishant Neeraj Length: 322 pages Edition: 2 Language: English Publisher: Packt Publishing Publication Date: 2015-02-27 ISBN-10: 1784392618 ISBN-...

    Mastering Apache Cassandra

    ### Apache Cassandra 掌控指南 #### 一、引言 在大数据时代,高效的数据存储与管理变得至关重要。《Mastering Apache Cassandra》这本书旨在帮助读者掌握 Apache Cassandra 的核心技术和最佳实践,使其能够在处理...

    Learning Apache Cassandra - Second Edition

    Learning Apache Cassandra - Second Edition by Sandeep Yarabarla English | 25 Apr. 2017 | ASIN: B01N52R0B5 | 360 Pages | AZW3 | 10.68 MB Key Features Install Cassandra and set up multi-node clusters ...

    Learning Apache Cassandra 2015

    ### 学习Apache Cassandra 2015:深入理解NoSQL数据库系统 #### 一、了解Apache Cassandra及其特性 **Apache Cassandra**是一款分布式NoSQL数据库系统,由Facebook开发并在2008年作为开源项目发布。它以Amazon的...

    Learning_Apache_Cassandra

    在本文档中,标题“Learning_Apache_Cassandra”透露了内容的主题,即学习Apache Cassandra。Cassandra是一个开源的NoSQL分布式数据库管理系统,它以高可用性和分布式架构著称。该书详细介绍了Cassandra的基本概念、...

    Expert Apache Cassandra Administration.pdf

    Apache Cassandra是一个分布式的NoSQL数据库管理系统,它被设计用来处理大量的数据跨越多个数据中心。Cassandra对高性能、高可用性、可扩展性有着出色的支持,因此它特别适合于那些需要不断增长和变化的数据集的应用...

    Mastering Apache Cassandra(PACKT,2013)

    Apache Cassandra is the perfect choice for building fault tolerant and scalable databases. Implementing Cassandra will enable you to take advantage of its features which include replication of data ...

    Cassandra(apache-cassandra-3.11.11-bin.tar.gz)

    Cassandra(apache-cassandra-3.11.11-bin.tar.gz)是一套开源分布式NoSQL数据库系统。它最初由Facebook开发,用于储存收件箱等简单格式数据,集GoogleBigTable的数据模型与Amazon Dynamo的完全分布式的架构于一身...

    apache-cassandra-3.11.13

    Apache Cassandra 是一个分布式数据库系统,特别设计用于处理大规模数据,具备高可用性、线性可扩展性和优秀的性能。在这个"apache-cassandra-3.11.13"版本中,我们探讨的是Cassandra项目的其中一个稳定版本,它包含...

    Apache Cassandra

    Cassandra的主要特点就是它不是一个数据库,而是由一堆数据库节点共同构成的一个分布式网络服务,对Cassandra 的一个写操作,会被复制到其他节点上去,对Cassandra的读操作,也会被路由到某个节点上面去读取。...

    CIS-Apache-Cassandra-3.11-Benchmark-v1.0.0.pdf

    CIS-Apache-Cassandra-3.11-Benchmark-v1.0.0.pdf IT 安全评估指南 CIS(Center for Internet Security)针对 Apache Cassandra 3.11 版本发布的安全评估 Benchmark 文档,旨在帮助用户和管理员对 Apache Cassandra...

    apress.beginning.apache.cassandra.development.1484201434

    Beginning Apache Cassandra Development takes you through the Apache Cassandra NoSQL database and how to build applications that leverage its capabilities. As Java, PHP, Python, and JavaScript are the ...

Global site tag (gtag.js) - Google Analytics