Cassandra教程
作者: 飘雪无垠
Java连接Cassandra

1、Maven依赖

<dependency>
  <groupId>com.datastax.cassandra</groupId>
  <artifactId>cassandra-driver-core</artifactId>
  <version>3.0.0</version>
</dependency>

2、创建Cluster

Cluster cluster = Cluster.builder()
                .addContactPoint("127.0.0.1")
                .withPort(9042)
                .withCredentials("admin", "admin")
                .build();
Session session = cluster.connect();

3、使用连接池

//连接池设置
PoolingOptions poolingOptions = new PoolingOptions();
// 每个连接的最大请求数
poolingOptions.setMaxRequestsPerConnection(HostDistance.LOCAL, 16);
// 表示和集群里的机器至少有2个连接 最多有4个连接
poolingOptions.setCoreConnectionsPerHost(HostDistance.LOCAL, 2)
    .setMaxConnectionsPerHost(HostDistance.LOCAL, 4)
    .setCoreConnectionsPerHost(HostDistance.REMOTE, 2)
    .setMaxConnectionsPerHost(HostDistance.REMOTE, 4);
    
Cluster cluster = Cluster.builder()
                    .addContactPoint("127.0.0.1")
                    .withPort(9042)
                    .withCredentials("admin", "admin")
                    .withPoolingOptions(poolingOptions)
                    .build();
Session session = cluster.connect();



标签: poolingoptions、cluster、hostdistance、admin、session
一个创业中的苦逼程序员
  • 回复
隐藏