Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Mysql迁移到Cassandra Spring boot_Mysql_Spring Boot_Cassandra - Fatal编程技术网

从Mysql迁移到Cassandra Spring boot

从Mysql迁移到Cassandra Spring boot,mysql,spring-boot,cassandra,Mysql,Spring Boot,Cassandra,我对Cassandra完全陌生,这是第一次,以前从未使用过,到目前为止,我们一直使用Spring Boot和MySql作为数据库,但现在我们计划将数据库迁移到Cassandra,只需最少的代码,或者不做任何更改。 下面是我们一直在使用的代码的示例演示 配置类 package com.example.demo.config; import com.zaxxer.hikari.HikariDataSource; import lombok.Generated; import org.springfr

我对Cassandra完全陌生,这是第一次,以前从未使用过,到目前为止,我们一直使用Spring Boot和MySql作为数据库,但现在我们计划将数据库迁移到Cassandra,只需最少的代码,或者不做任何更改。 下面是我们一直在使用的代码的示例演示

配置类

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;
    }
}
格雷德尔先生

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()
}
我在google的帮助下安装了Cassandra,可以使用
cqlsh
登录

创建键空间

CREATE KEYSPACE IF NOT EXISTS mykeyspace WITH replication = {'class':'SimpleStrategy', 'replication_factor':1};
在Gradle中添加了Cassandra依赖项

compile 'org.springframework.boot:spring-boot-starter-data-cassandra'
但是application.properties中的配置属性应该是什么呢?我在google上找不到创建自定义数据源的任何示例,所有示例都使用spring boot auto config,但我不能这样做,因为这将是一个重大的代码更改。
请提供帮助

您需要使用
CassandraRepository
而不是
JpaRepository
界面

public interface ChildRepository extends CassandraRepository<Child, Long> { }
public interface ChildRepository扩展了CassandraRepository{}
您需要在应用程序中具有这些属性。属性

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
spring.data.cassandra.contact points=127.0.0.1(或相应的连接uri)
spring.data.cassandra.username=
spring.data.cassandra.password=
spring.data.cassandra.keyspace=默认值
spring.data.cassandra.port=9042
spring.data.cassandra.schema action=NONE

那你就相对好了。但是就像其他人在评论部分所说的,理解卡桑德拉的本质是非常重要的。如果不根据查询实现数据模型,则不会成功。

除非关系数据库中的数据模型是专为nosql数据库设计的,否则您将无法在没有实际代码更改的情况下进行移植。我认为首先需要考虑数据模型,因为如果数据模型是以正常的关系方式设计的,您已经可以确定这不是一个“最小”的更改。再加上Andrew的观点——Cassandra中的所有内容都是从查询开始的——所有表都是按照您获取数据的方式组织的……Spring data Cassandra适用于小项目。但是,如果您希望它在某个时候能够扩展,Spring Data Cassandra会在幕后使用一些反模式。我始终建议大家使用DataStax Java驱动程序:
compile 'org.springframework.boot:spring-boot-starter-data-cassandra'
public interface ChildRepository extends CassandraRepository<Child, Long> { }
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