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   荣锋亮  阅读(188)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2020-07-27 基于openresty进行 query string 重写
2019-07-27 telegraf 学习一 基本安装
2018-07-27 swagger api 转graphql npm 包试用
2017-07-27 piwik docker 安装
2015-07-27 jQuery的图像裁剪插件Jcrop
2015-07-27 jquery-qrcode生成二维码

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示