Skip to content

Commit

Permalink
* 创建索引
Browse files Browse the repository at this point in the history
  • Loading branch information
missxiaolin committed Jan 29, 2024
1 parent 0a4c3ef commit 5b095f5
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 19 deletions.
6 changes: 5 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
EXPRESS_PORT=9001
EXPRESS_PORT=9001
EXPRESS_NAME=msc

ELASTICSEARCH_ISOPEN=true
ELASTICSEARCH_URL=http://localhost:9200
3 changes: 3 additions & 0 deletions src/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ const registedCommandList = [
'./commands/dataHour/dataInit.js', // dataHour表初始化
'./commands/dataHour/index.js', // 分析数据插入
'./commands/watch_dog/alarm.js', // 监控发送
'./commands/elasticsearch/esCreateIndex.js', // es 索引创建
'./commands/elasticsearch/esIndxList.js', // 获取es索引列表
'./commands/elasticsearch/index.js', // es存储
]


Expand Down
28 changes: 28 additions & 0 deletions src/commands/elasticsearch/esCreateIndex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import Base from "../base";
import Es from "../../library/es/index"
const es = new Es();

class EsCreateIndex extends Base {
static get signature() {
return `
Es:Create:Index
{key:创建索引key}
`;
}

static get description() {
return "Elasticsearch 创建索引";
}

/**
* ES 存储方便查询更快
* @param {*} args
* @param {*} options
*/
async execute(args, options) {
let { key } = args;
console.log(es.createIndex(key))
}
}

export default EsCreateIndex
27 changes: 27 additions & 0 deletions src/commands/elasticsearch/esIndxList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import Base from "../base";
import Es from "../../library/es/index"
const es = new Es();

class EsIndexList extends Base {
static get signature() {
return `
Es:Index:List
`;
}

static get description() {
return "Elasticsearch 获取所有索引列表";
}

/**
* ES 存储方便查询更快
* @param {*} args
* @param {*} options
*/
async execute(args, options) {
const indexs = await es.indicesList()
console.log(indexs)
}
}

export default EsIndexList
49 changes: 31 additions & 18 deletions src/commands/elasticsearch/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import Base from "../base";
import Es from "../../library/es/index"
import PageModel from "../../model/page_model"


const es = new Es();
const pageModel = new PageModel();

// 安装
// docker pull docker.elastic.co/elasticsearch/elasticsearch:7.15.1
Expand All @@ -7,23 +13,30 @@ import Base from "../base";
// docker pull docker.elastic.co/kibana/kibana:7.15.1
// docker run -d --name kibana --link elasticsearch:elasticsearch -p 5601:5601 docker.elastic.co/kibana/kibana:7.15.1

class SaveLog extends Base {
static get signature() {
return `
class EsSave extends Base {
static get signature() {
return `
Es:Save
{startTime:日志扫描范围上限格式}
{endTime:日志扫描范围下限格式}
`;
}

static get description() {
return "Elasticsearch 存储";
}

/**
* ES 存储方便查询更快
* @param {*} args
* @param {*} options
*/
async execute(args, options) {

}
}
}

static get description() {
return "Elasticsearch 存储";
}

/**
* ES 存储方便查询更快
* @param {*} args
* @param {*} options
*/
async execute(args, options) {
let { startTime, endTime } = args;
console.log(startTime, endTime)
// pageModel.
// console.log(es.saveData())
}
}

export default EsSave
46 changes: 46 additions & 0 deletions src/library/es/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import dotenv from "dotenv";
import { Client } from "elasticsearch";
const appConfig = dotenv.config().parsed;

export default class Es {
constructor() {
if (!appConfig["ELASTICSEARCH_ISOPEN"]) {
return;
}
// 创建 Elasticsearch 客户端实例
const client = new Client({ node: appConfig["ELASTICSEARCH_URL"] });
this.esClient = client;
}

async saveData(data) {
return await this.esClient.bulk({
body: data,
});
}

/**
* 创建索引
* @param {*} key
*/
async createIndex(key) {
return await this.esClient.indices.create({
index: key,
});
}

/**
* 获取所有索引列表
* @returns
*/
async indicesList() {
const { body } = await this.esClient.cat.indices({ format: "json" });
if (body) {
console.log(body)
// 输出索引列表
const indices = body.map((index) => index.index);
return indices;
} else {
return {}
}
}
}

0 comments on commit 5b095f5

Please sign in to comment.