赞
踩
docker pull cassandra
docker run -p 9042:9042 --name cassandra -d cassandra
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>
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 查询超时时间
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';
实体类
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; }
测试代码
@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); //批量插入 } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。