Search code examples
cassandradatastax-java-driver

How to set DcInferringLoadBalancingPolicy in CqlSessionBuilder programmatically


I using 4.4.0 datastax-java-driver. In my scenario, I have to provide contacts points as I am connecting to remote cluster. If I am doing so I am getting following error - Since you provided explicit contact points, the local DC must be explicitly set. I also don't have option to provide this explicitly as I am connecting to different cluster on demand which can be in different data centre. I have found option to set DcInferringLoadBalancingPolicy to infer data centre But I am not sure how to set this in CqlSessionBuilder. Please help me with this.


Solution

  • You need be very careful with it - it's mostly for people who are building tools, such as IDEs, etc. For applications itself it's better to pass datacenter name explicitly - either via config file, or via Java system property.

    In short it could be done as following:

    ProgrammaticDriverConfigLoaderBuilder configBuilder = 
         DriverConfigLoader.programmaticBuilder();
    configBuilder.withClass(DefaultDriverOption.LOAD_BALANCING_POLICY_CLASS,
         DcInferringLoadBalancingPolicy.class);
    DriverConfigLoader loader = configBuilder.endProfile().build();
    CqlSessionBuilder clusterBuilder = CqlSession.builder()
                .addContactPoints(hosts);
    CqlSession session = clusterBuilder.withConfigLoader(loader).build();