使用Jenkins创建Cassandra数据库 [英] create Cassandra database with Jenkins
问题描述
有谁知道如何运行一个DDL脚本通过Jenkins创建一个Cassandra数据库?我试图通过Jenkins在测试环境中连接到Cassandra,以便上传测试基准数据集并对其运行集成测试。
Does anyone know how to run a DDL script to create a Cassandra database via Jenkins? I'm trying to connect to Cassandra through Jenkins in a testing environment in order to upload a test baseline dataset and run integration tests against it.
推荐答案
我创建了自己的解决方案来解决类似的问题。不仅仅用于测试,而是用于随着时间的推移对架构进行更改时应用脚本。它将在Jenkins或任何地方工作。有一个类按顺序旋转脚本列表,打开每个作为输入流。该类然后调用此类的execute()方法:
I created my own solution to solve a similar issue. Not just for testing, but for applying scripts in order as changes occur to the schema over time. It will work under Jenkins or wherever. There's a class to spin through the list of scripts in order, opening each as an input stream. That class then invokes the execute() method on this class:
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 scripts来创建表和加载数据。这对于小容量的东西很有好处,但对于大的东西可能太慢了。脚本可能如下所示:
Given this, you can run "cql scripts" to create tables and load data. It's good for small-volume stuff, but probably too slow for anything big. A script might look like:
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项目:
Since this original post, I've extracted the Java versioning component that I'm using for my project. I also created a small sample project that shows how to integrate it. It's bare-bones. There are different approaches to this problem, so I picked one that was simple to build and does what I need. Here are the two github projects:
https:// github .com / DonBranson / cql_schema_versioning
https: //github.com/DonBranson/cql_schema_versioning_example
这篇关于使用Jenkins创建Cassandra数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!