首頁
云原生多模數(shù)據(jù)庫 Lindorm
操作指南
寬表引擎(兼容HBase或Cassandra)
應用開發(fā)教程
基于Cassandra CQL的應用開發(fā)
通過Cassandra CQL非Java Driver的應用開發(fā)
通過Cassandra CQL非Java Driver的應用開發(fā)
更新時間:
本文介紹多語言Driver(例如C++、Python、Nodejs和Go)通過Cassandra CQL訪問云原生多模數(shù)據(jù)庫 Lindorm寬表引擎的操作方法。
前提條件
已獲取Lindorm寬表引擎的CQL連接地址,具體操作請參見查看連接地址。
根據(jù)您使用的語言下載并安裝驅(qū)動程序。更多信息請參見Cassandra client drivers。
Cassandra CQL Python Driver訪問Lindorm寬表
安裝Datastax Python依賴包,具體操作請參見安裝Python SDK。
# 指定版本安裝(建議安裝3.x版本) pip install cassandra-driver==3.19.0 # 安裝最新版本 pip install cassandra-driver
訪問Lindorm寬表的示例代碼。
#!/usr/bin/env python # -*- coding: UTF-8 -*- import logging import sys from cassandra.cluster import Cluster from cassandra.auth import PlainTextAuthProvider logging.basicConfig(stream=sys.stdout, level=logging.INFO) cluster = Cluster( # 填寫Lindorm寬表引擎的CQL連接地址,不用填端口 contact_points=["ld-bp17j28j2y7pm****-proxy-lindorm.lindorm.rds.aliyuncs.com"], # 填寫用戶名和密碼,默認為root auth_provider=PlainTextAuthProvider("cassandra", "****")) session = cluster.connect() # 創(chuàng)建keyspace session.execute( "CREATE KEYSPACE IF NOT EXISTS testKeyspace WITH replication = {'class':'SimpleStrategy', 'replication_factor':1};"); # 創(chuàng)建table session.execute( "CREATE TABLE IF NOT EXISTS testKeyspace.testTable (id int PRIMARY KEY, name text,age int,address text);"); # 插入數(shù)據(jù) session.execute( "INSERT INTO testKeyspace.testTable (id, name, age, address) VALUES ( 1, 'testname', 11, 'hangzhou');"); # 查詢數(shù)據(jù) rows = session.execute( "SELECT * FROM testKeyspace.testTable ;"); # 打印每行數(shù)據(jù)到控制臺 for row in rows: print("# row: {}".format(row)) # 關閉Session session.shutdown() # 關閉 cluster.shutdown()
Cassandra CQL C++ Driver訪問Lindorm寬表
下載C++依賴包,下載鏈接請參見Datastax c++。
訪問Lindorm寬表的示例代碼。
CassFuture* connect_future = NULL; CassCluster* cluster = cass_cluster_new(); CassSession* session = cass_session_new(); //填寫Lindorm寬表引擎的CQL連接地址,不用填端口 char* hosts = "ld-bp17j28j2y7pm****-proxy-lindorm.lindorm.rds.aliyuncs.com"; //建立連接 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);
Cassandra CQL Nodejs Driver訪問Lindorm寬表
安裝Node.js依賴包。
npm install cassandra-driver
訪問Lindorm寬表的示例代碼。
const cassandra = require('cassandra-driver'); /** * 填寫Lindorm寬表引擎的CQL連接地址,不用填端口。dc名字填datacenter1 * 填寫用戶名和密碼,默認為root */ const client = new cassandra.Client({ contactPoints: ['ld-bp17j28j2y7pm****-proxy-lindorm.lindorm.rds.aliyuncs.com'], localDataCenter: 'datacenter1', credentials: { username: '用戶名', password: '密碼' } }); client.connect() .then(() => client.execute("CREATE KEYSPACE IF NOT EXISTS lindormtest WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '2' }")) .then(() => client.execute("CREATE TABLE IF NOT EXISTS lindormtest.nodejs (name text PRIMARY KEY, age int)")) .then(() => { return client.execute("INSERT INTO lindormtest.nodejs (name, age) VALUES ('lindorm', 10)"); }) .then(() => { return client.execute("SELECT name, age FROM lindormtest.nodejs WHERE name = 'lindorm' "); }) .then(result => { const row = result.first(); console.log('Obtained row: ', row); const p = row.age; }) .finally(() => client.shutdown());
Cassandra CQL go Driver訪問Lindorm寬表
安裝GoCQL。
go get github.com/gocql/gocql
訪問Lindorm寬表的示例代碼。
package main import ( "fmt" "log" "github.com/gocql/gocql" ) func main() { // 填寫Lindorm寬表引擎的CQL連接地址,不用填端口 cluster := gocql.NewCluster("ld-bp17j28j2y7pm****-proxy-lindorm.lindorm.rds.aliyuncs.com") cluster.Authenticator = gocql.PasswordAuthenticator{ Username:"用戶名", Password: "密碼", } session, _ := cluster.CreateSession() defer session.Close() //create keyspace if err := session.Query(`CREATE KEYSPACE ks WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 2}`).Exec(); err != nil { log.Fatal(err) } // create table if err := session.Query(`CREATE TABLE ks.tb (cn1 text PRIMARY KEY, cn2 text, cn3 text)`).Exec(); err != nil { log.Fatal(err) } // insert value if err := session.Query(`INSERT INTO ks.tb (cn1, cn2, cn3) VALUES (?, ?, ?)`, "v11", "v12", "v13").Exec(); err != nil { log.Fatal(err) } var column1 string var column2 string if err := session.Query(`SELECT cn1, cn2 FROM ks.tb WHERE cn1 = ?`, "v11").Consistency(gocql.One).Scan(&column1, &column2); err != nil { log.Fatal(err) } fmt.Println("ALL values:", column1, column2) var column3 string // list all tweets iter := session.Query(`SELECT * FROM ks.tb WHERE cn1 = ? `, "v11").Iter() for iter.Scan(&column1, &column2, &column3) { fmt.Println("ALL value:", column1, column2, column3) } if err := iter.Close(); err != nil { log.Fatal(err) } }
文檔內(nèi)容是否對您有幫助?