0

I am a scala newbie and really need help with an issue I've been experiencing with some code. I have set up the following code to create cassandra connections:

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

}

While building the project in IntelliJ, the following error happens:

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

Found something similar to my problem here, and tried to change my code to the following:

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

This resolved the issue and now the build finishes successfully but I am not so sure if what I've done is necessarily correct, so I'd really appreciate any help in this regard.

ahajib
  • 12,838
  • 29
  • 79
  • 120

1 Answers1

0

DCAwareRoundRobinPolicy uses the Builder Pattern to decouple the public API for instance creation from the particular implementation of the constructors for the instances. This is accomplished by not exposing a public constructor and instead only allowing DCAwareRoundRobinPolicy.Builder to construct instances of DCAwareRoundRobinPolicy.

You're using it as it's intended (assuming that you're passing options which match what you're expecting).

Levi Ramsey
  • 18,884
  • 1
  • 16
  • 30