ablog

不器用で落着きのない技術者のメモ

Java アプリから Amazon KeySpaces に insert すると"Consistency level LOCAL_ONE is not supported for this operation. Supported consistency levels are: LOCAL_QUORUM" エラー

事象

DataStax の Java ドライバを使って、Java アプリケーションから Amazon KeySpaces に insert すると、"Consistency level LOCAL_ONE is not supported for this operation. Supported consistency levels are: LOCAL_QUORUM" エラーになる。

  • src/main/java/com/keySpacesTest/App.java
package com.keySpacesTest;

import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.config.DriverConfigLoader;
import com.datastax.oss.driver.api.core.cql.BatchStatement;
import com.datastax.oss.driver.api.core.cql.BoundStatement;
import com.datastax.oss.driver.api.core.cql.ResultSet;
import com.datastax.oss.driver.api.core.cql.Row;
import com.datastax.oss.driver.api.core.cql.SimpleStatement;
import com.datastax.oss.protocol.internal.request.query.QueryOptions;
import com.datastax.oss.driver.api.core.cql.PreparedStatement;
import com.datastax.oss.driver.api.core.ConsistencyLevel;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );

        //Use DriverConfigLoader to load your configuration file
        DriverConfigLoader loader = DriverConfigLoader.fromClasspath("application.conf");
        try (CqlSession session = CqlSession.builder()
                .withConfigLoader(loader)
                .build()) {

            PreparedStatement prepared = session.prepare("insert into tutorialkeyspace.tutorialtable(age, name, email) values (?, ?, ?)");
            BoundStatement bound = prepared.bind(1,"shiotsu","chrai@co2.com");
            session.execute(bound);

        }
    }
}
  • 実行すると、"Consistency level LOCAL_ONE is not supported for this operation. Supported consistency levels are: LOCAL_QUORUM" エラーになる。
$ mvn clean
$ mvn package
$ java -jar target/keySpacesTest-1.0-SNAPSHOT.jar 
Hello World!
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" com.datastax.oss.driver.api.core.servererrors.InvalidQueryException: Consistency level LOCAL_ONE is not supported for this operation. Supported consistency levels are: LOCAL_QUORUM
        at com.datastax.oss.driver.api.core.servererrors.InvalidQueryException.copy(InvalidQueryException.java:48)
        at com.datastax.oss.driver.internal.core.util.concurrent.CompletableFutures.getUninterruptibly(CompletableFutures.java:149)
        at com.datastax.oss.driver.internal.core.cql.CqlRequestSyncProcessor.process(CqlRequestSyncProcessor.java:53)
        at com.datastax.oss.driver.internal.core.cql.CqlRequestSyncProcessor.process(CqlRequestSyncProcessor.java:30)
        at com.datastax.oss.driver.internal.core.session.DefaultSession.execute(DefaultSession.java:230)
        at com.datastax.oss.driver.api.core.cql.SyncCql

解決策

  • src/main/resources/application.conf に basic.request { consistency = LOCAL_QUORUM } を追記
datastax-java-driver {

    basic.contact-points = [ "cassandra.ap-northeast-1.amazonaws.com:9142"]
    advanced.auth-provider{
        class = PlainTextAuthProvider
        username = "keyspaces_user-at-********"
        password = "********"
    }
    basic.load-balancing-policy {
        local-datacenter = "ap-northeast-1"
        slow-replica-avoidance = false           
    }

    advanced.ssl-engine-factory {
        class = DefaultSslEngineFactory
        truststore-path = "./src/main/resources/cassandra_truststore.jks"
        truststore-password = "********"
        hostname-validation = false
      }
      
      basic.request {
            consistency = LOCAL_QUORUM
      }
}

実行結果

  • 実行すると
$ mvn clean
$ mvn package
$ java -jar target/keySpacesTest-1.0-SNAPSHOT.jar 
Hello World!
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
  • レコードの insert に成功している。

参考

To configure the consistency level programmatically, you can set the consistency with the appropriate Cassandra client drivers. For example, the 4.x version drivers allow you to set the consistency level in the app config file as shown below.

basic.request.consistency = LOCAL_QUORUM
Supported Apache Cassandra consistency levels in Amazon Keyspaces - Amazon Keyspaces (for Apache Cassandra)