zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    关于html5的一些知识。
    常见的http状态码总结。
    踩坑记录-安装node-sass运行报错TypeError: this.getResolve is not a function at Object.loader
    踩坑记录-!!vue-style-loader!css-loader错误
    koa-passport做登录注册验证
    nuxt项目里使用vuex状态树
    node(koa、nuxt等项目)中使用import报错问题
    koa+nodemailer实现邮箱验证注册功能
    踩坑记录-nuxt引入vuex报错store/index.js should export a method that returns a Vuex instance.
    常用shell命令积累
  • 原文地址:https://www.cnblogs.com/rongfengliang/p/15068241.html
Copyright © 2011-2022 走看看