diff --git a/pom.xml b/pom.xml
index cb5947c..486221c 100644
--- a/pom.xml
+++ b/pom.xml
@@ -58,6 +58,11 @@
mybatis
3.5.3
+
+ org.elasticsearch.client
+ elasticsearch-rest-high-level-client
+ 7.3.1
+
diff --git a/src/main/java/com/github/DeeJay0921/ElasticsearchDataGenerator.java b/src/main/java/com/github/DeeJay0921/ElasticsearchDataGenerator.java
new file mode 100644
index 0000000..b58637d
--- /dev/null
+++ b/src/main/java/com/github/DeeJay0921/ElasticsearchDataGenerator.java
@@ -0,0 +1,67 @@
+package com.github.DeeJay0921;
+
+import org.apache.http.HttpHost;
+import org.apache.ibatis.io.Resources;
+import org.apache.ibatis.session.SqlSession;
+import org.apache.ibatis.session.SqlSessionFactory;
+import org.apache.ibatis.session.SqlSessionFactoryBuilder;
+import org.elasticsearch.action.index.IndexRequest;
+import org.elasticsearch.action.index.IndexResponse;
+import org.elasticsearch.client.RequestOptions;
+import org.elasticsearch.client.RestClient;
+import org.elasticsearch.client.RestHighLevelClient;
+import org.elasticsearch.common.xcontent.XContentType;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * 用于向Elasticsearch中插入数据
+ */
+public class ElasticsearchDataGenerator {
+ private SqlSessionFactory sqlSessionFactory;
+
+ public ElasticsearchDataGenerator() {
+ try {
+ String resource = "db/mybatis/mybatis-config.xml";
+ InputStream inputStream = Resources.getResourceAsStream(resource);
+ sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public static void main(String[] args) throws IOException {
+ ElasticsearchDataGenerator elasticsearchDataGenerator = new ElasticsearchDataGenerator();
+ elasticsearchDataGenerator.insertDataIntoES(elasticsearchDataGenerator.selectNewsFromDatabase());
+ }
+
+ private void insertDataIntoES(List newsFromDatabase) throws IOException {
+ try (RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")))) {
+ for (News news : newsFromDatabase) {
+ IndexRequest request = new IndexRequest("news");
+
+ Map param = new HashMap<>();
+ param.put("title", news.getTitle());
+ param.put("url", news.getUrl());
+ param.put("content", news.getContent());
+ request.source(param, XContentType.JSON);
+
+ IndexResponse response = client.index(request, RequestOptions.DEFAULT);
+ System.out.println("response = " + response.status().getStatus());
+ }
+ }
+ }
+
+ public List selectNewsFromDatabase() {
+ try {
+ SqlSession session = sqlSessionFactory.openSession();
+ return session.selectList("com.github.DeeJay0921.mock.selectNews");
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+}
diff --git a/src/main/resources/db/mybatis/mockDataMapper.xml b/src/main/resources/db/mybatis/mockDataMapper.xml
index 203e1a5..c7ac0cf 100644
--- a/src/main/resources/db/mybatis/mockDataMapper.xml
+++ b/src/main/resources/db/mybatis/mockDataMapper.xml
@@ -10,6 +10,6 @@
\ No newline at end of file