0

我需要通过 Gradle 执行 CQL3 脚本,我们是否有任何 cassandra 插件供 Gradle 执行相同操作,或者是否有任何其他方式可以在构建过程中执行 CQL3 脚本。请建议。

达伍德

4

1 回答 1

1

您可以将 Astyanax 之类的 Cassandra 客户端添加到 buildscript 类路径中,然后直接在 Gradle 脚本中使用它。例如

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));
    }
}
于 2013-09-04T04:51:54.847 回答