Cassandra Client for Vert.x

694 阅读3分钟

这是我参与8月更文挑战的第21天,活动详情查看:8月更文挑战

开始

pom.xml

<dependency>
 <groupId>io.vertx</groupId>
 <artifactId>vertx-cassandra-client</artifactId>
 <version>4.1.2</version>
</dependency>

Gradle

compile 'io.vertx:vertx-cassandra-client:4.1.2'

Cassandra 是一个分布式系统 , 拥有很多节点。链接Cassandra需要添加集群的各个节点

CassandraClientOptions options = new CassandraClientOptions()
  .addContactPoint("node1.address", 9142)
  .addContactPoint("node2.address", 9142)
  .addContactPoint("node3.address", 9142);
CassandraClient client = CassandraClient.create(vertx, options);

绑定keySpace
30 stars

API

Examples

Edit

v4.1.2

Cassandra Client for Vert.x

A Vert.x client allowing applications to interact with an Apache Cassandra service.

Getting started

To use this module, add the following to the dependencies section of your Maven POM file:

<dependency>
 <groupId>io.vertx</groupId>
 <artifactId>vertx-cassandra-client</artifactId>
 <version>4.1.2</version>
</dependency>

Or, if you use Gradle:

compile 'io.vertx:vertx-cassandra-client:4.1.2'

Creating a client

Client options

卡桑德拉是一个分布式系统,它可以有很多节点。要连接到卡桑德拉,在创建对象时需要指定某些聚类节点的地址:CassandraClientOptions

CassandraClientOptions options = new CassandraClientOptions()
  .addContactPoint("node1.address", 9142)
  .addContactPoint("node2.address", 9142)
  .addContactPoint("node3.address", 9142);
CassandraClient client = CassandraClient.create(vertx, options);

默认情况下,Vert.x 的卡桑德拉客户端连接到本地机器的端口,并且不与任何特定的密钥空间绑定。但是,您可以设置以下两种选项之一或两种选项:9042

CassandraClientOptions options = new CassandraClientOptions()
  .addContactPoint("localhost", 9142)
  .setKeyspace("my_keyspace");
CassandraClient client = CassandraClient.create(vertx, options);

同一个 keySpace 下的集群 的链接方式

CassandraClientOptions options = new CassandraClientOptions()
  .addContactPoint("node1.address", 9142)
  .addContactPoint("node2.address", 9142)
  .addContactPoint("node3.address", 9142)
  .setKeyspace("my_keyspace");
CassandraClient client = CassandraClient.createShared(vertx, "sharedClientName", options);

生命周期

创建客户端后,在第一个查询执行之前,它不会连接。 在VertX 内创建的 客户端在未部署时会自动停止,所以不需要在语法中调用 close 来停掉它。卡桑德拉克莱恩 (Vert.x 堆栈 - 文档 4.1.2 API) (vertx.io)

在其他情况下,必须手动关闭客户端

API

Streaming

流式查询适合产生迭代结果的查询,特别是对于大量的行非常有效。

cassandraClient.queryStream("SELECT my_string_col FROM my_keyspace.my_table where my_key = 'my_value'", queryStream -> {
  if (queryStream.succeeded()) {
    CassandraRowStream stream = queryStream.result();

    // resume stream when queue is ready to accept buffers again
    response.drainHandler(v -> stream.resume());

    stream.handler(row -> {
      String value = row.getString("my_string_col");
      response.write(value);

      // pause row stream when we buffer queue is full
      if (response.writeQueueFull()) {
        stream.pause();
      }
    });

    // end request when we reached end of the stream
    stream.endHandler(end -> response.end());

  } else {
    queryStream.cause().printStackTrace();
    // response with internal server error if we are not able to execute given query
    response
      .setStatusCode(500)
      .end("Unable to execute the query");
  }
});

以上的代码中,执行了查询,并且通过 HTTP 流式传输结果。

批量处理所有行

cassandraClient.executeWithFullFetch("SELECT * FROM my_keyspace.my_table where my_key = 'my_value'", executeWithFullFetch -> {
  if (executeWithFullFetch.succeeded()) {
    List<Row> rows = executeWithFullFetch.result();
    for (Row row : rows) {
      // handle each row here
    }
  } else {
    System.out.println("Unable to execute the query");
    executeWithFullFetch.cause().printStackTrace();
  }
});

ps:能够承受在内存中设置的完整结果时,才使用批量取件。

Collector queries

cassandraClient.execute("SELECT * FROM users", listCollector, ar -> {
  if (ar.succeeded()) {
    // Get the string created by the collector
    String list = ar.result();
    System.out.println("Got " + list);
  } else {
    System.out.println("Failure: " + ar.cause().getMessage());
  }
});

预加载查询

使用准备好的语句来解决多次使用的所有查询

cassandraClient.prepare("SELECT * FROM my_keyspace.my_table where my_key = ? ", preparedStatementResult -> {
  if (preparedStatementResult.succeeded()) {
    System.out.println("The query has successfully been prepared");
    PreparedStatement preparedStatement = preparedStatementResult.result();
    // now you can use this PreparedStatement object for the next queries
  } else {
    System.out.println("Unable to prepare the query");
    preparedStatementResult.cause().printStackTrace();
  }
});

然后使用PreparedStatement进行下一步的所有查询:

cassandraClient.execute(preparedStatement.bind("my_value"), done -> {
  ResultSet results = done.result();
  // handle results here
});

// Bulk fetching API
cassandraClient.executeWithFullFetch(preparedStatement.bind("my_value"), done -> {
  List<Row> results = done.result();
  // handle results here
});

// Streaming API
cassandraClient.queryStream(preparedStatement.bind("my_value"), done -> {
  CassandraRowStream results = done.result();
  // handle results here
});

RxJava 3 API

创建一个 Rxified client

CassandraClientOptions options = new CassandraClientOptions()
  .addContactPoint("node1.corp.int", 7000)
  .addContactPoint("node2.corp.int", 7000)
  .addContactPoint("node3.corp.int", 7000);
CassandraClient cassandraClient = CassandraClient.createShared(vertx, options);

查询

Streaming

cassandraClient.rxQueryStream("SELECT my_key FROM my_keyspace.my_table where my_key = my_value")
  // Convert the stream to a Flowable
  .flatMapPublisher(CassandraRowStream::toFlowable)
  .subscribe(row -> {
    // Handle single row
  }, t -> {
    // Handle failure
  }, () -> {
    // End of stream
  });

批量

cassandraClient.rxExecuteWithFullFetch("SELECT my_key FROM my_keyspace.my_table where my_key = my_value")
  .subscribe(rows -> {
    // Handle list of rows
  }, throwable -> {
    // Handle failure
  });