【问题标题】:Scala "constructor DCAwareRoundRobinPolicy in class DCAwareRoundRobinPolicy cannot be accessed in object CassandraConnector"Scala“无法在对象 CassandraConnector 中访问 DCAwareRoundRobinPolicy 类中的构造函数 DCAwareRoundRobinPolicy”
【发布时间】:2020-08-19 15:48:41
【问题描述】:

我是 scala 新手,我确实需要帮助解决我在使用某些代码时遇到的问题。我已经设置了以下代码来创建 cassandra 连接:

package some.package

import java.net.InetAddress
import java.util

import com.datastax.driver.core._
import com.datastax.driver.core.policies.{DCAwareRoundRobinPolicy, TokenAwarePolicy}

import scala.collection.JavaConversions._

object CassandraConnector {

  def getCluster(contactPointIpString: String, preferred_dc: String): Cluster = {

    val contactPointIpStrings = contactPointIpString.split(",").toList

    val contactPointIpList = contactPointIpStrings.flatMap { ipAddress: String => InetAddress.getAllByName(ipAddress) }
    println(s"Building Cluster w/ Contact Points: $contactPointIpList")

    Cluster.builder()
      .addContactPoints(contactPointIpList)
      .withClusterName("multi_dc_user_data")
      .withLoadBalancingPolicy(new TokenAwarePolicy(new DCAwareRoundRobinPolicy(preferred_dc)))
      .withQueryOptions(new QueryOptions().setConsistencyLevel(ConsistencyLevel.LOCAL_ONE))
      .build()
  }

}

在 IntelliJ 中构建项目时,出现以下错误:

constructor DCAwareRoundRobinPolicy in class DCAwareRoundRobinPolicy cannot be accessed in object CassandraConnector
      .withLoadBalancingPolicy(new TokenAwarePolicy(new DCAwareRoundRobinPolicy(preferred_dc)))

发现类似于我的问题here,并尝试将我的代码更改为以下内容:

val dcAwareRoundRobinPolicyBuilder = new DCAwareRoundRobinPolicy.Builder
val dcAwareRoundRobinPolicy = dcAwareRoundRobinPolicyBuilder.withLocalDc(preferred_dc)

Cluster.builder()
  .addContactPoints(contactPointIpList)
  .withClusterName("multi_dc_user_data")
  .withLoadBalancingPolicy(new TokenAwarePolicy(dcAwareRoundRobinPolicy.build()))
  .withQueryOptions(new QueryOptions().setConsistencyLevel(ConsistencyLevel.LOCAL_ONE))
  .build()

这解决了问题,现在构建成功完成,但我不确定我所做的是否一定正确,所以我非常感谢这方面的任何帮助。

【问题讨论】:

    标签: scala intellij-idea sbt


    【解决方案1】:

    DCAwareRoundRobinPolicy 使用构建器模式将用于实例创建的公共 API 与实例构造函数的特定实现分离。这是通过不公开公共构造函数而仅允许DCAwareRoundRobinPolicy.Builder 构造DCAwareRoundRobinPolicy 的实例来实现的。

    您正在按预期使用它(假设您传递的选项与您的期望相匹配)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多