Web_Advance
  • 本書簡介
  • Node.js 部分
    • Node 版本管理
    • 使用NPM
      • Yarn
    • 開始Node
    • Worker Thread
    • REPL
    • TCP
    • path
    • Cluster and Child_process
    • assert (自訂拋出的錯誤)
    • Stream(流)
    • util (工具類)
    • EventEmitter
    • fs 文件操作
    • Buffer
      • Binary Diff
      • 查看 Binary 檔案內容
    • Process (進程)
    • 錯誤處理
  • OS
  • Async Hook
  • TCP
  • HTTP
    • 有關爬蟲
    • HTTP/2
    • HTTP Protocal
  • HTTPS
    • HTTPS 流程
    • SSL pinning
    • HTTPS 封包解密
    • 建立自簽發 HTTPS 證書
    • 幫網站加上HTTPS
    • HTTPS原理
  • Crypto加密
  • 有關繼承
  • JS 基本
    • JavaScript 迴圈與異步處理
  • 使用 Express
    • 上傳檔案
    • 圖片伺服器
    • 簡單範例
  • 使用 Nest.js
  • 使用MongoDB
    • 設置帳戶登入權限
    • Mongoose 框架
    • 進階Mongo
    • 基本環境操作
    • MongoDB Sharding
  • 使用MySQL
    • Schema 架構設計
    • SQL 語法
    • SQL Procedure
    • Node.js 操作 MySQL
    • 使用 Sequelize
      • DB Migration
      • function
  • 使用PostgreSQL
    • 常見問題
    • replica
    • 基本指令
    • 使用Node.js操控pg
    • SQL 基礎
  • 使用TypeORM
  • RethinkDB
  • CSS 深度探討
    • Width, Height
  • React
    • 第三方組件
      • Formik
    • styled component
    • propTypes
    • React webpack 部署
    • React util
    • 寫component並且publish
    • create-react-app
    • Context API
    • i18n
    • Server side render
    • Next.js教學
    • Higher order component 與 Recompose
    • component 間 互相存取
    • React hook
  • React router
    • 自己寫一個Router
  • Redux
    • Redux Toolkit
    • 小技巧
    • Redux sagas
    • compose
  • React Native
    • adb
    • InApp Billing
    • Icon
    • SVG
    • Firebase
      • Phone Auth
    • 自動化測試
    • Splash screen
    • Websocket
    • Googla OAuth
      • iOS
      • Android
    • Facebook OAuth
      • iOS
      • Android
    • IOS
    • 第三方組件
      • Auth Code Input
      • Country Code Picker
      • onboarding screen
      • Toast
    • ESlint
    • Push notification
    • Android 上架步驟
    • Expo
    • router
      • react-navigation套件
    • 原生組件
      • RefreshControl
      • Modal
      • Alert
      • button
      • KeyboardAvoidingView
      • Drawer
      • Image
    • 限制螢幕垂直與水平
    • NativeBase UI
    • Debug
    • 常見問題
    • Network
    • 硬體操作
      • 隱藏鍵盤
      • 地理位置
      • 相機與圖片庫存取
    • Async Storage
    • Animation
    • Admob
  • JS 模組化
  • 使用 Webpack
  • 使用 Babel
  • JWT Token
  • ES6 ES7 ES8
    • Array method
    • ES8 Async
    • ES6 Proxy
    • ES6 Object
    • ES6 Arrow function
    • ES6 Promise
    • ES6 Symbol
    • ES6 Generator
    • ES6 Set,Map
    • ES6 Class
  • 模板引擎
    • Mustache
    • Handlebars.js
    • EJS
  • ESLint
  • 部屬到OpenShift
  • OpenStack
  • OAuth
    • Twitter OAuth
    • Google authenticator
    • facebook oauth
      • facebook like ,share
    • google oauth
  • Redis
  • 做一個簡單的markdown editor
  • Websocket
    • WebSocket 相關 Protocol
  • Sublime 安裝套件
  • Google api
    • Cloud Run
    • speech api
    • place autocomplete
    • Geocode
    • Map
      • React map
    • vision api
    • Google-recaptcha
    • Google sheet
  • Instagram API
  • Markdown 與 code pretty js
  • HTML5
    • IntersectionObserver
    • HTML5 audio
    • HTML5 Video 與 WebRTC
      • WebRTC 進階
      • WebEX API
    • HTML5 IndexedDB
  • Google Cloud Platform (GCP)
    • Cloud Storage
    • Cloud storage 串接 Cloud CDN
  • Vim 編輯器
  • 使用nginx
    • config
  • Unix 實用指令
    • 新 VPS 安裝流程
    • Ubuntu 22 安裝
    • Shell Script 教學
  • Git 實用指令
    • Git hook
    • 加上 SSH-key 到 GitHub
    • GPG簽名
  • SSH 實用指令
  • 有關Fetch與axios與跨域請求
  • 圖片上傳相關
    • imgur API
  • JS 格式轉換
  • js trick
  • AWS
    • AWS EBS
    • AWS HTTPS 憑證
    • AWS Cloudfront、ELB、ACL
    • AWS Athena
    • AWS CloudWatch、SNS
    • AWS RDS
    • AWS lambda
      • 範例
      • 加上權限控管
    • AWS S3
    • AWS DynamoDB
      • 結合Lambda
    • 快速把 EC2 串上 AWS Cloudfront CDN
    • AWS 證照相關
  • 有關日期Date
  • VS code 編輯器
    • VSCode 外掛 Plugin
  • CI with Gitlab&Jenkins
  • API 測試
    • Postman
      • 設置 Postman 環境變數
    • API Blueprint
    • swagger
      • 註解寫在Code內生成swagger UI
  • Javascript 實用Lib
  • 遠端寫程式
  • Quicktime錄影注意事項
  • Web開發進階Bug
  • Web壓力測試
  • LineBot
  • PM2部署
  • i18n
  • VPN
  • Protocol Buffers
  • Docker教學
    • LXC LXD
    • Docker Compose
    • Docker 原理
    • Docker 指令
  • E2E Testing
    • Cypress
    • PlayWright
    • Puppeteer 與其他 UI 測試工具
  • Unit Test (Jest & enzyme)
    • React Testing Library
    • mocha
  • Cassandra
    • cluster
  • Distribute Web
    • Dat project
    • IPFS project
  • Cluster and Child_process
  • 打包應用程式
  • Java
    • 使用gradle結合docker
  • Debug 頁面
  • Proxy
  • Chrome extension
  • 消息系統
    • RabbitMQ
  • 金流串接
    • Paypal
    • spgateway智富通
    • Stripe 串接
  • 有關Log
  • 設定 feature flag
  • Azure
    • Face API
    • Image Analyze API
    • Azure Serverless
    • Cosmos DB
      • 使用 SDK
      • 以 RESTful 操作 DB
      • 一致性策略與 DB replicate
  • NodeBB 筆記
  • 瀏覽器快取與緩存(Etag, If-None-Match)
  • 瀏覽器快取與緩存(Expires, Last-modified, Cache-Control)
  • Node.js 第三方模組
    • OpenCV
  • Kubernetes
    • 本地測試 MiniKube
  • Ngrok 使用
  • Telegram MiniAPP 開發
  • Firebase 教學
  • 演算法筆記
  • 圖表
    • Echart
    • TradingView 圖表
    • D3
    • 熱力圖 heatmap
  • 後端緩存 Cache
  • 資料一致性
  • Web 安全機制
    • Cookie 與 LocalStorage
  • Vue
    • Element UI
    • Devtool
    • Vuex
    • Vue router
  • 相關網路協議
    • 網路 IP 基礎
    • Google Search 技巧
    • 網路診斷工具
    • IP
    • DNS
  • GitLab 與 Drone
  • SMTP、POP、IMAP
    • SendGrid
  • IPC
  • 串流服務
    • Twilio
    • Agora
  • 其他資源
  • GraphQL
  • Typescript
  • UI 相關資源
  • FFmpeg
  • Unity 遊戲開發筆記
  • Influx DB
  • Windows 相關
  • DALL·E 3
  • Coap
  • Slack API
  • 資訊安全
    • 破解 ZIP 密碼
Powered by GitBook
On this page
  • SQL API
  • Cassandra API
  • Cassandra DB 簡介
  • CAP
  • Azure Cosmos DB 的 Cassandra API
  • 使用官方範例
  • 範例:

Was this helpful?

Edit on GitHub
  1. Azure
  2. Cosmos DB

使用 SDK

PreviousCosmos DBNext以 RESTful 操作 DB

Last updated 5 years ago

Was this helpful?

SQL API

上一篇稍微看過了 Azure Cosmos DB 的簡單操作後,這一篇會來講解它的大致結構,可參考下圖:

https://ithelp.ithome.com.tw/upload/images/20181102/20112426pH8epMIjCX.png

每個 Azure 帳號可以創建多個 database,每個 database 可以包含多個 collections,在 collections 中包含多個 documents。而在 collection 中可以加入 stored procedures、triggers、User Defined Functions(UDFs) 等等。

插入資料到資料庫

接著我們使用程式插入一筆資料試試:

yarn add @azure/cosmos
const cosmos = require('@azure/cosmos');
const CosmosClient = cosmos.CosmosClient;

const endpoint = "填上 443 port 的 DB Endpoint";
const masterKey = "填上金鑰";
const client = new CosmosClient({ endpoint, auth: { masterKey } });

const databaseDefinition = { id: 'TestDB' };
const collectionDefinition = { id: 'Fruits' };
const documentDefinition = { name: 'Green Apple', category: 'Apple', data: Date.now() };

async function createFruit() {
  const { body } = await client.database(databaseDefinition.id).container(collectionDefinition.id).items.create(documentDefinition);
  console.log('Created item with content');
}

createFruit().catch(err => {
  console.error(err);
});

點進去 Data explorer 的 Document 後可以看到剛才新增的資料如下:

document 結構

接著我們要來講一下每個欄位的意思:

_rid 由系統產生的唯一值,並且有順序性,例如上一筆插入的資料此欄位值為 `SvJDANLJWUiBhB4AAAAAAA==` 則下一筆會是 `SvJDANLJWUiChB4AAAAAAA==`

_etag 由系統產生, 用來優化 concurrency

_ts 由系統產生,當作最後一次更新的 timestamp

_self 由系統產生,代表資源的一個 URI,通常為此種格式 `/dbs/{dbName}/users/{userId}/permissions/{permissionId}`

id 可以由使用者自行設定,如果沒有設定系統會自動產生。

我們使用 Node.js SDK 時可以如下使用:

初始化連線:

const client = new CosmosClient({ endpoint: endpoint, auth: { masterKey: masterKey } });

創建資料庫:

const { database } = await client.databases.createIfNotExists({ id: databaseId });

建立 collection:

const { container } = await client.database(databaseId).containers.createIfNotExists({ id: containerId });

執行 SQL

const querySpec = {
    query: "SELECT VALUE r.children FROM root r WHERE r.lastName = @lastName",
    parameters: [
        {
            name: "@lastName",
            value: "Andersen"
        }
    ]
};

const { result: results } = await client.database(databaseId).container(containerId).items.query(querySpec).toArray();
for (var queryResult of results) {
    let resultString = JSON.stringify(queryResult);
    console.log(`\tQuery returned ${resultString}\n`);
}

Cassandra API

Cassandra DB 簡介

相信已經許多人先前已經搭配 Spark 使用過,不過因為本賽季後續會使用,所以還是簡介一下。Cassandra 本身是 2008 由 Facebook 開源的一個 NoSQL DB,轉眼間已經十年,與 IT 鐵人賽一樣。Cassandra 的資料是以 Key-Value 的方式儲存,資料模型與 Google BigTable 和 HBase 類似,但 Cassandra 在建立 cluster 中不會有 master 節點,每個節點都是平等的,這樣可以避免單一節點有問題時造成整個系統故障。

使用 Cassandra 一開始要先建立一個 KEYSPACE,同時設定 replication 的方式。

CREATE  KEYSPACE [IF NOT EXISTS] keyspace_name 
   WITH REPLICATION = { 
      'class' : 'SimpleStrategy', 'replication_factor' : N } 
     | 'class' : 'NetworkTopologyStrategy', 
       'dc1_name' : N [, ...] 
   }
   [AND DURABLE_WRITES =  true|false] ;

CAP

Azure Cosmos DB 的 Cassandra API

無痛轉移到 Azure Cosmos DB 的 Cassandra ,以後再也不用去監控 DB 機器的狀態,以及 CPU、 RAM 使用量突然飆高等問題,也不用管理一堆 yaml 等等的配置檔案以及煩惱配置跨地區 cluster 與設定網路等等,通通都交給 Azure Cosmos DB 幫你處理即可。throughput 與 latency 出問題時再也不用 Dear DevOps 然後 Dear SysOps,只要於 portal 面板按個按鈕即可解決。

下一篇將介紹如何實際使用 Azure Cosmos DB 的 Cassandra API 。

這篇文章要來實際使用一下 Azure Cosmos DB 的 Cassandra API。

set SSL_VERSION=TLSv1_2
set SSL_VALIDATE=false

cqlsh yichengcosmos.cassandra.cosmosdb.azure.com 10350 -u yichengcosmos -p <填上 PRIMARY PASSWORD> --ssl

使用官方範例

首先我們一樣先使用看看官方提供的範例:

git clone https://github.com/Azure-Samples/azure-cosmos-db-cassandra-nodejs-getting-started.git && cd  azure-cosmos-db-cassandra-nodejs-getting-started && npm install

config.contactPoint 記得在後面加上 PORT 號

產生憑證

然後把他移動到專案目錄下,並把檔案副檔名改為 cer。

mkdir ./cert && mv ~/Downloads/bc2025.crt ./cert/bc2025.cer

之後執行程式: node uprofile.js。

這時各位可能會產生 Error: error:0906D06C:PEM routines:PEM_read_bio:no start line 的錯誤,然後十分困惑。

不過不要緊,我們不要用官方的,自己來產生一張 X509 證書即可:

openssl req -newkey rsa:2048 -new -nodes -keyout key.pem -out csr.pem

輸入後填上相關資訊,也可以都按 Enter

然後從 pem 轉為 crt

openssl x509 -req -days 365 -in csr.pem -signkey key.pem -out server.crt

最後再把 server.crt 改名字為 server.cer 即可

mv server.crt server.cer

之後執行範例程式:

node uprofile.js

這時會出現 { TypeError: Cannot read property 'type' of undefined 的錯誤,因為 Azure 目前的官方範例有點問題,在 batch 批量 Insert 的時候 params 多了一個參數。

這時把最後面的日期參數移除後再次執行:

沒想到又出現如下錯誤: { ResponseError: Logged batches are not supported by the service yet. Please use unlogged batches instead.

範例:

我們接著不使用官方範例,把 uprofile.js 直接改為以下,自己來操作看看。

創建 DB(Keyspace):

const cassandra = require('cassandra-driver');
const tls = require("tls");
const fs = require("fs");

const config = require("./config");

const ssl_option = {
  cert: fs.readFileSync("./cert/server.cer"),
  secureProtocol: "TLSv1_2_method"
};

const authProviderLocalCassandra = 
    new cassandra.auth.PlainTextAuthProvider(config.username, config.password);
const client = new cassandra.Client({ 
  contactPoints: [config.contactPoint], 
  authProvider: authProviderLocalCassandra, 
  sslOptions:ssl_option
});

client.connect();

const query = "CREATE KEYSPACE IF NOT EXISTS Fruits WITH replication = {\'class\': \'NetworkTopologyStrategy\', \'datacenter\' : \'1\' }";

client.execute(query)
  .then(result => {
    console.log(result);
    client.shutdown();
  })
  .catch(err => console.log(err));

創建 TABLE

const cassandra = require("cassandra-driver");
const tls = require("tls");
const fs = require("fs");

const config = require("./config");

const ssl_option = {
  cert: fs.readFileSync("./cert/server.cer"),
  secureProtocol: "TLSv1_2_method"
};

const authProviderLocalCassandra = new cassandra.auth.PlainTextAuthProvider(
  config.username,
  config.password
);
const client = new cassandra.Client({
  contactPoints: [config.contactPoint],
  authProvider: authProviderLocalCassandra,
  sslOptions: ssl_option
});

client.connect()
  .then(function () {
    const query = "CREATE TABLE IF NOT EXISTS Fruits.banana " +
      "(_id int PRIMARY KEY, price int, name text)";
    return client.execute(query);
  })
  .catch(function (err) {
    console.error('There was an error', err);
    return client.shutdown();
  });

插入資料與查詢資料:

const cassandra = require("cassandra-driver");
const tls = require("tls");
const fs = require("fs");

const config = require("./config");

const ssl_option = {
  cert: fs.readFileSync("./cert/server.cer"),
  secureProtocol: "TLSv1_2_method"
};

const authProviderLocalCassandra = new cassandra.auth.PlainTextAuthProvider(
  config.username,
  config.password
);
const client = new cassandra.Client({
  contactPoints: [config.contactPoint],
  authProvider: authProviderLocalCassandra,
  sslOptions: ssl_option
});

client.connect()
  .then(function () {
    console.log('Inserting');
    const query = 'INSERT INTO Fruits.banana (_id, price, name) VALUES (?, ?, ?)';
    return client.execute(query, [1, 100, "Super banana"], { prepare: true});
  })
  .then(function () {
    const query = 'SELECT price, name FROM Fruits.banana WHERE _id = ?';
    return client.execute(query, [1], { prepare: true });
  })
  .then(function (result) {
    const row = result.first();
    console.log('Retrieved row: %j', row);
    return client.shutdown();
  })
  .catch(function (err) {
    console.error('There was an error', err);
    return client.shutdown();
  });

有時插入資料後按下 portal 頁面上的重新整理不會有反應,要把網頁重新整理一次才行。

那我們這篇介紹就到這邊為止,有興趣的朋友可以稍微玩一下。

我們先手動在 Data Explorer 介面上創建一個 TestDB 與 Fruits collection。

https://ithelp.ithome.com.tw/upload/images/20181102/20112426IWH0FssgSp.png

參考資料:

https://ithelp.ithome.com.tw/upload/images/20181105/20112426dYUQHlvSly.png

Cassandra 的查詢語言 CQL 與 SQL 類似,可參考官方文件:

replication 策略包含以下兩種: 1.SimpleStrategy 只有一個datacenters 和一個 rack 時使用,通常用作測試環境,會設定 replication_factor 代表同步儲存資料的節點數目。 2.NetworkTopologyStrategy 通常用在生產正式環境,設定 datacenters 的各個名稱,與分別要同步的節點數目。

相關名詞介紹:

SaaS 有 ,分散式系統有 CAP,Cassandra 屬於比較偏向 AP 的部分。

熟悉的圖:

來源:

Azure Cosmos DB 的 Cassandra API 與 可以無縫的整合,因為使用相同的 CQLv4 ( Cassandra Query Language),只需要改一下連線的字串,即可將原本連線到自行架設的 Cassandra DB 轉為連線到 Azure Cosmos DB。

如果已經安裝了 可以直接用如下方式連線:

然後到 portal 把 Connection string 記錄下來。

填入 config.js 中

因為在連線需要填入 sslOptions 所以要一張憑證,首先在此處官方提供的連結下載憑證:

最後更改一下 uprofile.js 的 cert 路徑。

https://ithelp.ithome.com.tw/upload/images/20181106/2011242670eI4XZNJV.png

logged batches 代表該 batch 執行是原子性的,而 cosmos DB 目前不支援,只能用 unlogged batched。但是 cosmos DB 通常會有多個 partition 所以如果使用 unlogged batched 為 anti-pattern 的操作,可參考: 在 Azure 解決這個範例錯誤與 batch 的問題之前我們先將它改為一般的 Insert。

回到 portal 的 Data explorer 即可看到剛才插入的資料:

https://docs.microsoft.com/en-us/azure/cosmos-db/sql-api-resources
https://cassandra.apache.org/doc/latest/cql/index.html
https://docs.datastax.com/en/cassandra/3.0/cassandra/architecture/archDataDistributeReplication.html
https://docs.datastax.com/en/cassandra/3.0/cassandra/architecture/archIntro.html
twelve-factor
http://digbigdata.com/wp-content/uploads/2013/05/media_httpfarm5static_mevIk.png
Apache Cassandra
cqlsh
https://cacert.omniroot.com/bc2025.crt
https://stackoverflow.com/a/49471102
https://ithelp.ithome.com.tw/upload/images/20181102/20112426uC3DoPg0UZ.png
https://ithelp.ithome.com.tw/upload/images/20181105/20112426Zpk7ZaDgog.png
https://ithelp.ithome.com.tw/upload/images/20181106/20112426bWuPeN3hez.png
https://ithelp.ithome.com.tw/upload/images/20181106/20112426axdF7DYoJy.png
https://ithelp.ithome.com.tw/upload/images/20181106/2011242615yKR52M8v.png
https://ithelp.ithome.com.tw/upload/images/20181106/20112426Cui9CdThg5.png