ablog

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

Amazon KeySpaces に SigV4 認証でアクセスするサンプル Java アプリを実行する(DataStax Java Driver 3.x 版)

Amazon KeySpaces に SigV4 認証でアクセスするサンプル Java アプリを実行した手順(DataStax Java Driver 3.x 版)。

前提

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "Service": "ec2.amazonaws.com"
            },
            "Action": "sts:AssumeRole"
        }
    ]
}

準備手順

KeySpaces にテーブルを作成してデータを登録する

マネコンの Amazon KeySpaces の CQL エディタから以下の CQL を実行する
-キースペースを作成する

create keyspace acme with replication = {'class': 'SimpleStrategy', 'replication_factor' : 1 };
  • テーブルを作成する
create table acme.orders (
       customer_id text,
       order_timestamp timestamp,
       order_id uuid,
       primary key (customer_id, order_timestamp)) with clustering order by (order_timestamp desc);
  • レコードを挿入する
insert into acme.orders (customer_id, order_timestamp, order_id) values ('1234', toTimestamp(now()), uuid());
insert into acme.orders (customer_id, order_timestamp, order_id) values ('1234', toTimestamp(now()), uuid());
insert into acme.orders (customer_id, order_timestamp, order_id) values ('1234', toTimestamp(now()), uuid());
insert into acme.orders (customer_id, order_timestamp, order_id) values ('1234', toTimestamp(now()), uuid());
サンプル Java アプリをビルドする
  • Maven プロジェクトを作成する
$ mvn -B archetype:generate \
 -DarchetypeGroupId=org.apache.maven.archetypes \
 -DgroupId=com.keySpacesSigv4AuthJavaDriver3 \
 -DartifactId=keySpacesSigv4AuthJavaDriver3
  • keySpacesSigv4AuthJavaDriver3/pom.xml に以下を追加する
<dependency>
    <groupId>software.aws.mcs</groupId>
    <artifactId>aws-sigv4-auth-cassandra-java-driver-plugin_3</artifactId>
    <version>3.0.3</version>
</dependency>
<dependency>
    <groupId>com.datastax.cassandra</groupId>
    <artifactId>cassandra-driver-core</artifactId>
    <version>3.7.2</version>
</dependency>
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-assembly-plugin</artifactId>
				<version>3.1.1</version>
				<configuration>
					<appendAssemblyId>false</appendAssemblyId>
					<descriptorRefs>
						<descriptorRef>jar-with-dependencies</descriptorRef>
					</descriptorRefs>
					<archive>
						<manifest>
							<mainClass>com.keySpacesSigv4AuthJavaDriver3.App</mainClass>
						</manifest>
					</archive>
				</configuration>
				<executions>
					<execution>
						<id>make-assembly</id>
						<phase>package</phase>
						<goals>
							<goal>single</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-dependency-plugin</artifactId>
				<version>3.1.1</version>
				<executions>
					<execution>
						<id>copy-dependencies</id>
						<phase>package</phase>
						<goals>
							<goal>copy-dependencies</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-jar-plugin</artifactId>
				<configuration>
					<archive>
						<manifest>
							<mainClass>com.keySpacesSigv4AuthJavaDriver3.App</mainClass>
						</manifest>
					</archive>
				</configuration>
			</plugin>
		</plugins>
	</build>
  • aws-sigv4-auth-cassandra-java-driver-plugin/src/main/java/KeySpacesSigV4JavaDriver3/App.java を作成する
package com.keySpacesSigv4AuthJavaDriver3;

import software.aws.mcs.auth.SigV4AuthProvider;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;

public class App
{

    public static void main( String[] args )
    {
        String endPoint = "cassandra.ap-northeast-1.amazonaws.com";
        int portNumber = 9142;
        Session session = Cluster.builder()
                                         .addContactPoint(endPoint)
                                         .withPort(portNumber)
                                         .withAuthProvider(new SigV4AuthProvider("ap-northeast-1"))
                                         .withSSL()
                                         .build()
                                         .connect();

        ResultSet rs = session.execute("select * from acme.orders");
        Row row = rs.one();
        System.out.println(row.getString("customer_id"));
    }
}
  • ビルドする
$ pwd
/home/ec2-user/github/aws-sigv4-auth-cassandra-java-driver-plugin
$ mvn package

実行

  • 実行する
$ java -jar target/keySpacesSigv4AuthJavaDriver3-1.0-SNAPSHOT.jar 
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.
1234

参考