美文网首页
基于libcurl的与es进行通信(C++代码)

基于libcurl的与es进行通信(C++代码)

作者: 龟龟51 | 来源:发表于2018-08-23 19:48 被阅读0次

    Elasticsearch可以通过http报文和json语句来进行增删查改,可以通过libcurl构造语句,去发送到es集群进行操作。参考

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

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

    实现一个简便的esclient()函数,方便读写

    运行结果:

    图1是查询单条语句的结果

    图2是批量查询的结果

    图3是以上程序运行前的图

    图4是以上程序运行后的图

    附录:以上源码

    ```

        #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");        //查询单条数据

                esclient("http://172.31.100.114:9200/bank/_search","","search");        //批量查询数据,但是只返回10条,如需其他要求,加json语句

                esclient("http://172.31.100.114:9200/customer/external/3/"," { \"name\": \"hhh\" }","add");                //增加一条数据

                esclient("http://172.31.100.114:9200/customer/external/2/_updata/"," {\"doc\": { \"name\": \"eee\" }}","updata");        //更新某条数据

                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\" }}}","");        //匹配删除数据

        }

    ```

    相关文章

      网友评论

          本文标题:基于libcurl的与es进行通信(C++代码)

          本文链接:https://www.haomeiwen.com/subject/ernmiftx.html