使用 Java 遗留 driver 将 Cassandra Cluster EC2 实例替换为 AWS Keyspace
Replacing Cassandra Cluster EC2 Instances with AWS Keyspace using Java legacy driver
我们正在尝试用 AWS Keyspace 替换我们现有的 Cassandra EC2 集群,并且我们正在尝试保留我们的旧代码库。我们的想法是简单地摆脱 devops 业务,让我们的 Cassandra 由 AWS 管理(扩展、升级等)。查看他们提供的指南
https://docs.aws.amazon.com/keyspaces/latest/devguide/using_java_driver.html
他们使用的 driver 与我们目前使用的不同(更新):
我们现在的driver:
<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-core</artifactId>
<version>3.3.0</version>
</dependency>
他们的例子:
<dependency>
<groupId>com.datastax.oss</groupId>
<artifactId>java-driver-core</artifactId>
<version>4.4.0</version>
</dependency>
代码示例似乎与我们现有的代码完全不同。
问题 - 是否有人使用旧 driver 和旧代码成功迁移到 AWS Keyspaces?或者这需要升级吗?我的犹豫是我们有很多代码,考虑到重构的成本,放弃 Cassandra 并从其他东西(DynamoDB、MongoDB 等)重新开始可能更容易。
Amazon Keyspaces 可以非常简单地使用 3.x 驱动程序。虽然 4.x 驱动程序有一些好处,例如外部化配置,但无需升级即可移动 Amazon Keyspaces。
您可以使用传统的 user/name 密码验证或 sigv4 插件。
<!-- https://mvnrepository.com/artifact/software.aws.mcs/aws-sigv4-auth-cassandra-java-driver-plugin -->
<dependency>
<groupId>software.aws.mcs</groupId>
<artifactId>aws-sigv4-auth-cassandra-java-driver-plugin_3</artifactId>
<version>3.0.3</version>
</dependency>
连接 java 3.x 并写入键空间的代码示例。
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.PreparedStatement;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import software.aws.mcs.auth.SigV4AuthProvider;
import java.net.InetSocketAddress;
import java.util.Collections;
import java.util.List;
public class OrderFetcher {
public final static String TABLE_FORMAT = "%-25s%s\n";
public final static int KEYSPACES_PORT = 9142;
public static Cluster connectToCluster(String region, List<InetSocketAddress> contactPoints) {
SigV4AuthProvider provider = new SigV4AuthProvider(region);
return Cluster.builder()
.addContactPointsWithPorts(contactPoints)
.withPort(KEYSPACES_PORT)
.withAuthProvider(provider)
.withSSL()
.build();
}
public static void main(String[] args) {
if (args.length != 3) {
System.err.println("Usage: OrderFetcher <region> <endpoint> <customer ID>");
System.exit(1);
}
String region = args[0];
List<InetSocketAddress> contactPoints = Collections.singletonList(new InetSocketAddress(args[1], KEYSPACES_PORT));
try (Cluster cluster = connectToCluster(region, contactPoints)) {
Session session = cluster.connect();
// Use a prepared query for quoting
PreparedStatement prepared = session.prepare("select * from acme.orders where customer_id = ?");
// We use execute to send a query to Cassandra. This returns a ResultSet, which is essentially a collection
// of Row objects.
ResultSet rs = session.execute(prepared.bind(args[2]));
// Print the header
System.out.printf(TABLE_FORMAT, "Date", "Order Id");
for (Row row : rs) {
System.out.printf(TABLE_FORMAT, row.getTimestamp("order_timestamp"), row.getUUID("order_id"));
}
}
}
}
这是 link github
的示例
我们正在尝试用 AWS Keyspace 替换我们现有的 Cassandra EC2 集群,并且我们正在尝试保留我们的旧代码库。我们的想法是简单地摆脱 devops 业务,让我们的 Cassandra 由 AWS 管理(扩展、升级等)。查看他们提供的指南
https://docs.aws.amazon.com/keyspaces/latest/devguide/using_java_driver.html
他们使用的 driver 与我们目前使用的不同(更新):
我们现在的driver:
<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-core</artifactId>
<version>3.3.0</version>
</dependency>
他们的例子:
<dependency>
<groupId>com.datastax.oss</groupId>
<artifactId>java-driver-core</artifactId>
<version>4.4.0</version>
</dependency>
代码示例似乎与我们现有的代码完全不同。
问题 - 是否有人使用旧 driver 和旧代码成功迁移到 AWS Keyspaces?或者这需要升级吗?我的犹豫是我们有很多代码,考虑到重构的成本,放弃 Cassandra 并从其他东西(DynamoDB、MongoDB 等)重新开始可能更容易。
Amazon Keyspaces 可以非常简单地使用 3.x 驱动程序。虽然 4.x 驱动程序有一些好处,例如外部化配置,但无需升级即可移动 Amazon Keyspaces。
您可以使用传统的 user/name 密码验证或 sigv4 插件。
<!-- https://mvnrepository.com/artifact/software.aws.mcs/aws-sigv4-auth-cassandra-java-driver-plugin -->
<dependency>
<groupId>software.aws.mcs</groupId>
<artifactId>aws-sigv4-auth-cassandra-java-driver-plugin_3</artifactId>
<version>3.0.3</version>
</dependency>
连接 java 3.x 并写入键空间的代码示例。
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.PreparedStatement;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import software.aws.mcs.auth.SigV4AuthProvider;
import java.net.InetSocketAddress;
import java.util.Collections;
import java.util.List;
public class OrderFetcher {
public final static String TABLE_FORMAT = "%-25s%s\n";
public final static int KEYSPACES_PORT = 9142;
public static Cluster connectToCluster(String region, List<InetSocketAddress> contactPoints) {
SigV4AuthProvider provider = new SigV4AuthProvider(region);
return Cluster.builder()
.addContactPointsWithPorts(contactPoints)
.withPort(KEYSPACES_PORT)
.withAuthProvider(provider)
.withSSL()
.build();
}
public static void main(String[] args) {
if (args.length != 3) {
System.err.println("Usage: OrderFetcher <region> <endpoint> <customer ID>");
System.exit(1);
}
String region = args[0];
List<InetSocketAddress> contactPoints = Collections.singletonList(new InetSocketAddress(args[1], KEYSPACES_PORT));
try (Cluster cluster = connectToCluster(region, contactPoints)) {
Session session = cluster.connect();
// Use a prepared query for quoting
PreparedStatement prepared = session.prepare("select * from acme.orders where customer_id = ?");
// We use execute to send a query to Cassandra. This returns a ResultSet, which is essentially a collection
// of Row objects.
ResultSet rs = session.execute(prepared.bind(args[2]));
// Print the header
System.out.printf(TABLE_FORMAT, "Date", "Order Id");
for (Row row : rs) {
System.out.printf(TABLE_FORMAT, row.getTimestamp("order_timestamp"), row.getUUID("order_id"));
}
}
}
}
这是 link github
的示例