基于libcurl的與es進(jìn)行通信(C++代碼)

Elasticsearch可以通過(guò)http報(bào)文和json語(yǔ)句來(lái)進(jìn)行增刪查改,可以通過(guò)libcurl構(gòu)造語(yǔ)句,去發(fā)送到es集群進(jìn)行操作。參考

https://blog.csdn.net/xsdxs/article/details/72849796

https://stackoverflow.com/questions/25887453/how-to-use-libcurl-in-c-to-send-post-to-elasticsearch

實(shí)現(xiàn)一個(gè)簡(jiǎn)便的esclient()函數(shù),方便讀寫

運(yùn)行結(jié)果:

圖1是查詢單條語(yǔ)句的結(jié)果

圖2是批量查詢的結(jié)果

圖3是以上程序運(yùn)行前的圖

圖4是以上程序運(yùn)行后的圖


附錄:以上源碼

```

????#include

????#include

????#include

????#include

????#include

????using namespace std;

????size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)

????{

? ????? strncat((char *)stream, (char *)ptr, size*nmemb);

? ? ????return size * nmemb;

????}

????int esclient(const string &_http, const string &_json, const string &_command)

????{

????????????CURL *curl;

????????????CURLcode res;

????????????struct curl_slist* headers = NULL;????????

????????????curl = curl_easy_init();

????????????curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);

????????????curl_easy_setopt(curl, CURLOPT_URL, _http.c_str());


????????????headers=curl_slist_append(headers, "Content-Type:application/json");

????????????curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);

????????????char out[40960]={0};????????

????????????curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);

????????????curl_easy_setopt(curl, CURLOPT_WRITEDATA, &out);


????????????if(_command=="delete")

????????????{

????????????????????curl_easy_setopt(curl,? CURLOPT_CUSTOMREQUEST, "DELETE");

????????????}

????????????else if(_command=="get")

????????????{????????????????

????????????????????curl_easy_setopt(curl,? CURLOPT_CUSTOMREQUEST, "GET");????????????????

????????????}

????????????else

????????????{

????????????????????curl_easy_setopt(curl,? CURLOPT_CUSTOMREQUEST, "POST");

????????????}


????????????if(!_json.empty())

????????????{

????????????????????curl_easy_setopt(curl,CURLOPT_POSTFIELDS,_json.c_str());

????????????????????curl_easy_setopt(curl,CURLOPT_POSTFIELDSIZE,_json.length());

????????????}


????????????res = curl_easy_perform(curl);

???????????printf("%s\n",out);

????????????curl_easy_cleanup(curl);

????????????return res;

????}

????int main(void)

????{

????????????esclient("http://172.31.100.114:9200/customer/external/1","","get");????????//查詢單條數(shù)據(jù)

????????????esclient("http://172.31.100.114:9200/bank/_search","","search");????????//批量查詢數(shù)據(jù),但是只返回10條,如需其他要求,加json語(yǔ)句

????????????esclient("http://172.31.100.114:9200/customer/external/3/"," { \"name\": \"hhh\" }","add");????????????????//增加一條數(shù)據(jù)

????????????esclient("http://172.31.100.114:9200/customer/external/2/_updata/"," {\"doc\": { \"name\": \"eee\" }}","updata");????????//更新某條數(shù)據(jù)

????????????esclient("http://172.31.100.114:9200/ccc","","delete");????????????????//刪除索引

????????????esclient("http://172.31.100.114:9200/customer/external/_delete_by_query"," {\"query\":{ \"match\":{\"name\": \"hhh\" }}}","");????????//匹配刪除數(shù)據(jù)


????}

```

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容