Spring Boot中Java的Cassandra架构迁移

时间:2018-08-08 06:58:01

标签: spring-boot cassandra

无论何时运行项目,我都使用Cassandra Schema Migration library初始化数据库。通过本教程:

Database database = new Database(cluster, "nameOfMyKeyspace");
MigrationTask migration = new MigrationTask(database, new MigrationRepository());
migration.migrate();

我应将上述脚本放在哪里: 在SpringBootApplication或Cassandra Config中还是其他?

如何保留和检查数据库版本?该库有任何教程吗?

1 个答案:

答案 0 :(得分:2)

您可以添加一个CommandLineRunner类,该类将在应用程序启动时运行。像这样:

@Component
@Slf4j
public class AppStartupRunner implements CommandLineRunner {

@Autowired
Cluster cluster;

@Autowired
private Environment environment;

@Override
public void run(String...args) throws Exception {
    log.info("Starting DB Migration");
    Database database = new Database(cluster, environment.getProperty("cassandra.keyspace"));
    MigrationTask migration = new MigrationTask(database, new MigrationRepository("resources/cassandra/migration"));
    migration.migrate();
    log.info("DB Migration Complete");
}

}

希望这会有所帮助。