ELK研究(一):elasticsearch java api介面操作ES叢集 ---TransportClient的使用介紹 bulk批量提交資料
Java client操作ES: 1:配置叢集物件資訊;2:建立客戶端;3:檢視叢集資訊
1、設定叢集名字
預設叢集名為elasticsearch,如果叢集名稱和指定的不一致則在使用節點資源時會報錯。
Settings settings = Settings.builder()
.put("cluster.name", "myClusterName").build();
TransportClient client = new PreBuiltTransportClient(settings);
//Add transport addresses and do something with the client...
2、嗅探功能
通過client.transport.sniff啟動嗅探功能,這樣只需要指定叢集中的某一個節點(不一定是主節點),然後會載入叢集中的其他節點,這樣只要程式不停即使此節點當機仍然可以連線到其他節點。
Settings settings = Settings.builder()
.put("client.transport.sniff", true).build();
TransportClient client = new PreBuiltTransportClient(settings);
3、建立client
// on startup
TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
.addTransportAddress(new TransportAddress(InetAddress.getByName("host1"), 9300))
.addTransportAddress(new TransportAddress(InetAddress.getByName("host2"), 9300));
// on shutdown
client.close();
4、完整示例–elasticsearch java api bulk批量提交資料
json資料儲存在文中,程式碼是通過讀檔案的方式:
{“10190”:0,”10071”:0,”10191”:0,”10070”:0,”48”:”122186”,”type”:”122186”,”10018”:0,”10117”:0,”332”:0,”333”:0,”999”:3202,”10178”:10131,”8503”:0,”8504”:0,”10135”:”SUSP”,”31”:0,”10116”:0,”35”:”UA3202”,”10185”:0,”10184”:0,”140”:100000,”387”:0,”8538”:”P010”,”10044”:0,”10187”:0,”10043”:0,”10186”:0,”10189”:0,”10068”:[],”10188”:0,”10202”:-1,”10146”:1,”10069”:[],”10204”:0,”10203”:-1}
{“10190”:0,”10071”:0,”10191”:0,”10070”:0,”48”:”122186”,”type”:”122186”,”10018”:0,”10117”:0,”332”:0,”333”:0,”999”:3202,”10178”:10131,”8503”:0,”8504”:0,”10135”:”SUSP”,”31”:0,”10116”:0,”35”:”UA3202”,”10185”:0,”10184”:0,”140”:100000,”387”:0,”8538”:”P010”,”10044”:0,”10187”:0,”10043”:0,”10186”:0,”10189”:0,”10068”:[],”10188”:0,”10202”:-1,”10146”:1,”10069”:[],”10204”:0,”10203”:-1}
{“10190”:0,”10071”:0,”10191”:0,”10070”:0,”48”:”122186”,”type”:”122186”,”10018”:0,”10117”:0,”332”:0,”333”:0,”999”:3202,”10178”:10131,”8503”:0,”8504”:0,”10135”:”SUSP”,”31”:0,”10116”:0,”35”:”UA3202”,”10185”:0,”10184”:0,”140”:100000,”387”:0,”8538”:”P010”,”10044”:0,”10187”:0,”10043”:0,”10186”:0,”10189”:0,”10068”:[],”10188”:0,”10202”:-1,”10146”:1,”10069”:[],”10204”:0,”10203”:-1}
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import static org.elasticsearch.common.xcontent.XContentFactory.*;
import java.io.*;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
try {
//通過setting來制定叢集資訊,單機就不需要制定了叢集資訊了,去掉即可
Settings settings = Settings.builder().
put("cluster.name", "elk_test.cluster")
.put("client.transport.sniff",true).build();
//建立客戶端clients
InetAddress address = InetAddress.getByName("192.168.0.153"); // ip
InetSocketAddress socketAddress=new InetSocketAddress(address, 9300); //socket address = ip + port
TransportAddress transportAddress = new InetSocketTransportAddress(socketAddress);
TransportClient client = new PreBuiltTransportClient(settings).addTransportAddress(transportAddress);
File file = new File("E:/json");
FileReader reader=new FileReader(file);
BufferedReader bfr=new BufferedReader(reader);
String line;
BulkRequestBuilder bulkRequest=client.prepareBulk();
int count=0;
while((line=bfr.readLine())!=null){
bulkRequest.add(client.prepareIndex("dddddddd","article").setSource(line));
if (count%10==0) {
bulkRequest.execute().actionGet();
bulkRequest=client.prepareBulk();
}
count++;
//System.out.println(line);
}
bulkRequest.execute().actionGet();
bfr.close();
reader.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
相關文章
- Go 呼叫 elasticsearch 批量操作介面 “/_bulk” ,提示"\n"錯誤GoElasticsearch
- Elasticsearch(ES)叢集的搭建Elasticsearch
- ELK批量刪除索引及叢集相關操作記錄索引
- 使用 Java API 操作 elasticsearchJavaAPIElasticsearch
- mongodb批量操作, bulk_write,MongoDB
- Redis介紹、使用、資料結構和叢集模式總結Redis資料結構模式
- Java API操作ESJavaAPI
- 109.全文檢索-ElasticSearch-入門-刪除資料&bulk批量操作匯入樣本測試資料Elasticsearch
- ElasticSearch7.4批量匯入_bulkElasticsearch
- 【elasticsearch】bulk api奇特的json格式的原因ElasticsearchAPIJSON
- Centos8 部署 ElasticSearch 叢集並搭建 ELK,基於Logstash同步MySQL資料到ElasticSearchCentOSElasticsearchMySql
- java .stream(). 使用介紹 Streams APIJavaAPI
- elasticsearch跨叢集資料遷移Elasticsearch
- Dynamics CRM 如何使用XrmToolBox中的Bulk Workflow Execution批量更新資料
- ES 25 - Elasticsearch生產叢集的配置建議Elasticsearch
- 搭建ELK叢集
- Elasticsearch使用系列-Docker搭建Elasticsearch叢集ElasticsearchDocker
- 寶付介紹ELK中kibana的使用
- 日誌分析平臺ELK之搜尋引擎Elasticsearch叢集Elasticsearch
- ElasticSearch Java API使用ElasticsearchJavaAPI
- Cz工具集使用介紹 - 規範Git提交說明Git
- Docker搭建ELK叢集Docker
- MNIST資料集介紹
- nuPlan資料集介紹
- Cora 資料集介紹
- ClickHouse 邏輯叢集介紹
- Redis 漸進叢集介紹Redis
- Elasticsearch高階之-叢集搭建,資料分片Elasticsearch
- ES叢集
- 06 . ELK Stack + kafka叢集Kafka
- ES 31 - 從0開始搭建Elasticsearch生產叢集Elasticsearch
- ES 30 - Elasticsearch生產叢集的伺服器配置建議Elasticsearch伺服器
- solr叢集構建的基本流程介紹Solr
- ChatGPT API使用介紹ChatGPTAPI
- MyBatis框架的使用解析!資料庫相關API的基本介紹MyBatis框架資料庫API
- Java Stream API groupingBy()介紹JavaAPI
- java操作Redis3.0.6叢集JavaRedisS3
- ElasticSearch Sharing | ES入門分享(spring-boot-starter-ElasticSearch+ElasticStack/ELK)ElasticsearchSpringboot
- Elasticsearch——mget及bulkElasticsearch