2013-10-16 79 views

回答

0

有一些斯卡拉項目出現支持卡桑德拉連接,其中包括通過推特cassie,但他們似乎都死了。

既然你知道Hector,如果你的類路徑中有Hector jar,那麼你可以簡單地在Scala中使用Hector API。

例如,從documentation借款,你可能只是這樣做:

val myCluster = HFactory.getOrCreateCluster("test-cluster","localhost:9160") 

val template = new ThriftColumnFamilyTemplate[String, String](ksp, columnFamily, StringSerializer.get(), StringSerializer.get()) 

我寫了這個寫意,所以語法可能會關閉,但你的想法。

+0

這是不好的建議。您應該使用@ kfer38建議的原生Java驅動程序。 –

+0

告訴某人將雪糕醬加入冰淇淋聖代是「不好的建議」。您錯過了更廣泛的觀點,那就是您可以使用Scala語法來結合任何Java API(與使用Scala構建的API相反),並且由於OP的經驗,我以Hector爲例。感謝@ kfer38提出我的觀點的另一個例子。 Datastax驅動程序如何與Hector作爲Cassandra客戶端進行比較超出了問題的範圍。 – Vidya

+0

也許「壞建議」太強大了。在你的辯護中,他確實提出了有關赫克託的問題。然而,從他正在使用Scala來第一次訪問Cassandra的事實中也可以看出,他正在編寫新的代碼,在這種情況下,使用有效的已棄用的庫是不明智的。本地Java庫應被視爲此時唯一有效的基於JVM的選項。 –

5

我正在使用datastax java驅動程序。它的開發在github上仍然很活躍。我早些時候看過Hector,但看起來很快。該文檔有幫助:http://www.datastax.com/documentation/developer/java-driver/1.0/webhelp/index.html#java-driver/quick_start/qsQuickstart_c.html。我把第一個例子翻譯成scala。另外,請看類型安全激活器中的akka​​-cassandra示例。乾杯。

object Hello extends App { 

    import com.datastax.driver.core.Cluster; 
    import com.datastax.driver.core.Host; 
    import com.datastax.driver.core.Metadata; 
    import scala.collection.JavaConversions._ 

    var cluster: Cluster = null 
    private var session: Session = null 

    def connect(node: String) { 
    cluster = Cluster.builder().addContactPoint(node).build() 
    val metadata = cluster.getMetadata() 
    printf("Connected to cluster: %s\n", 
     metadata.getClusterName()) 
    metadata.getAllHosts() map { 
     case host => 
     printf("Datatacenter: %s; Host: %s; Rack: %s\n", 
      host.getDatacenter(), host.getAddress(), host.getRack()) 
    } 
    } 

    def close() { 
    cluster.shutdown() 
    } 

    this.connect("127.0.0.1"); 
    this.close(); 
} 
+0

你能告訴我我必須放在sbt文件中的庫依賴關係嗎? –

+0

'libraryDependencies + =「com.datastax.cassandra」%「cassandra-driver-core」%「1.0.4」' –

+0

我得到的錯誤 - 「無法找到抽象類的類org.apache.cassandra.db .marshal.TimestampType']「 –

2

我一直在研究Java驅動程序的Scala包裝,以最小化樣板代碼。你可以在這裏找到它: https://github.com/InnovaCo/binders-cassandra

下面是一些卡桑德拉DB工程類的例子:

class Db(session: com.datastax.driver.core.Session) { 

    implicit val cache = new SessionQueryCache[PlainConverter](session) 

    // class for binding input/output parameters 
    case class User(userId: Int, name: String) 

    def insertUser(user: User): Future[Unit] = cql"insert into users(userid, name) values (?, ?)".bind(user).execute 

    // returns Future[Iterator[User]] 
    def selectAllUsers: Future[Iterator[User]] = cql"select * from users".all[User] 

    // if no user is found will throw NoRowsSelectedException 
    def selectUser(userId: Int) = cql"select * from users where userId = $userId".one[User] 

    // if no user is found will return None, otherwise Some(User) 
    def selectUserIfFound(userId: Int) = cql"select * from users where userId = $userId".oneOption[User] 
} 

要在playframework項目中使用的庫,此行添加到build.sbt文件:

libraryDependencies += "eu.inn" %% "binders-cassandra" % "0.2.0"