简体   繁体   English

从 Mysql 迁移到 Cassandra Spring 启动

[英]Migrating from Mysql to Cassandra Spring boot

I am totally new to Cassandra, this is the very first time, never used it before, so far we have been using Spring Boot and MySql as our DB, but now we are planning to migrate our database to Cassandra with the minimum code or no change.我对 Cassandra 完全陌生,这是第一次,以前从未使用过它,到目前为止我们一直使用 Spring Boot 和 MySql 作为我们的数据库,但现在我们计划使用最少的代码或没有将我们的数据库迁移到 Cassandra改变。 here is a sample demo of code that we have been using.这是我们一直在使用的代码示例演示。

config class配置 class

package com.example.demo.config;
import com.zaxxer.hikari.HikariDataSource;
import lombok.Generated;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
import java.util.HashMap;
import static org.hibernate.cfg.AvailableSettings.*;
@Configuration
@EnableTransactionManagement
@EnableJpaAuditing
@EnableJpaRepositories(entityManagerFactoryRef = "orclEntityManagerFactory", transactionManagerRef = "orclTransactionManager", basePackages = {"com.example.demo.repository"})
@Generated
public class DataSourceConfig {
    @Value("${spring.datasource.driver-class-name}")
    private String orclDbDriver;
    @Value("${spring.datasource.url}")
    private String orclDbConnUrl;
    @Value("${spring.datasource.username}")
    private String orclDbUsername;
    @Value("${spring.datasource.password}")
    private String orclDbPassword;
    @Value("${spring.datasource.poolName}")
    private String dataSourcePoolName;
    @Value("${spring.jpa.properties.hibernate.dialect}")
    private String orclHibernateDialect;
    @Value("${spring.jpa.hibernate.ddl-auto}")
    private String hibernateDDL;
    @Value("${spring.jpa.show-sql}")
    private boolean showSql;
    @Value("${spring.datasource.packagesToScan}")
    private String[] packagesToScan;
    public DataSourceConfig() {
    }
    @Bean
    @Primary
    public DataSource orclDataSource() {
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setDriverClassName(this.orclDbDriver);
        dataSource.setJdbcUrl(this.orclDbConnUrl);
        dataSource.setUsername(this.orclDbUsername);
        dataSource.setPassword(this.orclDbPassword);
        dataSource.setPoolName(this.dataSourcePoolName);
        return dataSource;
    }
    @Bean
    @Primary
    public LocalContainerEntityManagerFactoryBean orclEntityManagerFactory() {
        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        factory.setDataSource(this.orclDataSource());
        factory.setPackagesToScan(this.packagesToScan);
        HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
        jpaVendorAdapter.setDatabase(Database.MYSQL);
        jpaVendorAdapter.setGenerateDdl(Boolean.TRUE);
        jpaVendorAdapter.setShowSql(showSql);
        jpaVendorAdapter.setDatabasePlatform(orclHibernateDialect);
        factory.setJpaVendorAdapter(jpaVendorAdapter);
        HashMap<String, Object> properties = new HashMap();
        properties.put(HBM2DDL_AUTO, this.hibernateDDL);
        properties.put(DIALECT, this.orclHibernateDialect);
        properties.put(STATEMENT_BATCH_SIZE, "500");
        properties.put(ORDER_UPDATES, "true");
        properties.put(ORDER_INSERTS, "true");
        properties.put(GENERATE_STATISTICS, "true");
        factory.setJpaPropertyMap(properties);
        return factory;
    }
    @Bean
    @Primary
    public JpaTransactionManager orclTransactionManager() {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(this.orclEntityManagerFactory().getObject());
        return transactionManager;
    }
}

controller class controller class

import com.example.demo.entity.Child;
import com.example.demo.repository.ChildRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.util.List;

@RestController
public class HelloController {
    @Autowired
    private ChildRepository childRepository;

    @GetMapping("put")
    public Child getHello() {
        Child child = new Child();
        child.setName("shrikant");
        child.setUpdatedAt(new Timestamp(System.currentTimeMillis()));
        return childRepository.save(child);
    }

    @GetMapping("get")
    public List<Child> hello() {
        return childRepository.findAll();
    }
}

repository存储库

package com.example.demo.repository;
import com.example.demo.entity.Child;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface ChildRepository extends JpaRepository<Child, Long> {
}

 

application.dev应用程序.dev

spring.datasource.url=jdbc:mysql://localhost:3306/student_db?jdbcCompliantTruncation=false&sessionVariables=sql_mode='NO_ENGINE_SUBSTITUTION'&useSSL=false&useServerPrepStmts=false&rewriteBatchedStatements=true&useUnicode=true&characterEncoding=utf8
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.poolName=springHikariCp
spring.jpa.generate-ddl=true
spring.jpa.database.schema=student_db
spring.jpa.hibernate.ddl-auto=update
#spring.jpa.hibernate.ddl-auto=none
spring.jpa.show-sql=true
spring.datasource.packagesToScan=com.example.demo.entity
spring.profiles.active=dev

build.gradle build.gradle

plugins {
    id 'org.springframework.boot' version '2.2.6.RELEASE'
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
    id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '8'
configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}
repositories {
    mavenCentral()
}
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    compileOnly 'org.projectlombok:lombok'
    compile 'mysql:mysql-connector-java'  
    annotationProcessor 'org.projectlombok:lombok' 
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}
test {
    useJUnitPlatform()
}

I have installed Cassandra with the help of google and able to login using cqlsh .我已经在谷歌的帮助下安装了 Cassandra 并且能够使用cqlsh登录。

created keyspace创建的键空间

CREATE KEYSPACE IF NOT EXISTS mykeyspace WITH replication = {'class':'SimpleStrategy', 'replication_factor':1};

added Cassandra dependency in Gradle在 Gradle 添加 Cassandra 依赖

compile 'org.springframework.boot:spring-boot-starter-data-cassandra'

but what should be the configuration properties in the application.properties, I couldn't find any of the examples on Google creating a custom data source, all are using Spring Boot auto-config, but I can't do that, because that will be a major code change.但是 application.properties 中的配置属性应该是什么,我在 Google 上找不到任何创建自定义数据源的示例,所有示例都使用 Spring 启动自动配置,但我不能那样做,因为那样会是一个主要的代码更改。

You need to use the CassandraRepository instead of JpaRepository interface您需要使用CassandraRepository而不是JpaRepository接口

public interface ChildRepository extends CassandraRepository<Child, Long> { }

You need to have these properties in your application.properties您需要在 application.properties 中包含这些属性

spring.data.cassandra.contact-points=127.0.0.1 (or your corresponding connection uri)
spring.data.cassandra.username=<username, if any>
spring.data.cassandra.password=<password, if any>
spring.data.cassandra.keyspace=default
spring.data.cassandra.port=9042
spring.data.cassandra.schema-action=NONE

Then you will be relatively fine.那你就相对好了。 But like the other guys said in the comments section, it is quite important that you should understand the nature of Cassandra. If you don't implement your data model based on your queries, it won't be successful.但是就像其他人在评论部分所说的那样,您应该了解Cassandra的性质非常重要。如果您不根据您的查询实现您的数据model,它将不会成功。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM