Skip to content

Commit

Permalink
给Elasticsearch添加一些数据
Browse files Browse the repository at this point in the history
引入Elasticsearch  且写了一个ElasticsearchDataGenerator类去插入一些数据
  • Loading branch information
DeeJay0921 committed Jan 14, 2020
1 parent 466296f commit 817e20c
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
5 changes: 5 additions & 0 deletions pom.xml
Expand Up @@ -58,6 +58,11 @@
<artifactId>mybatis</artifactId>
<version>3.5.3</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.3.1</version>
</dependency>

</dependencies>

Expand Down
@@ -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<News> 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<String, Object> 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<News> selectNewsFromDatabase() {
try {
SqlSession session = sqlSessionFactory.openSession();
return session.selectList("com.github.DeeJay0921.mock.selectNews");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
2 changes: 1 addition & 1 deletion src/main/resources/db/mybatis/mockDataMapper.xml
Expand Up @@ -10,6 +10,6 @@

<select id="selectNews" resultType="com.github.DeeJay0921.News">
select ID, TITLE, CONTENT, URL, CREATED_AT, UPDATED_AT
from NEWS
from NEWS LIMIT 4000
</select>
</mapper>

0 comments on commit 817e20c

Please sign in to comment.