Using Apache Cassandra In Coldfusion

亡梦爱人 提交于 2019-12-10 17:13:08

问题


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:

  1. 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)

  2. Make sure to also download the Cassandra JDBC dependencies

  3. Copy the jar files to your Coldfusion lib directory

  4. Restart Coldfusion/Railo

  5. Create a datasource type "Other"

  6. For the Driver Class, enter "org.apache.cassandra.cql.jdbc.CassandraDriver"

  7. The connection string should be "jdbc:cassandra://host1--host2--host3:9160/keyspace1?primarydc=DC1&backupdc=DC2&consistency=QUORUM"

  8. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!