Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
引入Elasticsearch 且写了一个ElasticsearchDataGenerator类去插入一些数据
- Loading branch information
1 parent
466296f
commit 817e20c
Showing
3 changed files
with
73 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/main/java/com/github/DeeJay0921/ElasticsearchDataGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters