当前位置: 首页>>代码示例>>Scala>>正文


Scala Cluster类代码示例

本文整理汇总了Scala中com.datastax.driver.core.Cluster的典型用法代码示例。如果您正苦于以下问题:Scala Cluster类的具体用法?Scala Cluster怎么用?Scala Cluster使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Cluster类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。

示例1: Validator

//设置package包名称以及导入依赖的类
package com.bwsw.tstreams.velocity

import java.net.InetSocketAddress
import java.util.UUID

import com.datastax.driver.core.Cluster

import scala.collection.mutable.ListBuffer

object Validator {
  def isSorted(list: ListBuffer[UUID]): Boolean = {
    if (list.isEmpty)
      return true
    var curVal = list.head
    var v = 0
    list foreach { el =>
      if (el.timestamp() < curVal.timestamp()) {
        println(s"value=$v")
        return false
      }
      if (el.timestamp() > curVal.timestamp())
        curVal = el
      v += 1
    }
    true
  }

  def main(args: Array[String]) {
    //    if (args.length != 1)
    //      throw new IllegalArgumentException("specify [keyspace]")
    //    val keyspace = args(0)

    val cluster = Cluster.builder().addContactPointsWithPorts(new InetSocketAddress("176.120.27.82", 9042)).build()
    val session = cluster.connect()

    val set = session.execute(s"select * from velocity.commit_log").all()
    val it = set.iterator()
    val buffers = scala.collection.mutable.Map[Int, ListBuffer[UUID]]()

    while (it.hasNext) {
      val row = it.next()
      val partition = row.getInt("partition")
      val uuid = row.getUUID("transaction")
      if (!buffers.contains(partition)) {
        buffers(partition) = ListBuffer(uuid)
      } else {
        buffers(partition) += uuid
      }
    }

    val checkVal = buffers.map(x => isSorted(x._2)).reduceLeft((a, b) => a & b)

    if (checkVal)
      println("sorted")
    else
      println("not sorted")

    cluster.close()
    session.close()
  }
} 
开发者ID:bwsw,项目名称:t-streams-pts-benchmark,代码行数:62,代码来源:Validator.scala

示例2: Connector

//设置package包名称以及导入依赖的类
package connector

import com.datastax.driver.core.{Session, Cluster}
import com.websudos.phantom.connectors.{KeySpace, SessionProvider}


trait CassandraConnector extends SessionProvider {

  implicit val space: KeySpace = Connector.keySpace
  val cluster = Connector.cluster
  override implicit lazy val session = Connector.session
}

object Connector {
  val keySpace = KeySpace("MovieLens")
  val cluster = Cluster.builder().addContactPoint("192.168.99.100").build()
  val session = cluster.connect(keySpace.name)
} 
开发者ID:abhitechdojo,项目名称:MovieLensWebServices,代码行数:19,代码来源:CassandraConnector.scala

示例3: AwsS3USWest1ConnectionFactoryTest

//设置package包名称以及导入依赖的类
package co.verdigris.spark.connector.cql

import com.datastax.driver.core.Cluster

class AwsS3USWest1ConnectionFactoryTest extends ConnectionFactorySpec {
  override def beforeAll {
    super.beforeAll

    factory = AwsS3USWest1ConnectionFactory
  }

  describe(".clusterBuilder") {
    it("should return a new Cluster.Builder instance") {
      factory.clusterBuilder(cassandraConf) shouldBe a [Cluster.Builder]
    }
  }

  describe(".createCluster") {
    it("should return a new Cluster instance") {
      factory.createCluster(cassandraConf) shouldBe a [Cluster]
    }
  }
} 
开发者ID:VerdigrisTech,项目名称:spark-cassandra-connection-factory,代码行数:24,代码来源:AwsS3USWest1ConnectionFactoryTest.scala

示例4: MetadataCreator

//设置package包名称以及导入依赖的类
package com.bwsw.tstreams.velocity

import com.bwsw.tstreams.common.CassandraHelper
import com.datastax.driver.core.Cluster

object MetadataCreator {
  def main(args: Array[String]) {
    import Common._
    val cluster = Cluster.builder().addContactPoint("localhost").build()
    val session = cluster.connect()
    try {
      session.execute(s"DROP KEYSPACE $keyspace")
    }
    catch {
      case e: Exception => println(s"msg=${e.getMessage}")
    }
    CassandraHelper.createKeyspace(session, keyspace)
    CassandraHelper.createMetadataTables(session, keyspace)
    cluster.close()
    session.close()
  }
} 
开发者ID:bwsw,项目名称:t-streams-pts-benchmark,代码行数:23,代码来源:MetadataCreator.scala

示例5: Config

//设置package包名称以及导入依赖的类
package conf.connection

import java.net.InetAddress

import com.datastax.driver.core.Cluster
import com.typesafe.config.ConfigFactory
import com.websudos.phantom.connectors.{KeySpace, SessionProvider}
import com.websudos.phantom.dsl.Session

import scala.collection.JavaConversions._



object Config {
  val config = ConfigFactory.load()

}

trait DefaultsConnector extends SessionProvider {
  val config = ConfigFactory.load()
  implicit val space: KeySpace = DataConnection.keySpace
  val cluster = DataConnection.cluster
  override implicit lazy val session: Session = DataConnection.session
}

object DataConnection {
  val config = ConfigFactory.load()
  val hosts: Seq[String] = Config.config.getStringList("cassandra.host").toList
  val inets = hosts.map(InetAddress.getByName)
  val keySpace: KeySpace = KeySpace(Config.config.getString("cassandra.keyspace"))
  val cluster =
    Cluster.builder()
      .addContactPoints(inets)
      .withClusterName(Config.config.getString("cassandra.cluster"))
//      .withCredentials(config.getString("cassandra.username"), config.getString("cassandra.password"))
      .build()
  val session: Session = cluster.connect(keySpace.name)
} 
开发者ID:Masebeni,项目名称:hwork,代码行数:39,代码来源:Connection.scala

示例6: Config

//设置package包名称以及导入依赖的类
package conf.connection

import java.net.InetAddress

import com.datastax.driver.core.Cluster
import com.mongodb.casbah.{MongoDB, MongoClientURI, MongoClient}
import com.typesafe.config.ConfigFactory
import com.websudos.phantom.connectors.{KeySpace, SessionProvider}
import com.websudos.phantom.dsl.Session

import scala.collection.JavaConversions._



object Config {
  val config = ConfigFactory.load()

}

object HashDB {
  def getConnection(): MongoDB={
    val hosts = Config.config.getString("mongodb.host")
    val database = Config.config.getString("mongodb.database")
    val uri = MongoClientURI(hosts)
    MongoClient(uri)(database)
  }
}

trait DefaultsConnector extends SessionProvider {
  val config = ConfigFactory.load()
  implicit val space: KeySpace = DataConnection.keySpace
  val cluster = DataConnection.cluster
  override implicit lazy val session: Session = DataConnection.session
}

object DataConnection {
  val config = ConfigFactory.load()
  val hosts: Seq[String] = Config.config.getStringList("cassandra.host").toList
  val inets = hosts.map(InetAddress.getByName)
  val keySpace: KeySpace = KeySpace(Config.config.getString("cassandra.keyspace"))

  val cluster =
    Cluster.builder()
      .addContactPoints(inets)
      .withClusterName(Config.config.getString("cassandra.cluster"))
//      .withCredentials(config.getString("cassandra.username"), config.getString("cassandra.password"))
      .build()
  val session: Session = cluster.connect(keySpace.name)
} 
开发者ID:Thulebona,项目名称:hashpvtapi,代码行数:50,代码来源:Connection.scala

示例7: CassandraAsyncContextImpl

//设置package包名称以及导入依赖的类
package data

import javax.inject.{Inject, Singleton}

import com.datastax.driver.core.{Cluster, Session}
import com.typesafe.config.Config
import io.getquill.{CassandraAsyncContext, SnakeCase}
import play.api.inject.ApplicationLifecycle
import play.api.{Configuration, Environment, Logger}
import util.FutureUtils.toFutureUnit

import scala.concurrent.ExecutionContext

@Singleton
class CassandraAsyncContextImpl @Inject()(cassandra: CassandraCluster, conf: CassandraConfig)
  extends CassandraAsyncContext[SnakeCase](cassandra.cluster, conf.keySpace, 100L)

@Singleton
class CassandraCluster @Inject()(conf: CassandraConfig, appLifecycle: ApplicationLifecycle)(implicit executor: ExecutionContext) {

  import conf._

  private[data] val cluster =
    Cluster.builder()
      .addContactPoints(hosts: _*)
      .withPort(port)
      .build()

  private[data] def noKeySpaceSession: Session = cluster.connect()

  private[data] def stop() = toFutureUnit(cluster.closeAsync())

  Logger.info(s"Cassandra host to be used: '${hosts.mkString(",")}' with port:$port")
  appLifecycle.addStopHook(() => stop())
}

@Singleton
class CassandraConfig @Inject()(configuration: Configuration, environment: Environment) {
  val config: Config = configuration.underlying

  val keySpace = config.getString("devgym.db.cassandra.keyspace")
  val port = config.getInt("devgym.db.cassandra.port")

  val hosts: Seq[String] =
    configuration.getStringSeq("devgym.db.cassandra.hosts").get
} 
开发者ID:DmytroOrlov,项目名称:devgym,代码行数:47,代码来源:CassandraCluster.scala

示例8: CassandraSpec

//设置package包名称以及导入依赖的类
package hmda.query.cassandra

import com.datastax.driver.core.{ Cluster, Session }
import org.scalatest.{ BeforeAndAfterAll, MustMatchers, WordSpec }
import org.cassandraunit.CQLDataLoader
import org.cassandraunit.dataset.cql.ClassPathCQLDataSet
import org.cassandraunit.utils.EmbeddedCassandraServerHelper

class CassandraSpec extends WordSpec with MustMatchers with BeforeAndAfterAll {

  var cluster: Cluster = _
  var session: Session = _

  override def beforeAll(): Unit = {
    EmbeddedCassandraServerHelper.startEmbeddedCassandra(60000L)
    cluster = EmbeddedCassandraServerHelper.getCluster
    session = cluster.connect()
    loadData()
  }

  override def afterAll(): Unit = {
    EmbeddedCassandraServerHelper.cleanEmbeddedCassandra()
  }

  def loadData(): Unit = {
    val dataLoader = new CQLDataLoader(session)
    dataLoader.load(new ClassPathCQLDataSet("simple.cql", "hmda_query"))
  }

  "Cassandra" must {
    "Select from table" in {
      val resultSet = session.execute("select * from myTable where id = 'myKey01'")
      resultSet.iterator().next().getString("value") mustBe "myValue01"
    }
  }

} 
开发者ID:cfpb,项目名称:hmda-platform,代码行数:38,代码来源:CassandraSpec.scala

示例9: CassandraDao

//设置package包名称以及导入依赖的类
package io.gzet.story.web.dao

import com.datastax.driver.core.Cluster
import io.gzet.story.model.Article
import io.gzet.story.util.SimhashUtils._
import io.gzet.story.web.SimpleConfig

import scala.collection.JavaConversions._
import scala.language.postfixOps

class CassandraDao() extends SimpleConfig {

  private val cluster = Cluster.builder().addContactPoint(cassandraHost).withPort(cassandraPort).build()
  val session = cluster.connect()

  def count(): Long = {
    val stmt = s"SELECT count(*) FROM $cassandraKeyspace.$cassandraTable;"
    val results = session.execute(stmt).all()
    results map { row =>
      row.getLong(0)
    } head
  }

  def findDuplicates(hash: Int): List[Article] = {
    searchmasks flatMap { mask =>
      val searchHash = mask ^ hash
      val stmt = s"SELECT hash, url, title, body FROM $cassandraKeyspace.$cassandraTable WHERE hash = $searchHash;"
      val results = session.execute(stmt).all()
      results map { row =>
        Article(row.getInt("hash"), row.getString("body"), row.getString("title"), row.getString("url"))
      }
    } toList
  }

} 
开发者ID:PacktPublishing,项目名称:Mastering-Spark-for-Data-Science,代码行数:36,代码来源:CassandraDao.scala

示例10: Config

//设置package包名称以及导入依赖的类
package conf.connection

import java.net.InetAddress

import com.datastax.driver.core.Cluster
import com.typesafe.config.ConfigFactory
import com.websudos.phantom.connectors.{KeySpace, SessionProvider}
import com.websudos.phantom.dsl.Session

import scala.collection.JavaConversions._



object Config {
  val config = ConfigFactory.load()

}

trait DefaultsConnector extends SessionProvider {
  val config = ConfigFactory.load()
  implicit val space: KeySpace = DataConnection.keySpace
  val cluster = DataConnection.cluster
  override implicit lazy val session: Session = DataConnection.session
}

object DataConnection {
  val config = ConfigFactory.load()
  val hosts: Seq[String] = Config.config.getStringList("cassandra.host").toList
  val inets = hosts.map(InetAddress.getByName)
  val keySpace: KeySpace = KeySpace(Config.config.getString("cassandra.keyspace"))

  val cluster =
    Cluster.builder()
      .addContactPoints(inets)
      .withClusterName(Config.config.getString("cassandra.cluster"))
//      .withCredentials(config.getString("cassandra.username"), config.getString("cassandra.password"))
      .build()
  val session: Session = cluster.connect(keySpace.name)
} 
开发者ID:roclet,项目名称:hashworkapi,代码行数:40,代码来源:Connection.scala

示例11: Launcher

//设置package包名称以及导入依赖的类
package com.knoldus

import com.datastax.driver.core.{Cluster, ResultSet, Session}
import com.knoldus.utils.ConfigReader

import scala.collection.JavaConversions
import scala.util.{Failure, Success, Try}




object Launcher extends App {

  Try {
    CassandraConnector.getCasssandraBuilder
  } match {
    case Success(cluster) =>
      val session = getCassandraSession(ConfigReader.getKeyspaceName, cluster)
      QueryHandler.runQuery(session)
      session.close()
      cluster.close()
    case Failure(exception) => println("Unable to Connect to Cassandra" + exception)
  }

  private def getCassandraSession(keyspace: String, cluster: Cluster): Session = {
    Try{
      CassandraConnector.getSession(keyspace, cluster)
    } match {
      case Success(session) => session
      case Failure(exception) => throw new Exception("Unable to connect to keyspace" + exception)
    }
  }


} 
开发者ID:SangeetaGulia,项目名称:activator-cassandra-scala-template.g8,代码行数:36,代码来源:Launcher.scala

示例12: DatabaseUtil

//设置package包名称以及导入依赖的类
package com.yper.utils

import com.datastax.driver.core.{Cluster, Session}


object DatabaseUtil {

  val keyspace = "yper"

  def getSession: Session = DatabaseUtil.synchronized {
    try {
      val builder = Cluster
        .builder()
        .addContactPoint("localhost")
        .withPort(9042)
        .build()
      builder.connect(keyspace)
    } catch {
      case e: Exception => {
        println(e.getMessage)
        throw e
      }
    }
  }

} 
开发者ID:vi-kas,项目名称:yper,代码行数:27,代码来源:DatabaseUtil.scala

示例13: CassandraFactory

//设置package包名称以及导入依赖的类
package com.bwsw.sj.common.utils

import java.net.InetSocketAddress

import com.bwsw.tstreams.common.{CassandraConnectorConf, CassandraHelper}
import com.bwsw.tstreams.data.cassandra.Factory
import com.bwsw.tstreams.metadata.MetadataStorageFactory
import com.datastax.driver.core.{Cluster, Session}

class CassandraFactory {

  import scala.collection.JavaConverters._

  private var cluster: Cluster = null
  private var session: Session = null
  private var cassandraConnectorConf: CassandraConnectorConf = null
  private val metadataStorageFactory = new MetadataStorageFactory()
  private val dataStorageFactory = new Factory()

  def open(hosts: Set[(String, Int)]) = {
    val cassandraHosts = hosts.map(s => new InetSocketAddress(s._1, s._2))
    cluster = Cluster.builder().addContactPointsWithPorts(cassandraHosts.toList.asJava).build()
    session = cluster.connect()
    cassandraConnectorConf = CassandraConnectorConf.apply(cassandraHosts)
  }

  def getDataStorage(keyspace: String) = {
    dataStorageFactory.getInstance(
      cassandraConnectorConf,
      keyspace = keyspace)
  }

  def getMetadataStorage(keyspace: String) = {
    metadataStorageFactory.getInstance(
      cassandraConnectorConf,
      keyspace = keyspace)
  }

  def createKeyspace(keyspace: String) = {
    CassandraHelper.createKeyspace(session, keyspace)
  }

  def createDataTable(keyspace: String) = {
    CassandraHelper.createDataTable(session, keyspace)
  }

  def createMetadataTables(keyspace: String) = {
    CassandraHelper.createMetadataTables(session, keyspace)
  }

  def dropKeyspace(keyspace: String) = {
    CassandraHelper.dropKeyspace(session, keyspace)
  }

  def close() = {
    metadataStorageFactory.closeFactory()
    session.close()
    cluster.close()
  }
} 
开发者ID:bwsw,项目名称:sj-platform,代码行数:61,代码来源:CassandraFactory.scala

示例14: Post

//设置package包名称以及导入依赖的类
package demo6

import java.util.UUID
import com.datastax.driver.core.{Row, Cluster, Session}
import troy.dsl._
import troy.driver.DSL._

import scala.concurrent.Await
import scala.concurrent.duration.Duration

case class Post(id: UUID, title: String)

object Main extends App {
  import scala.concurrent.ExecutionContext.Implicits.global

  val port: Int = 9042
  val host: String = "127.0.0.1"

  private val cluster =
    new Cluster.Builder().addContactPoints(host).withPort(port).build()

  implicit val session: Session = cluster.connect()

  val create = withSchema {
    (authorId: String, title: String) =>
      cql"""
         INSERT INTO test.posts (author_id , post_id , post_title )
         VALUES ( $authorId, now(), $title);
       """.prepared.executeAsync
  }

  val listByAuthor = withSchema {
    (authorId: String) =>
      cql"""
         SELECT post_id, post_title
         FROM test.posts
         WHERE author_id = $authorId
       """
        .prepared
        .executeAsync
        .as(Post)
  }

  println(Await.result(create("test", "title"), Duration(1, "second")))
  println(Await.result(listByAuthor("test"), Duration(1, "second")))

  session.close()
  cluster.close()
} 
开发者ID:schemasafe,项目名称:troy,代码行数:50,代码来源:Main.scala

示例15: Post

//设置package包名称以及导入依赖的类
package demo2

import java.util.UUID
import com.datastax.driver.core.{Row, Cluster, Session}
import troy.dsl._
import troy.driver.DSL._

import scala.concurrent.Await
import scala.concurrent.duration.Duration

case class Post(id: UUID, title: String)

object Main extends App {
  import scala.concurrent.ExecutionContext.Implicits.global

  val port: Int = 9042
  val host: String = "127.0.0.1"

  private val cluster =
    new Cluster.Builder().addContactPoints(host).withPort(port).build()

  implicit val session: Session = cluster.connect()

  val listByAuthor = withSchema {
    (authorId: String) =>
      cql"""
         SELECT post_id, post_title
         FROM test.posts
         WHERE author_id = $authorId
       """
        .prepared
        .executeAsync
        .as(Post)
  }

  val result = listByAuthor("test")
  println(Await.result(result, Duration(1, "second")))

  session.close()
  cluster.close()
} 
开发者ID:schemasafe,项目名称:troy,代码行数:42,代码来源:Main.scala


注:本文中的com.datastax.driver.core.Cluster类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。