一个静态网站怎么做,小微企业所得税5%优惠政策,社保官方网站登录入口,一家只做t恤的网站天行健#xff0c;君子以自强不息#xff1b;地势坤#xff0c;君子以厚德载物。 每个人都有惰性#xff0c;但不断学习是好好生活的根本#xff0c;共勉#xff01; 文章均为学习整理笔记#xff0c;分享记录为主#xff0c;如有错误请指正#xff0c;共同学习进步。… 天行健君子以自强不息地势坤君子以厚德载物。 每个人都有惰性但不断学习是好好生活的根本共勉 文章均为学习整理笔记分享记录为主如有错误请指正共同学习进步。 文章目录 一、服务安装参考二、Java实现新增数据到ES1. 环境2. 包结构3. 依赖引入4. http请求工具5. 测试代码6. 访问kibana服务 一、服务安装参考
首先需要准备好elasticsearch和kibana elasticsearch的下载、安装、使用可参考Elasticsearch安装 kibana的下载、安装、使用可参考Kibana安装、配置 服务的启动使用和数据增删改查可参考kibana操作elasticsearch增删改查 在进行一下Java实现之前先将es服务和kibana服务启动
二、Java实现新增数据到ES
Elasticsearch的服务开启后可以使用http请求进行调用接口来操作Elasticsearch数据 请求的url格式如下
http://localhost:9200/index/type/id对于Java来说可以使用http请求工具进行实现同时传参参数为json类型数据 具体实现如下
1. 环境
并非要求只是我这里使用的这个环境 JDK 1.8 Maven 3.9.4 IDEA 2023.2.1
2. 包结构
这里主要用到三个文件pom引入依赖HttpClientUtils是请求工具EsHttpRequestController是请求调用测试
3. 依赖引入
引入http工具所需要的依赖也就是实现请求的依赖 传入的参数为json类型所以也需要json工具的依赖 pom.xml完整内容
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdcom.es/groupIdartifactIdES-HTTP/artifactIdversion1.0-SNAPSHOT/versionpropertiesmaven.compiler.source8/maven.compiler.sourcemaven.compiler.target8/maven.compiler.targetproject.build.sourceEncodingUTF-8/project.build.sourceEncoding/propertiesdependencies!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore --dependencygroupIdorg.apache.httpcomponents/groupIdartifactIdhttpcore/artifactIdversion4.4.14/version/dependencydependencygroupIdorg.apache.httpcomponents/groupIdartifactIdhttpclient/artifactIdversion4.5.13/version/dependency!--json工具--dependencygroupIdcom.alibaba.fastjson2/groupIdartifactIdfastjson2/artifactIdversion2.0.33/version/dependency/dependencies/project4. http请求工具
HttpClientUtils.java
package com.es.utils;import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;/*** ClassDescription:* JdkVersion: 1.8* Author: 李白* Created: 2023/10/16 16:12*/
public class HttpClientUtils {public static void post(){}public static String doPost(String url, String str, String encoding) {String body ;try {// 创建httpclient对象CloseableHttpClient client HttpClients.createDefault();// 创建post方式请求对象HttpPost httpPost new HttpPost(url);// 设置参数到请求对象中httpPost.setEntity(new StringEntity(str, encoding));// 设置header信息// 指定报文头【Content-type】、【User-Agent】httpPost.setHeader(Content-type, application/json;charsetUTF-8);// 执行请求操作并拿到结果同步阻塞CloseableHttpResponse response client.execute(httpPost);// 获取结果实体HttpEntity entity response.getEntity();if (entity ! null) {// 按指定编码转换结果实体为String类型body EntityUtils.toString(entity, encoding);}EntityUtils.consume(entity);// 释放链接response.close();return body;} catch (Exception e1) {e1.printStackTrace();return ;}}}
5. 测试代码
编写mian方法执行请求存数据到es EsHttoRequestController.java
package com.es.test;import com.alibaba.fastjson2.JSONObject;
import com.es.utils.HttpClientUtils;/*** ClassDescription:* JdkVersion: 1.8* Author: 李白* Created: 2023/10/16 16:12*/
public class EsHttpRequestController {public static void main(String[] args) {JSONObject js new JSONObject();js.put(name,杜甫);js.put(age,6800);js.put(gender,男);String jsonStr js.toJSONString();HttpClientUtils.doPost(http://127.0.0.1:9200/deviceinfo/users/1002,jsonStr,UTF-8);}}
6. 访问kibana服务
先看kibana服务查看数据 打开侧边栏Analytics–Discover 查看现有数据 执行5. 测试代码的代码然后刷新界面查看新增数据 如下新增成功 感谢阅读祝君暴富