Connecting cassandra cluster through scala

前端 未结 4 1635
深忆病人
深忆病人 2021-02-11 10:40

I am new in both scala and cassandra . I know the connectivity of cassandra with java using hector . But I don\'t know how to connect cassandra through scala. I want a simple ex

4条回答
  •  野的像风
    2021-02-11 11:31

    I am using the datastax java driver. It's development is still active on github. I looked at Hector earlier but it seemed dying. The doc here's helpful: http://www.datastax.com/documentation/developer/java-driver/1.0/webhelp/index.html#java-driver/quick_start/qsQuickstart_c.html. I translated the first example to scala. Also, look at the akka-cassandra example from typesafe activator. Cheers.

    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();
    }
    

提交回复
热议问题