jenkins - 使用 Jenkins 创建 Cassandra 数据库

标签 jenkins cassandra ddl

有谁知道如何通过 Jenkins 运行 DDL 脚本来创建 Cassandra 数据库?我正在尝试在测试环境中通过 Jenkins 连接到 Cassandra,以便上传测试基线数据集并针对它运行集成测试。

最佳答案

我创建了自己的解决方案来解决类似的问题。不仅用于测试,还用于随着架构随着时间的推移发生变化而按顺序应用脚本。它将在 Jenkins 或任何地方工作。有一个类可以按顺序浏览脚本列表,将每个脚本作为输入流打开。然后该类调用该类的execute() 方法:

package org.makeyourcase.persistence;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;

import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.exceptions.SyntaxError;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class CqlFileRunner {

    private static final Logger LOG = Logger.getLogger(CqlFileRunner.class);

    @Value("${cassandra.node}")
    private String node;

    @Value("${cassandra.keyspace}")
    private String keyspace;

    @Autowired
    private CassandraClusterBuilderMaker cassandraClusterBuilderMaker;

    public void execute(InputStream commandStream) throws IOException {
        byte[] commandBuffer = new byte[commandStream.available()];
        IOUtils.readFully(commandStream, commandBuffer);

        Cluster cluster = cassandraClusterBuilderMaker.create().addContactPoint(node).build();
        Session session = cluster.connect(keyspace);

        List<String> commands = Arrays.asList(new String(commandBuffer, "UTF-8").split(";"));
        for(String command : commands){
            if(!command.trim().isEmpty()){
                command = command.trim() + ";";
                LOG.info("Execute:\n" + command);
                try {
                    session.execute(command);
                } catch (SyntaxError e) {
                    LOG.error("Command failed with " + e.getMessage());
                    throw e;
                }
            }
        }

    }
}

鉴于此,您可以运行“cql 脚本”来创建表并加载数据。它对于小容量的东西很有用,但对于大容量的东西来说可能太慢了。脚本可能如下所示:

CREATE TABLE access_tiers (
    level bigint PRIMARY KEY,
    role text
);
ALTER TABLE access_tiers WITH caching = 'all' AND compression = {'sstable_compression' : ''};

INSERT INTO access_tiers (level, role) VALUES (200, 'user_tier2');
INSERT INTO access_tiers (level, role) VALUES (1000, 'user_tier3');
INSERT INTO access_tiers (level, role) VALUES (5000, 'user_tier4');
INSERT INTO access_tiers (level, role) VALUES (10000, 'user_tier5');
INSERT INTO access_tiers (level, role) VALUES (20000, 'user_tier6');
INSERT INTO access_tiers (level, role) VALUES (50000, 'moderator');

编辑:

自从这篇原始文章发布以来,我已经提取了我在项目中使用的 Java 版本控制组件。我还创建了一个小示例项目来展示如何集成它。这是赤裸裸的。解决这个问题有不同的方法,因此我选择了一种易于构建且能满足我需要的方法。这是两个 github 项目:

https://github.com/DonBranson/cql_schema_versioning

https://github.com/DonBranson/cql_schema_versioning_example

关于jenkins - 使用 Jenkins 创建 Cassandra 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24867756/

相关文章:

管道脚本中的 java.io.NotSerializedException 错误

java - Cloudbees 构建错误

cassandra - Reddit中使用的Cassandra数据库架构是什么?

mysql - SQL 中两个表的交集

linux - Jenkins 执行 bash shell 命令

linux - 通过从 Jenkins slave 执行的 bash 脚本向 Windows 机器发送 Ssh 命令

java - 如何在我的工作区中导入 astyanax 客户端示例代码

java - Cassandra session 生命周期

sql - 错误: could not create unique index DETAIL: Key (id)=(3105115) is duplicated

java - Oracle ORA-29536 关于 DDL-将 java 源加载到数据库中