当前位置: 首页 > news >正文

C语言实现Elasticsearch增删改查API

 还需要CJSON库 自行查找下载

/** @Author: SingleBiu* @Date: 2025-05-06 20:31:04* @LastEditors: SingleBiu* @LastEditTime: 2025-05-07 00:48:26* @Description: 使用json作为输入的Elasticsearch的C语言API* 编译使用 gcc es_main.c cJSON.c -o main*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "cJSON.h"// 读取文件的函数
char* read_file(const char* filename) {FILE* file = fopen(filename, "r");if (file == NULL) {perror("Failed to open file");return NULL;}fseek(file, 0, SEEK_END);long length = ftell(file);fseek(file, 0, SEEK_SET);char* data = (char*)malloc(length + 1);if (data == NULL) {perror("Failed to allocate memory");fclose(file);return NULL;}fread(data, 1, length, file);data[length] = '\0'; // 确保字符串以null结尾fclose(file);return data;
}int main(int argc ,char *argvp[])
{// 文件指针FILE *fp = NULL;// CJSON对象初始化cJSON *cjson_obj = cJSON_CreateObject();// IDchar char_arr_id[16];// namechar char_arr_name[24];// json的指针char *jp = NULL;//获取输入printf("Please intput ID(max length 13):\n");scanf("%s",&char_arr_id);printf("Please input name(max length 23):\n");scanf("%s",&char_arr_name);// 向CJSON中添加对象cJSON_AddItemToObject(cjson_obj,"ID",cJSON_CreateString(char_arr_id));cJSON_AddItemToObject(cjson_obj,"name",cJSON_CreateString(char_arr_name));// 打印CJSON对象jp = cJSON_Print(cjson_obj);printf("Json data:\n%s\n",jp);system("pause");// 创建并打开文件fp = fopen("./insert.json","w+");// 写入文件fprintf(fp,jp);// 关闭文件fclose(fp);// 释放内存free(jp);cJSON_Delete(cjson_obj);// 测试Elasticsearch // (需要将elasticsearch-9.0.0/config/elasticsearch.yml中的xpack.security.enabled设置成false)system("curl -X GET \"http://127.0.0.1:9200?pretty\"");system("pause");//删除索引system("curl -X DELETE \"http://127.0.0.1:9200/index?pretty\" -H Content-Type:application/json");system("pause");//创建索引system("curl -X PUT \"http://127.0.0.1:9200/index?pretty\" -H Content-Type:application/json -d @index.json");system("pause");// 添加id为1的数据system("curl -X POST \"http://127.0.0.1:9200/index/_doc/1?pretty\" -H Content-Type:application/json -d @insert.json");system("pause");// 添加数据 文档ID自动生成system("curl -X POST \"http://127.0.0.1:9200/index/_doc?pretty\" -H Content-Type:application/json -d @insert2.json");system("pause");// 查询所有数据system("curl -X POST \"http://127.0.0.1:9200/index/_search?pretty\" -H Content-Type:application/json -d @query_all.json");system("pause");// 按字段查询数据system("curl -X POST \"http://127.0.0.1:9200/index/_search?pretty\" -H Content-Type:application/json -d @query.json");system("pause");// 查询数据 另一种写法system("curl -X POST \"http://127.0.0.1:9200/index/_search?q=A1\" -H Content-Type:application/json");system("pause");// 修改数据 根据update.json的内容将id为1的文档的内容ID修改为B2system("curl -X POST \"http://127.0.0.1:9200/index/_update/1?pretty\" -H Content-Type:application/json -d @update.json");system("pause");// DEBUG 获取所有结果 发现id为1的内容已经修改system("curl -X POST \"http://127.0.0.1:9200/index/_search?pretty\" -H Content-Type:application/json -d @query_all.json");system("pause");// 删除数据 (删除了name为Fox3的数据)system("curl -X POST \"http://127.0.0.1:9200/index/_delete_by_query?pretty\" -H Content-Type:application/json -d @delete.json");system("pause");// DEBUG 获取所有结果 发现name为Fox3的数据不存在了system("curl -X POST \"http://127.0.0.1:9200/index/_search?pretty\" -H Content-Type:application/json -d @query_all.json");system("pause");// 查询数据 将内容存入文件system("curl -X POST \"http://127.0.0.1:9200/index/_search?pretty\" -H Content-Type:application/json -d @query2.json > result1_query.json");system("pause");const char* filename = "result1_query.json";char* json_data = read_file(filename);if (json_data == NULL) {printf("Error read json file\n");system("pause");return 1;}// 解析JSON字符串cJSON* root = cJSON_Parse(json_data);if (root == NULL) {const char* error_ptr = cJSON_GetErrorPtr();if (error_ptr != NULL) {fprintf(stderr, "Error before: %s\n", error_ptr);}free(json_data);printf("Error parse json file\n");system("pause");return 1;}// 提取hits数组cJSON* hits = cJSON_GetObjectItem(root, "hits");cJSON* hits2 = cJSON_GetObjectItem(hits,"hits");if (cJSON_IsArray(hits2) && cJSON_GetArraySize(hits2) > 0) {// 提取hits数组中的第一个元素cJSON* hit = cJSON_GetArrayItem(hits2, 0);if (hit != NULL) {// 提取_source对象cJSON* _source = cJSON_GetObjectItem(hit, "_source");if (cJSON_IsObject(_source)) {// 提取name字段cJSON* name = cJSON_GetObjectItem(_source, "name");if (name != NULL && cJSON_IsString(name)) {printf("Name: %s\n", name->valuestring);} else {printf("Name field not found or not a string.\n");}} else {printf("_source field is not an object.\n");}} else {printf("No hits found in the response.\n");}} else {printf("Hits field is not an array or is empty.\n");}// 释放内存cJSON_Delete(root);free(json_data);system("pause");return 0;
}

http://www.lryc.cn/news/610220.html

相关文章:

  • 部署 Kibana 8.2.2 可视化管理 Elasticsearch 8.2.2 集群
  • 解决 PS暂存盘已满的五种方法
  • PSOFT Pencil+ 4.25 插件安装教程(适用于 3ds Max 2022-2025)
  • 【c51单片机利用p2口,外接八个流水灯实现流水灯效果1.3.5.7.2.4.6.8亮】2022-10-9
  • MinIO 服务日志与监控实战:日志配置、Webhook、事件通知、Prometheus+Grafana 可视化全流程指南
  • AI 编程学习网站分享:vibe-coding-tutorial
  • SpringCloud相关知识
  • 【测试】⾃动化测试常⽤函数
  • 银河麒麟V10一键安装DM8的脚本及高阶运维SQL分享
  • 力扣-994.腐烂的橘子
  • RHCA02
  • 飞算JavaAI编程插件:以AI之力赋能Java开发,让编码效率再升级
  • 0基礎網站開發技術教學(三) --(後端PHP篇)-- [內有2025最新可用 phpstudy2018下載鏈接]
  • ShowDoc与Docmost对比分析:开源文档管理工具的选择指南
  • numpy基础知识2
  • 《P1462 通往奥格瑞玛的道路》
  • 图的存储方式-邻接表
  • 超急评估:用提前计算分摊性能成本
  • C + +
  • 机器学习(12):拉索回归Lasso
  • Linux环境下(Ubuntu)Fortran语言如何安装配置NetCDF
  • Integer Types Range and varieties
  • QT:交叉编译mysql驱动库
  • MySQL进阶:(第八篇)深入解析InnoDB存储架构
  • 如何手动打包 Linux(麒麟系统)的 Qt 程序
  • Linux 系统启动原理
  • 通用代码自用
  • [硬件电路-156]:什么是电信号? 电信号的本质:电信号是随时间变化的电压或电流。本质是电子运动表征信息,兼具能量传输与信息编码传递功能。
  • 开源网页生态掘金:从Bootstrap二次开发到行业专属组件库的技术变现
  • 多线程(一)