前邊有說明,cube.js 已經es 以下是通過opendistro的一個簡單的試用(沒跑通,后邊有時間可以嘗試下找下原因)
opendistro 環境准備
- docker-compose 文件
注意es 版本,目前cube.js 是7.6 的es npm 包,所以最好是兼容的,因為部分版本還是有兼容問題的
version: "3"
services:
elasticsearch:
image: amazon/opendistro-for-elasticsearch:1.4.0
ports:
- "9200:9200"
environment:
- "discovery.type=single-node"
- "http.host=0.0.0.0"
- "opendistro_security.ssl.http.enabled=false"
- "cluster.name=odfe-cluster"
- "transport.host=0.0.0.0"
- "network.host=0.0.0.0"
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
- 啟動
docker-compose up -d
- 測試數據
使用了官方的demo,注意官方的索引命名有問題,不符合cube 的約定,會有bug
初始化es 項目
yarn init -y
yarn add @elastic/elasticsearch@7.6
代碼
'use strict'
const { Client } = require('@elastic/elasticsearch')
const client = new Client({ node: 'http://admin:admin@localhost:9200' })
async function run () {
// Let's start by indexing some data
await client.index({
index: 'game_of_thrones',
// type: '_doc', // uncomment this line if you are using Elasticsearch ≤ 6
body: {
character: 'Ned Stark',
quote: 'Winter is coming.'
}
})
await client.index({
index: 'game_of_thrones',
// type: '_doc', // uncomment this line if you are using Elasticsearch ≤ 6
body: {
character: 'Daenerys Targaryen',
quote: 'I am the blood of the dragon.'
}
})
await client.index({
index: 'game_of_thrones',
// type: '_doc', // uncomment this line if you are using Elasticsearch ≤ 6
body: {
character: 'Tyrion Lannister',
quote: 'A mind needs books like a sword needs a whetstone.'
}
})
// here we are forcing an index refresh, otherwise we will not
// get any result in the consequent search
await client.indices.refresh({ index: 'game_of_thrones' })
// Let's search!
const { body } = await client.search({
index: 'game_of_thrones',
// type: '_doc', // uncomment this line if you are using Elasticsearch ≤ 6
body: {
query: {
match: { quote: 'winter' }
}
}
})
console.log(body.hits.hits)
}
run().catch(console.log)
package.json
{
"name": "elasticsearch-nodejs",
"version": "1.0.0",
"main": "app.js",
"license": "MIT",
"dependencies": {
"@elastic/elasticsearch": "7.6"
},
"scripts": {
"start":"node app.js"
}
}
運行
yarn start
效果
cube.js 集成es
- 創建項目
注意-d 為odelasticsearch 實際上應該是opendistro 的簡稱
cubejs create es -d odelasticsearch
- 配置環境變量
.env 文件
CUBEJS_DB_URL=http://admin:admin@localhost:9200
CUBEJS_WEB_SOCKETS=true
CUBEJS_DB_TYPE=odelasticsearch
CUBEJS_API_SECRET=08ad6563349e97296917798f5206cae5f865639114412149d2d9b8f318bb14c2d4eb4c6ce97d3b4a8a7e82abee161daaf2b1ca98003433600ca5178a6fb94b31
- 啟動
yarn dev
- 效果
之后需要生成schema,方便測試
bug 說明
當前對於opendistro elasticsearch bug 還是比較多的,比如map undefined
以及生成的schema ,默認帶了main 是有問題的
當然在查看官方文檔的時候也看到了最近的一個變動,還是需要好好研究下cube.js 的內部機制,已經api gateway 的原理
還是比較期待官方對於opendistro elasticsearch 比較完備的支持的。
參考資料
https://opendistro.github.io/for-elasticsearch-docs/version-history/
https://github.com/elastic/elasticsearch-js
https://www.npmjs.com/package/@elastic/elasticsearch
https://github.com/cube-js/cube.js/blob/master/packages/cubejs-elasticsearch-driver/driver/ElasticSearchDriver.js