{
CURL *curl
CURLcode res
res_buf = ""
curl = curl_easy_init() //初始化
if(curl&&strUrl)
{
curl_easy_setopt(curl,CURLOPT_URL,strUrl) //设置url地址
if(szPost)
{
curl_easy_setopt(curl,CURLOPT_POSTFIELDS,szPost) //设置post参数
}
curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,write_func) //设置回调函数
curl_easy_setopt(curl,CURLOPT_WRITEDATA,&res_buf) //设置写数据
if(NULL == pCaPath)
{
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false)//设定为不验证证书和HOST
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false)
}
else
{
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true)
curl_easy_setopt(curl, CURLOPT_CAINFO, pCaPath)
}
res = curl_easy_perform(curl) //执行
if(res == CURLE_OK)
{
if(m_json)
{
delete m_json
m_json = NULL
}
m_json = new char[strlen(res_buf.c_str())+1]
strcpy(m_json,Utf8toAnsi(res_buf.c_str()))
curl_easy_cleanup(curl)
return 1
}
return -1
}
return -1
}
需要https站点开启了验证客户端证书,而且客户端证书是https需要的。将客户证书放在bundle里,之后用libcurl的setopt设置cert的路径,而且还可以设置服务器验证的一些属性,https证书签发CA沃通wosign网站有相关资料。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\" }}}","") //匹配删除数据
}
```
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)