參考接上文cassandra入門 http://www.cnblogs.com/piaolingzxh/p/4197833.htmlhtml
源碼下載地址:https://bitbucket.org/openscg/cassandra2-jdbc/src,最新包可能會構建失敗,請下載2.1.1版的java
固然,你也能夠使用我構建好的,地址:http://download.csdn.net/detail/piaolingzxh/8320131sql
注:使用maven構建,數據庫
至於爲啥要引入這些包,能夠參看下載的jdbc源碼示例,examples\runTest.shmaven
jar包位置說明:下圖這兩個包直接使用jdbc驅動源碼下lib\目錄裏的版本,其餘的使用cassandra安裝目錄\lib下的版本.net
代碼以下:scala
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; public class cassandra2 { public static void main(String[] a) { try { Class.forName("org.bigsql.cassandra2.jdbc.CassandraDriver"); Connection con = DriverManager .getConnection("jdbc:cassandra://127.0.0.1:9160/demo"); String query = "select * from demo.users"; Statement statement = con.createStatement(); ResultSet rs = statement.executeQuery(query); while (rs.next()) { System.out.print(rs.getString(1) + ":" + rs.getString(2) + "\t" + rs.getString(3) + "\t" + rs.getString(4) + "\t" + rs.getString(5) + "\t" + rs.getString(6) + "\t" + "\n"); } rs.close(); statement.close(); } catch (Exception e) { e.printStackTrace(); } } }
建立scala項目,引用jar包,(和使用java同樣)3d
建立scala類,輸入如下代碼,htm
import java.sql.DriverManager
import org.bigsql.cassandra2.jdbc.CassandraDriverblog
object cassandra {
import java.sql.{ Connection, DriverManager, ResultSet }
def main(args: Array[String]): Unit = {
classOf[org.bigsql.cassandra2.jdbc.CassandraDriver]
val db = DriverManager.getConnection("jdbc:cassandra://127.0.0.1:9160/demo")
val st = db.createStatement
val res = st.executeQuery("select * from demo.users")
while (res.next) {
for (i <- 1 to res.getMetaData.getColumnCount) {
val r = res.getString(i).toString
print("\t" + r)
}
println
}
db.close
}
}
運行結果