cube.js elasticsearch 官方sql 扩展使用

环境准备

  • es 环境
version: "3"
services: 
  es01:
    image: elasticsearch:7.3.2
    container_name: es01
   // 开启sql 以及认证
    environment:
      - "http.host=0.0.0.0"
      - "transport.host=localhost"
      - "network.host=0.0.0.0"
      - "http.cors.enabled=true"
      - "http.cors.allow-origin=*"
      - "xpack.ml.enabled=false"
      - "xpack.security.enabled=true"
      - "xpack.sql.enabled=true"
    ulimits:
      memlock:
        soft: -1
        hard: -1
    ports:
      - 9200:9200

初始化账户(进入容器)

elasticsearch-setup-passwords  auto -v
  • es 数据
    app.js
 
const { Client } = require('@elastic/elasticsearch')
const client = new Client({ node: 'http://elastic:ed824a5iPV33lrO7MOz0@localhost:9200' })
 
async function run () {
  // Let's start by indexing some data
  await client.index({
    index: 'game',
    // type: '_doc', // uncomment this line if you are using Elasticsearch ≤ 6
    body: {
      character: 'Ned Stark',
      quote: 'Winter is coming.'
    }
  })
 
  await client.index({
    index: 'game',
    // 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',
    // 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' })
 
  // Let's search!
  const { body } = await client.search({
    index: 'game',
    // 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)

cube.js 配置

.env

CUBEJS_DB_URL=http://elastic:ed824a5iPV33lrO7MOz0@localhost:9200
CUBEJS_DB_ELASTIC_QUERY_FORMAT=json
CUBEJS_DEV_MODE=true
CUBEJS_DB_TYPE=elasticsearch
CUBEJS_API_SECRET=b9a87765c28b91d570e51647c3e7cbb5a71364147bdfbef65ffa307808ef9128b98142883a541e48a8b8517d0e02d7e8344ac694c29e7b0f09a3ee0df10755d8

package.json

{
  "name": "myappdemo",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "dev": "./node_modules/.bin/cubejs-server"
  },
  "template": "docker",
  "templateVersion": "0.26.44",
  "devDependencies": {
    "@cubejs-backend/cubestore-driver": "^0.28.8",
    "@cubejs-backend/elasticsearch-driver": "^0.28.8",
    "@cubejs-backend/server": "^0.28.8"
  }
}

访问效果

 

 


 

 

说明

对于database 的处理有点问题,多了main,都是可以使用的

参考资料

https://www.npmjs.com/package/@elastic/elasticsearch
https://cube.dev/docs/reference/environment-variables#database-connection

posted on 2021-07-27 22:29  荣锋亮  阅读(188)  评论(0编辑  收藏  举报

导航