慢插入和保存spring数据cassandra存储库的所有性能

b1zrtrql  于 2021-06-15  发布在  Cassandra
关注(0)|答案(1)|浏览(493)

我正在尝试使用spring向cassandra插入1500条记录。我有一个pojo列表,其中保存着这1500条记录,当我调用saveall或insert处理这些数据时,完成这个操作需要30秒。有人能给我建议一个更快完成这件事的方法吗?我目前正在运行cassandra3.11.2作为一个单节点测试集群。
实体pojo:

package com.samplepoc.pojo;

import static org.springframework.data.cassandra.core.cql.PrimaryKeyType.PARTITIONED;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

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

@Table("health")
public class POJOHealth
{
    @PrimaryKeyColumn(type=PARTITIONED)
    UUID primkey;
    @Column
    String col1;
    @Column
    String col2;
    @Column
    String col3;
    @Column
    String col4;
    @Column
    String col5;
    @Column
    Date ts;
    @Column
    boolean stale;
    @Column
    String col6;
    @Column
    String col7;
    @Column
    String col8;
    @Column
    String col9;
    @Column
    Map<String,String> data_map = new HashMap<String,String>();

    public POJOHealth(
             String col1,
             String col2,
             String col3,
             String col4,
             String col5,
             String col6,
             String col7,
             String col8,
             String col9,
             boolean stale,
             Date ts,
             Map<String,String> data_map
             )
    {
        this.primkey = UUID.randomUUID();
        this.col1=col1;
        this.col2=col2;
        this.col3=col3;
        this.col4=col4;
        this.col5=col5;
        this.col6=col6;
        this.col7=col7;
        this.col8=col8;
        this.col9=col9;
        this.ts=ts;
        this.data_map = data_map;
        this.stale=stale;
    }

    //getters & setter ommitted
}

持久化服务代码段:

public void persist(List<POJO> l_POJO)
{
        System.out.println("Enter Persist: "+new java.util.Date());

        List<l_POJO> l_POJO_stale = repository_name.findBycol1AndStale("sample",false);
        System.out.println("Retrieve Old: "+new java.util.Date());

        l_POJO_stale.forEach(s -> s.setStale(true));
        System.out.println("Set Stale: "+new java.util.Date());

        repository_name.saveAll(l_POJO_stale);
        System.out.println("Save stale: "+new java.util.Date());

        try 
        {
            repository_name.insert(l_POJO);
        } 
        catch (Exception e) 
        {
            System.out.println("Error in persisting new data");
        }
        System.out.println("Insert complete: "+new java.util.Date());
}
t9aqgxwy

t9aqgxwy1#

我不知道spring,但是它使用的java驱动程序可以异步插入。以这种方式保存时,示例的延迟决定了吞吐量,而不是查询的效率。假设你对c协调器有10毫秒的延迟,一次保存一个需要30秒(10毫秒那里10毫秒回来1500)。
如果你用executeasync同时插入所有的代码,并将它们全部阻塞,那么你应该能够在不到一秒钟的时间内完成1500个代码,除非你的硬件电源非常不足(几乎任何比raspberry pi更强大的东西都应该至少能够处理突发的代码)。也就是说,如果你的应用程序有任何并发性,你不希望每个应用程序同时发送1000个插入,所以在飞行中设置某种限制(例如128个限制的信号量)将是一个非常好的主意。

相关问题