Java操作Cassandra简单示范

 

Java直连cassandra datastax

 

java简单的直连cassandra,引入jar包

<dependency>
    <!--<groupId>com.datastax.cassandra</groupId>-->
    <!--<artifactId>cassandra-driver-core</artifactId>-->
    <!--<version>3.5.0</version>-->
</dependency>
package com.zhou.demo.utils;

import com.datastax.driver.core.*;
import com.datastax.driver.core.querybuilder.QueryBuilder;

/**
 * @auther:
 * @description:
 * @date: 17:46 2019/1/8
 */
public class CassandraSimpleDemo {

    public void simpleQuery(Session session) {
        // 执行CQL语句
        ResultSet rs = session.execute("select * from system_schema.tables");
        // 从返回结果中取出第一条结果
        Row row = rs.one();
        System.out.println("结果----:" + row.getString("keyspace_name"));
    }

    public void agileQuery(Session session) {
        ResultSet rs = session.execute(QueryBuilder.select().from("system_schema", "tables").where(QueryBuilder.eq("keyspace_name", "system_auth")));
        Row row = rs.one();
        System.out.println("结果----:" + row.getString("table_name"));
    }

    public static void main(String[] args) {
        Cluster cluster = null;
        try {
            // 创建连接到Cassandra的客户端
            cluster = Cluster.builder().addContactPoint("10.201.82.63").build();
            Metadata metadata = cluster.getMetadata();
            for (Host host : metadata.getAllHosts()) {
                System.out.println("host------" + host.getAddress());
            }
            // 创建用户会话
            Session session = cluster.connect();
            new CassandraSimpleDemo().agileQuery(session);
        } finally {
            // 调用cluster变量的close()函数并关闭所有与之关联的链接
            if (cluster != null) {
                cluster.close();
            }
        }

    }

}

cassandra和spring/springBoot结合

 

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-cassandra</artifactId>
</dependency>

配置项(简单配置一个就可以运行,还有一些其他的通过提示自行研究)

spring.data.cassandra.contact-points: ip

通过cassandraTemplate来操作,和entity进行关联,方便操作,其他增删改查按照api来;

类似mongodbTemplate;

String cql = String.format("select * from system_schema.tables");
List<Table> select = cassandraTemplate.select(cql, Table.class);

cassandra和SpringBoot结合可以说很方便了,就不过多赘述;

Spring参考文档和版本 https://spring.io/projects/spring-data-cassandra#learn

// 注解  表
org.springframework.data.cassandra.mapping.Table 
// 注解 主键按这个分区
org.springframework.data.cassandra.mapping.PrimaryKey
// 主键  联合主键  示例
//@PrimaryKeyColumn(ordinal = 0, type = PrimaryKeyType.PARTITIONED)
//@PrimaryKeyColumn(ordinal = 1, type = PrimaryKeyType.PARTITIONED)
org.springframework.data.cassandra.mapping.PrimaryKeyColumn

cassandra uuid匹配java.util.UUID 

cassandraTemplate.insert();  == merge 即插入又更新,只要PrimaryKey完全匹配

使用datastax可以参考zipkin的项目:zipkin-server和zipkin-dependencies

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Apache Cassandra is a massively scalable, peer-to-peer database designed for 100 percent uptime, with deployments in the tens of thousands of nodes, all supporting petabytes of data. This book offers a practical insight into building highly available, real-world applications using Apache Cassandra. The book starts with the fundamentals, helping you to understand how Apache Cassandra’s architecture allows it to achieve 100 percent uptime when other systems struggle to do so. You’ll get an excellent understanding of data distribution, replication, and Cassandra’s highly tunable consistency model. Then we take an in-depth look at Cassandra’s robust support for multiple data centers, and you’ll see how to scale out a cluster. Next, the book explores the domain of application design, with chapters discussing the native driver and data modeling. Lastly, you’ll find out how to steer clear of common anti-patterns and take advantage of Cassandra’s ability to fail gracefully. What you will learn Understand how the core architecture of Cassandra enables highly available applications Use replication and tunable consistency levels to balance consistency, availability, and performance Set up multiple data centers to enable failover, load balancing, and geographic distribution Add capacity to your cluster with zero downtime Take advantage of high availability features in the native driver Create data models that scale well and maximize availability Understand common anti-patterns so you can avoid them Keep your system working well even during failure scenarios

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值