0

I have a requirement to execute CQL3 scripts through Gradle, do we have any cassandra plugin for Gradle to do the same or is there any other way I can execute CQL3 scripts during the build itself. Please suggest.

Dawood

Dawood
  • 301
  • 1
  • 5
  • 12

1 Answers1

1

You can add a Cassandra client like Astyanax to the buildscript classpath and then use it directly in the Gradle script. 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));
    }
}
Justin Ryan
  • 2,413
  • 1
  • 15
  • 5