ElasticSearch Java API
官方地址:
https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-docs.html
通過JAVA api實現對ES的增刪改查.
準備資料用時:1秒
單條索引:1381秒
單條更新:1624秒
單條刪除:1393秒
批量索引:4秒
ID查詢:8秒
bulkProcessor處理:10秒
執行環境是開發機.
不過可以看到,單條處理每秒也就是10-20條左右.
而在批量處理的時候,甚至可以達到每秒索引5000條.
所以批量處理幾乎是ES的不二使用方法.
https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-docs.html
通過JAVA api實現對ES的增刪改查.
- import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
- import java.io.IOException;
- import java.net.InetAddress;
- import java.net.UnknownHostException;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.ResultSetMetaData;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import org.elasticsearch.action.bulk.BackoffPolicy;
- import org.elasticsearch.action.bulk.BulkProcessor;
- import org.elasticsearch.action.bulk.BulkRequest;
- import org.elasticsearch.action.bulk.BulkRequestBuilder;
- import org.elasticsearch.action.bulk.BulkResponse;
- import org.elasticsearch.action.get.GetResponse;
- import org.elasticsearch.action.update.UpdateRequest;
- import org.elasticsearch.client.Client;
- import org.elasticsearch.client.transport.TransportClient;
- import org.elasticsearch.common.settings.Settings;
- import org.elasticsearch.common.transport.InetSocketTransportAddress;
- import org.elasticsearch.common.unit.ByteSizeUnit;
- import org.elasticsearch.common.unit.ByteSizeValue;
- import org.elasticsearch.common.unit.TimeValue;
- public class Test {
- public static void main(String[] args)
- throws ClassNotFoundException, SQLException, IOException, InterruptedException {
- // 準備資料
- long start = System.currentTimeMillis();
- List<Map<String, ?>> data = prepareData();
- long end = System.currentTimeMillis();
- System.out.printf("準備資料用時:%s秒\n", (end - start) / 1000);
- start = System.currentTimeMillis();
- index(data);
- end = System.currentTimeMillis();
- System.out.printf("單條索引:%s秒\n", (end - start) / 1000);
- start = System.currentTimeMillis();
- update(data);
- end = System.currentTimeMillis();
- System.out.printf("單條更新:%s秒\n", (end - start) / 1000);
- start = System.currentTimeMillis();
- delete(data);
- end = System.currentTimeMillis();
- System.out.printf("單條刪除:%s秒\n", (end - start) / 1000);
- start = System.currentTimeMillis();
- bulk(data);
- end = System.currentTimeMillis();
- System.out.printf("批量索引:%s秒\n", (end - start) / 1000);
- start = System.currentTimeMillis();
- get(data);
- end = System.currentTimeMillis();
- System.out.printf("ID查詢:%s秒\n", (end - start) / 1000);
- start = System.currentTimeMillis();
- bulkProcessor(data);
- end = System.currentTimeMillis();
- System.out.printf("bulkProcessor處理:%s秒\n", (end - start) / 1000);
- }
- private static List<Map<String, ?>> prepareData()
- throws ClassNotFoundException, SQLException, UnknownHostException, InterruptedException {
- List<Map<String, ?>> data = new ArrayList<Map<String, ?>>();
- Class.forName("com.mysql.jdbc.Driver");
- Connection con = DriverManager.getConnection("jdbc:mysql://172.16.1.25/songod", "root", "123456");
- ResultSet rs = con.createStatement().executeQuery("select * from sod_song_ksc limit 20000");
- ResultSetMetaData metadata = rs.getMetaData();
- int columnCount = metadata.getColumnCount();
- while (rs.next()) {
- Map<String, Object> object = new HashMap<String, Object>();
- for (int i = 1; i <= columnCount; i++) {
- object.put(metadata.getColumnName(i), rs.getObject(i));
- }
- data.add(object);
- }
- rs.close();
- con.close();
- return data;
- }
- private static void index(List<Map<String, ?>> data) throws UnknownHostException {
- Client client = getClient();
- for (Map<String, ?> map : data) {
- client.prepareIndex("songod", "sod_song_ksc").setSource(map).setId(map.get("SongID").toString())
- .setRouting(map.get("SongID").toString()).execute().actionGet();
- }
- client.close();
- }
- private static void bulk(List<Map<String, ?>> data) throws UnknownHostException {
- Client client = getClient();
- BulkRequestBuilder bulkRequest = client.prepareBulk();
- for (Map<String, ?> map : data) {
- bulkRequest.add(client.prepareIndex("songod", "sod_song_ksc").setSource(map)
- .setRouting(map.get("SongID").toString()).setId(map.get("SongID").toString()));
- }
- BulkResponse bulkResponse = bulkRequest.get();
- client.close();
- }
- private static void get(List<Map<String, ?>> data) throws UnknownHostException {
- Client client = getClient();
- for (Map<String, ?> map : data) {
- GetResponse response = client.prepareGet("songod", "sod_song_ksc", map.get("SongID").toString())
- .setRouting(map.get("SongID").toString()).get();
- String str = response.getSourceAsString();
- // System.out.println(str);
- }
- client.close();
- }
- private static void delete(List<Map<String, ?>> data) throws UnknownHostException {
- Client client = getClient();
- for (Map<String, ?> map : data) {
- client.prepareDelete("songod", "sod_song_ksc", map.get("SongID").toString())
- .setRouting(map.get("SongID").toString()).get();
- }
- client.close();
- }
- private static void update(List<Map<String, ?>> data) throws IOException {
- Client client = getClient();
- for (Map<String, ?> map : data) {
- client.prepareUpdate().setIndex("songod").setType("sod_song_ksc")
- .setId(map.get("SongID").toString()).setDoc(jsonBuilder().startObject()
- .field("Name", "Hello World!!" + map.get("Name").toString()).endObject())
- .setRouting(map.get("SongID").toString()).get();
- }
- client.close();
- }
- private static void bulkProcessor(List<Map<String, ?>> data) throws IOException {
- BulkProcessor bulkProcessor = BulkProcessor.builder(getClient(), new BulkProcessor.Listener() {
- public void afterBulk(long arg0, BulkRequest arg1, BulkResponse arg2) {
- // TODO Auto-generated method stub
- }
- public void afterBulk(long arg0, BulkRequest arg1, Throwable arg2) {
- // TODO Auto-generated method stub
- }
- public void beforeBulk(long arg0, BulkRequest arg1) {
- // TODO Auto-generated method stub
- }
- }).setBulkActions(1000).setBulkSize(new ByteSizeValue(3, ByteSizeUnit.MB))
- .setFlushInterval(TimeValue.timeValueSeconds(5)).setConcurrentRequests(1)
- .setBackoffPolicy(BackoffPolicy.exponentialBackoff(TimeValue.timeValueMillis(100), 3)).build();
- // .setRouting(map.get("SongID").toString())
- for (Map<String, ?> map : data) {
- UpdateRequest updateRequest = new UpdateRequest("songod", "sod_song_ksc", map.get("SongID").toString());
- updateRequest.routing(map.get("SongID").toString());
- updateRequest.id(map.get("SongID").toString());
- updateRequest.doc(jsonBuilder().startObject().field("Name", "Hello World!!" + map.get("Name").toString())
- .endObject());
- bulkProcessor.add(updateRequest);
- }
- bulkProcessor.close();
- }
- private static Client getClient() throws UnknownHostException {
- Settings settings = Settings.settingsBuilder().put("cluster.name", "es_cluster").build();
- Client client = TransportClient.builder().settings(settings).build()
- .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.168.16.114"), 9300));
- return client;
- }
- }
準備資料用時:1秒
單條索引:1381秒
單條更新:1624秒
單條刪除:1393秒
批量索引:4秒
ID查詢:8秒
bulkProcessor處理:10秒
執行環境是開發機.
不過可以看到,單條處理每秒也就是10-20條左右.
而在批量處理的時候,甚至可以達到每秒索引5000條.
所以批量處理幾乎是ES的不二使用方法.
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29254281/viewspace-2103753/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- ElasticSearch—— Java APIElasticsearchJavaAPI
- ElasticSearch Java API使用ElasticsearchJavaAPI
- 使用 Java API 操作 elasticsearchJavaAPIElasticsearch
- ElasticSearch安裝及java Api使用ElasticsearchJavaAPI
- Elasticsearch Java High Level REST Client(Exists API)ElasticsearchJavaRESTclientAPI
- Elasticsearch Java High Level REST Client(Delete API)ElasticsearchJavaRESTclientdeleteAPI
- Elasticsearch Search APIElasticsearchAPI
- 在JAVA中將Elasticsearch索引載入到Lucene APIJavaElasticsearch索引API
- Elasticsearch 入門實戰(9)--Java API Client 使用二ElasticsearchJavaAPIclient
- elasticsearch(八)---search apiElasticsearchAPI
- Elasticsearch(二)——Rest APIElasticsearchRESTAPI
- ElasticSearch之基本用法APIElasticsearchAPI
- Elasticsearch cat api的用法ElasticsearchAPI
- Elasticsearch 入門實戰(8)--REST API 使用二(Search API)ElasticsearchRESTAPI
- elasticsearch常用請求介面Rest API示例ElasticsearchRESTAPI
- elasticsearch API使用方法備忘(Python)ElasticsearchAPIPython
- 【elasticsearch】bulk api奇特的json格式的原因ElasticsearchAPIJSON
- .Net Api 之如何使用Elasticsearch儲存文件APIElasticsearch
- Elasticsearch Java REST Client(目錄)ElasticsearchJavaRESTclient
- Docker 教程十三 JAVA 操作elasticsearchDockerJavaElasticsearch
- 實踐005-elasticsearch的Search API概覽ElasticsearchAPI
- Java操作Elasticsearch 之 [Java High Level REST Clientedit]JavaElasticsearchRESTclient
- Java 反射 APIJava反射API
- Java -fastjson apiJavaASTJSONAPI
- java常用APIJavaAPI
- Java 日期 APIJavaAPI
- ElasticSearch7.3學習(二十九)----聚合實戰之使用Java api實現電視案例ElasticsearchJavaAPI
- elasticsearch 6.x 與elasticsearch 7.x 配置與使用(Java)ElasticsearchJava
- ElasticSearch 7.6.x 版本 2020最新版 JavaRest apiElasticsearchJavaRESTAPI
- elasticsearch之Java呼叫原生程式碼ElasticsearchJava
- Java012-APIJavaAPI
- ZooKeeper 使用 Java APIJavaAPI
- java的runtime APIJavaAPI
- Java API操作ESJavaAPI
- EasyExcel Java API 使用ExcelJavaAPI
- Java之常用APIJavaAPI
- Zookeeper--Java APIJavaAPI
- 《ElasticSearch6.x實戰教程》之簡單的APIElasticsearchAPI
- ElasticSearch7.3學習(三十)----ES7.X SQL新特性解析及使用Java api實現sql功能ElasticsearchSQLJavaAPI