我需要通过Gradle执行CQL3脚本,我们有没有 Gradle的cassandra插件可以做同样的事情,还是有其他方法可以 在构建过程中执行CQL3脚本。请建议。
达乌德
答案 0 :(得分:1)
您可以将像Astyanax这样的Cassandra客户端添加到buildscript类路径中,然后直接在Gradle脚本中使用它。 e.g。
buildscript {
repositories {
mavenCentral()
}
dependencies {
compile 'com.netflix.astyanax:astyanax-cassandra:1.56.42'
}
}
task(doCql) << {
AstyanaxContext<Keyspace> context = new AstyanaxContext.Builder()
.forCluster("ClusterName")
.forKeyspace("KeyspaceName")
.withAstyanaxConfiguration(new AstyanaxConfigurationImpl()
.setDiscoveryType(NodeDiscoveryType.RING_DESCRIBE)
.setCqlVersion("3.0.0")
.setTargetCassandraVersion("1.2")
)
.withConnectionPoolConfiguration(new ConnectionPoolConfigurationImpl("MyConnectionPool")
.setPort(9160)
.setMaxConnsPerHost(1)
.setSeeds("127.0.0.1:9160")
)
.withConnectionPoolMonitor(new CountingConnectionPoolMonitor())
.buildKeyspace(ThriftFamilyFactory.getInstance());
context.start();
Keyspace keyspace = context.getClient();
result = keyspace
.prepareQuery(CQL3_CF)
.withCql("SELECT * FROM employees WHERE empId='111';")
.execute();
for (Row<Integer, String> row : result.getResult().getRows()) {
LOG.info("CQL Key: " + row.getKey());
ColumnList<String> columns = row.getColumns();
LOG.info(" first_name : " + columns.getStringValue ("first_name", null));
LOG.info(" last_name : " + columns.getStringValue ("last_name", null));
}
}