天行健,君子以自強(qiáng)不息;地勢(shì)坤,君子以厚德載物。
每個(gè)人都有惰性,但不斷學(xué)習(xí)是好好生活的根本,共勉!
文章均為學(xué)習(xí)整理筆記,分享記錄為主,如有錯(cuò)誤請(qǐng)指正,共同學(xué)習(xí)進(jìn)步。
一、服務(wù)安裝參考
首先需要準(zhǔn)備好elasticsearch和kibana
elasticsearch的下載、安裝、使用可參考:Elasticsearch安裝
kibana的下載、安裝、使用可參考:Kibana安裝、配置
服務(wù)的啟動(dòng)使用和數(shù)據(jù)增刪改查可參考:kibana操作elasticsearch(增刪改查)
在進(jìn)行一下Java實(shí)現(xiàn)之前,先將es服務(wù)和kibana服務(wù)啟動(dòng)
二、Java實(shí)現(xiàn)新增數(shù)據(jù)到ES
Elasticsearch的服務(wù)開啟后,可以使用http請(qǐng)求進(jìn)行調(diào)用接口來操作Elasticsearch數(shù)據(jù)
請(qǐng)求的url格式如下:
http://localhost:9200/index/type/id
對(duì)于Java來說,可以使用http請(qǐng)求工具進(jìn)行實(shí)現(xiàn),同時(shí)傳參,參數(shù)為json類型數(shù)據(jù)
具體實(shí)現(xiàn)如下
1. 環(huán)境
并非要求,只是我這里使用的這個(gè)環(huán)境
JDK 1.8
Maven 3.9.4
IDEA 2023.2.1
2. 包結(jié)構(gòu)
這里主要用到三個(gè)文件:pom引入依賴,HttpClientUtils是請(qǐng)求工具,EsHttpRequestController是請(qǐng)求調(diào)用測(cè)試
3. 依賴引入
引入http工具所需要的依賴,也就是實(shí)現(xiàn)請(qǐng)求的依賴
傳入的參數(shù)為json類型所以也需要json工具的依賴
pom.xml完整內(nèi)容
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.es</groupId>
<artifactId>ES-HTTP</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.14</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
<!--json工具-->
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>2.0.33</version>
</dependency>
</dependencies>
</project>
4. http請(qǐng)求工具
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 {
// 創(chuàng)建httpclient對(duì)象
CloseableHttpClient client = HttpClients.createDefault();
// 創(chuàng)建post方式請(qǐng)求對(duì)象
HttpPost httpPost = new HttpPost(url);
// 設(shè)置參數(shù)到請(qǐng)求對(duì)象中
httpPost.setEntity(new StringEntity(str, encoding));
// 設(shè)置header信息
// 指定報(bào)文頭【Content-type】、【User-Agent】
httpPost.setHeader("Content-type", "application/json;charset=UTF-8");
// 執(zhí)行請(qǐng)求操作,并拿到結(jié)果(同步阻塞)
CloseableHttpResponse response = client.execute(httpPost);
// 獲取結(jié)果實(shí)體
HttpEntity entity = response.getEntity();
if (entity != null) {
// 按指定編碼轉(zhuǎn)換結(jié)果實(shí)體為String類型
body = EntityUtils.toString(entity, encoding);
}
EntityUtils.consume(entity);
// 釋放鏈接
response.close();
return body;
} catch (Exception e1) {
e1.printStackTrace();
return "";
}
}
}
5. 測(cè)試代碼
編寫mian方法執(zhí)行請(qǐng)求存數(shù)據(jù)到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服務(wù)
先看kibana服務(wù)查看數(shù)據(jù)
打開側(cè)邊欄,Analytics–Discover
查看現(xiàn)有數(shù)據(jù)
執(zhí)行5. 測(cè)試代碼
的代碼,然后刷新界面查看新增數(shù)據(jù)
如下,新增成功文章來源:http://www.zghlxwxcb.cn/news/detail-777924.html
感謝閱讀,祝君暴富!
文章來源地址http://www.zghlxwxcb.cn/news/detail-777924.html
到了這里,關(guān)于Java操作Elasticsearch(新增數(shù)據(jù))的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!