赞
踩
- # 获取镜像
- docker pull cassandra
- # 运行
- docker run -d --name cassandra cassandra:latest -p 9402:9402
- # 进入容器
- docker exec -it cassandra bash
-
- # 更新apt-get 并安装vim
- apt-get update
-
- apt-get install vim -y
-
- # 进入数据库
- cqlsh
- # 修改集群名称
- UPDATE system.local SET cluster_name = 'cass_cluster' where key='local';
-
- # 修改完成,退出
- exit
- # 修改配置文件
- vim /etc/cassandra/cassandra.yaml
-
- # 集群名称
- cluster_name:cass_cluster
- # 修改集群ip
- seeds: "127.0.0.1,127.0.0.2,127.0.0.3"
-
- 保存退出重启容器
- docker restart cassandra
创建集群失败 , docker集群跑不起来 , 最后使用yum 方式安装集群启动成功
Eugene Valchkou 方式处理业务
- <dependency>
- <groupId>com.valchkou.datastax</groupId>
- <artifactId>cassandra-driver-mapping</artifactId>
- </dependency>
- <dependency>
- <groupId>com.codahale.metrics</groupId>
- <artifactId>metrics-servlets</artifactId>
- </dependency>
- package com.wdz.cassandra.config;
-
- import com.datastax.driver.core.Cluster;
- import com.datastax.driver.core.PlainTextAuthProvider;
- import com.datastax.driver.core.Session;
- import com.datastax.driver.mapping.MappingSession;
- import org.springframework.boot.context.properties.ConfigurationProperties;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- @Configuration
- @ConfigurationProperties(prefix = "spring.data.cassandra")
- public class CassandraConfig {
-
- private String clusterName;
- private String keyspaceName;
- private String username;
- private String password;
- private int port;
- private String contactPoints;
- private String consistencyLevel;
-
-
- @Bean
- public Cluster cluster() {
- Cluster build = Cluster.builder()
- .withCredentials(username,password)
- .withPort(port)
- .withClusterName(clusterName)
- .addContactPoints(contactPoints)
- .withAuthProvider(authProvider())
- .build();
- return build;
-
- }
- @Bean
- public PlainTextAuthProvider authProvider() {
- return new PlainTextAuthProvider(username,password);
- }
-
- @Bean
- public Session session(Cluster cluster) {
- // initialize datastax session.
- Session session = cluster.connect();
- return session;
- }
-
- @Bean
- public MappingSession mappingSession() {
- MappingSession session = new MappingSession(keyspaceName, session(cluster()));
- return session;
- }
-
- public String getClusterName() {
- return clusterName;
- }
-
- public void setClusterName(String clusterName) {
- this.clusterName = clusterName;
- }
-
- public String getKeyspaceName() {
- return keyspaceName;
- }
-
- public void setKeyspaceName(String keyspaceName) {
- this.keyspaceName = keyspaceName;
- }
-
- public String getUsername() {
- return username;
- }
-
- public void setUsername(String username) {
- this.username = username;
- }
-
- public String getPassword() {
- return password;
- }
-
- public void setPassword(String password) {
- this.password = password;
- }
-
- public int getPort() {
- return port;
- }
-
- public void setPort(int port) {
- this.port = port;
- }
-
- public String getContactPoints() {
- return contactPoints;
- }
-
- public void setContactPoints(String contactPoints) {
- this.contactPoints = contactPoints;
- }
-
- public String getConsistencyLevel() {
- return consistencyLevel;
- }
-
- public void setConsistencyLevel(String consistencyLevel) {
- this.consistencyLevel = consistencyLevel;
- }
- }
domain
- package com.wdz.cassandra.domian;
-
-
- import javax.persistence.Column;
- import javax.persistence.Id;
- import javax.persistence.Table;
-
- /**
- * demo
- */
- @Table(name = "device_locus")
- public class DeviceLocus {
- @Id
- private String device_key;
-
- @Column
- private Long timez;
- @Column
- private String latitude;
- @Column
- private String longitude;
- /**
- * 设备经纬度
- */
- @Column
- private String device_gps;
- /**
- * 设备时间戳
- */
- @Column
- private Long device_timez;
-
- @Column
- private Integer course;
- @Column
- private Float speed;
-
- public DeviceLocus() {
- }
-
- public DeviceLocus(String device_key, Long timez, String latitude, String longitude, Integer course, Float speed, String device_gps, Long device_timez) {
- this.device_key = device_key;
- this.timez = timez;
- this.latitude = latitude;
- this.longitude = longitude;
- this.course = course;
- this.speed = speed;
- this.device_gps = device_gps;
- this.device_timez = device_timez;
- }
-
- public DeviceLocus(String device_key, Long timez, String latitude, String longitude) {
- this.device_key = device_key;
- this.timez = timez;
- this.latitude = latitude;
- this.longitude = longitude;
- }
-
- public String getDevice_key() {
- return device_key;
- }
-
- public void setDevice_key(String device_key) {
- this.device_key = device_key;
- }
-
- public long getTimez() {
- return timez;
- }
-
- public void setTimez(long timez) {
- this.timez = timez;
- }
-
- public String getLatitude() {
- return latitude;
- }
-
- public void setLatitude(String latitude) {
- this.latitude = latitude;
- }
-
- public String getLongitude() {
- return longitude;
- }
-
- public void setLongitude(String longitude) {
- this.longitude = longitude;
- }
-
- public String getDevice_gps() {
- return device_gps;
- }
-
- public void setDevice_gps(String device_gps) {
- this.device_gps = device_gps;
- }
-
- public Long getDevice_timez() {
- return device_timez;
- }
-
- public void setDevice_timez(Long device_timez) {
- this.device_timez = device_timez;
- }
-
- public Integer getCourse() {
- return course;
- }
-
- public void setCourse(Integer course) {
- this.course = course;
- }
-
- public Float getSpeed() {
- return speed;
- }
-
- public void setSpeed(Float speed) {
- this.speed = speed;
- }
-
- @Override
- public String toString() {
- return "DeviceLocus{" +
- "device_key='" + device_key + '\'' +
- ", timez=" + timez +
- ", latitude='" + latitude + '\'' +
- ", longitude='" + longitude + '\'' +
- ", device_gps='" + device_gps + '\'' +
- ", device_timez=" + device_timez +
- ", course=" + course +
- ", speed=" + speed +
- '}';
- }
- }
- package com.wdz.cassandra;
-
- import com.datastax.driver.mapping.MappingSession;
- import com.wdz.cassandra.domian.DeviceLocus;
- import org.junit.jupiter.api.Test;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
-
-
- @SpringBootTest
- class CassandraBootApplicationTests {
-
- @Autowired
- private MappingSession mappingSession;
-
- @Test
- void contextLoads() {
- DeviceLocus deviceLocus = new DeviceLocus("8655", System.currentTimeMillis(), "34.22222", "123.123123");
- // demoService.insert(deviceLocus);
- mappingSession.save(deviceLocus);
- System.out.println("================");
- }
-
- }
- server:
- port: 8888
- spring:
- data:
- cassandra:
- clusterName: Test Cluster
- keyspaceName: test
- username: test
- password: test
- port: 9042
- contactPoints: 12.3.45.6
- consistencyLevel: ONE
-
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。