此处会介绍多语言Driver(Python、C++)通过Cassandra CQL 访问Lindorm的例子,其他访问方式可以参考社区文档

Cassandra CQL Python Driver访问Lindorm

安装Datastax Python SDK库

# 指定版本安装(建议安装3.x版本)
pip install cassandra-driver==3.19.0
# 安装最新版本
pip install cassandra-driver
# https://pypi.org/project/cassandra-driver/#history

编写Python访问代码

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import logging
import sysfrom cassandra.cluster
import Clusterfrom cassandra.auth 
import PlainTextAuthProvider

logging.basicConfig(stream=sys.stdout, level=logging.INFO)

cluster = Cluster(
    # 此处填写数据库连接点地址(公网或者内网的)
    contact_points=["ip"],
    # 填写账户名密码(如果忘记可以在 帐号管理 处重置
    auth_provider=PlainTextAuthProvider("cassandra", "123456"))

session = cluster.connect()
# 建keyspace
session.execute(
                "CREATE KEYSPACE IF NOT EXISTS testKeyspace WITH replication = {'class':'SimpleStrategy', 'replication_factor':1};");

# 建table
session.execute(
                "CREATE TABLE IF NOT EXISTS testKeyspace.testTable (id int PRIMARY KEY, name text,age int,address text);");

#执行写
session.execute(
                "INSERT INTO testKeyspace.testTable (id, name, age, address) VALUES ( 1, 'testname', 11, 'hangzhou');");

#读操作
rows = session.execute(
                "SELECT * FROM testKeyspace.testTable ;");

# 打印每行信息到控制台
for row in rows:
    print("# row: {}".format(row))

# 关闭Session
session.shutdown()

# 关闭
Clustercluster.shutdown()

通过Datastax c++ 链接获取相关平台下的rpm包。

编写访问代码

CassFuture* connect_future = NULL;
CassCluster* cluster = cass_cluster_new();
CassSession* session = cass_session_new();
char* hosts = "ip";//控制台获取ip

//建立相关连接
cass_cluster_set_contact_points(cluster, hosts);
connect_future = cass_session_connect(session, cluster);

//DDL操作
if (cass_future_error_code(connect_future) == CASS_OK) {
    CassFuture* close_future = NULL;

    
    const char* query = "SELECT  name FROM testKeyspace.testTable ";
    CassStatement* statement = cass_statement_new(query, 0);

    CassFuture* result_future = cass_session_execute(session, statement);

    if (cass_future_error_code(result_future) == CASS_OK) {
      //获取相关的结果
      const CassResult* result = cass_future_get_result(result_future);
      const CassRow* row = cass_result_first_row(result);

      if (row) {
        const CassValue* value = cass_row_get_column_by_name(row, "name");
        //打印结果
        const char* name;
        size_t name_length;
        cass_value_get_string(value, &name, &name_length);
        printf("release_version: '%.*s'\n", (int)name_length, name);
      }

      cass_result_free(result);
    } else {
      //异常处理
      const char* message;
      size_t message_length;
      cass_future_error_message(result_future, &message, &message_length);
      fprintf(stderr, "Unable to run query: '%.*s'\n", (int)message_length, message);
    }

    cass_statement_free(statement);
    cass_future_free(result_future);

    //手工释放资源信息
    close_future = cass_session_close(session);
    cass_future_wait(close_future);
    cass_future_free(close_future);
  } else {
    //异常处理
    const char* message;
    size_t message_length;
    cass_future_error_message(connect_future, &message, &message_length);
    fprintf(stderr, "Unable to connect: '%.*s'\n", (int)message_length, message);
  }

  cass_future_free(connect_future);
  cass_cluster_free(cluster);
  cass_session_free(session);