问题
I'm trying to use Apache Cassandra on a project I'm coding using Coldfusion. Since Coldfusion doesn't have a driver for Apache Cassandra and vice versa, I'm attempting to use Cassandra's Java drivers.
I'm pretty much a Java newbie so please bear with me.
I've managed to copy the necessary .jar files to /opt/railo/lib/ (I'm using Railo) and also managed to connect to Cassandra using Coldfusion using the code below. What I need help with is looping through the results returned by Cassandra when I run a query. I've included my very simple code below:
<cfset MyClusterInit = CreateObject("java", "com.datastax.driver.core.Cluster")>
<cfset MyCluster = MyClusterInit.builder().addContactPoint("127.0.0.1").build()>
<cfset MySession = MyCluster.connect("mytest")>
<cfset GetCustomer = MySession.execute("SELECT * FROM customer")>
<cfdump var="#GetCustomer#">
How do I loop through the results returned? The cfdump returns the Java method ArrayBackedResultSet$SinglePage. It's not something that I can loop over using Coldfusion.
From the "Getting Started With Cassandra and Java" post, I see the code as below:
ResultSet results = session.execute("SELECT * FROM users WHERE lastname='Jones'");
for (Row row : results) {
System.out.format("%s %d\n", row.getString("firstname"), row.getInt("age"));
}
How do I replicate the above in Coldfusion?
Many thanks for taking your time to help.
回答1:
As pointed out by Leigh and Mark A Kruger above, using the JDBC driver was the smarter way to connect to Cassandra in Coldfusion/Railo. Just follow the instructions below:
Download the JDBC driver at https://code.google.com/a/apache-extras.org/p/cassandra-jdbc/. It works with the latest version of Cassandra (at time of writing)
Make sure to also download the Cassandra JDBC dependencies
Copy the jar files to your Coldfusion lib directory
Restart Coldfusion/Railo
Create a datasource type "Other"
For the Driver Class, enter "org.apache.cassandra.cql.jdbc.CassandraDriver"
The connection string should be "jdbc:cassandra://host1--host2--host3:9160/keyspace1?primarydc=DC1&backupdc=DC2&consistency=QUORUM"
That's it! You can now query Cassandra like you query any other database
Not only that. I managed to get the Java driver working directly and the performance of the JDBC driver was much better than calling the Java driver directly. My test was just a very simple table with 3 records and it took 50ms to connect directly using Java while using the JDBC driver took less than 5ms.
Thanks to Leigh and Mark A Kruger for the tip!
来源:https://stackoverflow.com/questions/26584607/using-apache-cassandra-in-coldfusion