【问题标题】:Ballerina integrator and ElasticsearchBallerina 集成器和 Elasticsearch
【发布时间】:2020-07-16 20:40:24
【问题描述】:
我正在完成我的学士学位项目,我必须使用 Ballerina 集成器中间件。
现在我需要创建一项服务,该服务能够将传入 ballerina 的数据索引到 Elasticsearch 中。有没有一种方法可以仅使用 ballerina 与 Elasticsearch 进行通信(不使用 Log stash 或 File beat..),就像我们在通信一样到 SQL 数据库?
【问题讨论】:
标签:
elasticsearch
indexing
ballerina
【解决方案1】:
如果有人在寻找相同的东西,我刚刚找到了一种与 Elastic 交流的方法,并且效果很好
---这里是代码---
import ballerina/http;
import ballerina/log;
listener http:Listener httpListener = new(9090);
http:Client elasticDB = new("http://localhost:9200/");
@http:ServiceConfig{
basePath: "/elastic"
}
service GetElasticSource on httpListener{
@http:ResourceConfig {
methods: ["GET"],
path: "/{index}/_source/{id}"
}
resource function retrieveElasticIndexById(http:Caller httpCaller, http:Request httpRequest, string index, string id){
http:Response resp = new;
var data = elasticDB->get(<@untained> ("/"+index+"/_source/"+id));
if(data is http:Response){
resp = data;
}
var respRet = httpCaller->respond(resp);
if(respRet is error){
log:printError("error responding to the client", err = respRet);
}
}
}