当前位置:   article > 正文

docker 安装cassandra数据库_docker安装cassandra

docker安装cassandra
docker pull cassandra
  • 1
docker run -p 9042:9042 --name cassandra -d cassandra

  • 1
  • 2

springboot 连接代码
1.引入pom

 	<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-cassandra</artifactId>
        </dependency>
        <dependency>
            <groupId>com.datastax.cassandra</groupId>
            <artifactId>cassandra-driver-core</artifactId>
            <version>3.11.0</version>
        </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

yaml文件

spring:
  servlet:
    multipart:
      max-file-size: 50MB
      max-request-size: 50MB
  jackson:
    time-zone: GMT+8
    date-format: yyyy-MM-dd HH:mm:ss   #涉及接收日期参数格式
  main:
    allow-bean-definition-overriding: true  #重写jacksonObjectMapper
#  datasource:
#    driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
#    url: jdbc:sqlserver://10.10.1.65:1433;DatabaseName=EVODetection_ASSY_BZH
#    username: sa
#    password: 123456
  data:
    cassandra:
      contact-points: 192.168.206.128
      port: 9042
      local-datacenter: datacenter1
      session-name: JtTistCluster
      username: cassandra
      password: cassandra
      keyspace-name: test
      request:
        timeout: 15000 #ms 查询超时时间
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

建表语句

CREATE TABLE cuser (
    id bigint,
    code text,
    edittime date,
    name text,
    PRIMARY KEY (id)
) WITH caching={'keys':'ALL', 'rows_per_partition':'NONE'}
 AND compaction={'class':'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy', 'max_threshold':'32', 'min_threshold':'4'}
 AND compression={'chunk_length_in_kb':'16', 'class':'org.apache.cassandra.io.compress.LZ4Compressor'}
 AND speculative_retry='99p';

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

实体类

package com.fitow.ois.entity;


import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.test.autoconfigure.data.cassandra.AutoConfigureDataCassandra;
import org.springframework.data.cassandra.core.mapping.Column;
import org.springframework.data.cassandra.core.mapping.PrimaryKey;
import org.springframework.data.cassandra.core.mapping.Table;

import java.io.Serializable;
import java.util.Date;

@Table("cuser")
@Data
public class CUser implements Serializable {

    @Column("id")
    @PrimaryKey
    @ApiModelProperty(value = "主键ID")
   private Long id;
    @Column("name")
    private String name;
    @Column("code")
    private String code;
    @Column(value = "edit_time")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date editTime;

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

测试代码

 @Autowired
    private CassandraTemplate cassandraTemplate;
 @Test
    public  void test() throws Exception {
        Snowflake snowflake = IdUtil.getSnowflake(31,31);
        long l = System.currentTimeMillis();

        List<CUser> select = cassandraTemplate.select("select * from cuser", CUser.class); //2574 查询
        System.out.println(System.currentTimeMillis()-l);
   long l = System.currentTimeMillis();
        String csql= "insert into cuser(id,name,code)values(?,?,?)";
        for(int j=0;j<10;j++) {
            List<CUser> cUsers = new ArrayList<>();
            for (int i = 0; i < 10000; i++) {
                CUser mUser = new CUser();
                mUser.setId(snowflake.nextId());
                mUser.setName("xxx");
                mUser.setCode("xxxx");
                //mUser.setEditTime(new Date());
                             cUsers.add(mUser);
                //cassandraTemplate.insert(mUser);
            }
            cassandraTemplate.batchOps().insert(cUsers); //批量插入
        }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/126353
推荐阅读
相关标签
  

闽ICP备14008679号