java 增加es数据_Java连接ElasticSearch(low-level-rest-client)的配置和数据的增加/查询

配置ELK之后,可以发挥elasticsearch的更大功效,就是用java连接elastic进行数据的存储,达到快速读取的目的。可以取代redis、或者solr的功能。在一些搜索服务功能、或者列表服务功能处可以恰当的使用。

POM依赖

org.elasticsearch

elasticsearch

6.4.2

org.elasticsearch.client

elasticsearch-rest-high-level-client

6.4.2

org.elasticsearch.client

elasticsearch-rest-client

6.4.2

org.apache.httpcomponents

httpcore-nio

4.4.6

org.apache.httpcomponents

httpasyncclient-osgi

4.0.1

org.apache.httpcomponents

httpcore-niossl

4.0-alpha5

org.elasticsearch.plugin

shield

2.4.3-atlassian-shaded-1

下面是Java代码处理

package com.kaistart;

import java.io.IOException;

import java.util.ArrayList;

import java.util.Collections;

import java.util.concurrent.CountDownLatch;

import org.apache.http.Header;

import org.apache.http.HttpEntity;

import org.apache.http.HttpHost;

import org.apache.http.ParseException;

import org.apache.http.RequestLine;

import org.apache.http.auth.AuthScope;

import org.apache.http.auth.UsernamePasswordCredentials;

import org.apache.http.client.CredentialsProvider;

import org.apache.http.impl.client.BasicCredentialsProvider;

import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;

import org.apache.http.util.EntityUtils;

import org.elasticsearch.client.Request;

import org.elasticsearch.client.RequestOptions;

import org.elasticsearch.client.Response;

import org.elasticsearch.client.ResponseListener;

import org.elasticsearch.client.RestClient;

import org.elasticsearch.client.RestClientBuilder;

import org.elasticsearch.client.RestClientBuilder.HttpClientConfigCallback;

public class Test {

@SuppressWarnings("deprecation")

public static void main(String args[]) {

/** 用户认证对象 */

final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();

/** 设置账号密码 */

credentialsProvider.setCredentials(AuthScope.ANY,

new UsernamePasswordCredentials("elastic", "123456"));

/** 创建rest client对象 */

RestClientBuilder builder = RestClient.builder(new HttpHost("xx.xxx.xx.xx", 9200))

.setHttpClientConfigCallback(new HttpClientConfigCallback() {

@Override

public HttpAsyncClientBuilder customizeHttpClient(

HttpAsyncClientBuilder httpClientBuilder) {

return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);

}

});

/**

* 创建客户端实例

*/

RestClient client = builder.build();

/**

* 发送同步查询, zeus的索引模板信息

*/

Response response;

try {

response = client.performRequest("GET", "_template/zeus*",

Collections.singletonMap("pretty", "true"));

System.out.println(EntityUtils.toString(response.getEntity()));

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

// 索引的处理 //索引/类型/唯一ID

/**

* 创建请求信息 参数(请求方式, 请求地址【索引名称/文档类型/文档唯一编号】) 如:/project/test/1 项目索引/test类型/编号1

*

* 增加索引文档方式一 "PUT","/project/test/1" 需要唯一ID

* 增加索引文档方式二 "POST","/project/test/" 不需要指定唯一ID,会随机生成唯一字符串

* 查询索引文档方式 "GET","/project/test/1" 根据搜索语法可以进行查询

*/

Request request = new Request("GET", "/project/test/1");

/**

* 增加请求参数, 拼接在url后。请求后的连接地址为: /project/test/1?pretty=true

*/

request.addParameter("pretty", "true");

/**

* 设置 context内容 两种方式

* 1. request.setEntity(new NStringEntity("{\"json\":\"text\"}",ContentType.APPLICATION_JSON));

* 2. request.setJsonEntity("{\"json\":\"text\"}");

*/

request.setJsonEntity("{\"json\":\"text\"}");

/***

* 增加header参数

*/

RequestOptions.Builder builder_optioins = RequestOptions.DEFAULT.toBuilder();

builder_optioins.addHeader("Auth", "token");

request.setOptions(builder_optioins);

/***

* 创建异步的查询方式

*/

client.performRequestAsync(request, new ResponseListener() {

@Override

public void onSuccess(Response arg0) {

// TODO Auto-generated method stub

try {

//输出响应信息

RequestLine requestline = arg0.getRequestLine();

HttpHost host = arg0.getHost();

int statusCode = arg0.getStatusLine().getStatusCode();

Header[] headers = arg0.getHeaders();

String responseBody = EntityUtils.toString(arg0.getEntity());

System.out.println("requestline=" + requestline);

System.out.println("host=" + host.toHostString());

System.out.println("statusCode=" + statusCode);

System.out.println("headers=" + headers.toString());

System.out.println("responseBody=" + responseBody);

} catch (ParseException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

@Override

public void onFailure(Exception arg0) {

// TODO Auto-generated method stub

System.out.println(arg0.toString());

}

});

/***

* 异步处理多个文档列表 【官方推荐这种做法】

*/

ArrayList documents = new ArrayList();

final CountDownLatch latch = new CountDownLatch(documents.size());

for (int i = 0; i < documents.size(); i++) {

Request requestM = new Request("PUT", "/posts/doc/" + i);

// let's assume that the documents are stored in an HttpEntity array

requestM.setEntity(documents.get(i));

client.performRequestAsync(requestM, new ResponseListener() {

@Override

public void onSuccess(Response response) {

latch.countDown();

}

@Override

public void onFailure(Exception exception) {

latch.countDown();

}

});

}

try {

latch.await();

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

下面是输出的信息\第一个sysout

{

"test-*":{

"order":0,

"index_patterns":[

"test*"

],

"settings":{

"index":{

"number_of_shards":"3",

"number_of_replicas":"0"

}

},

"mappings":{

},

"aliases":{

}

}

}

第二个sysout

requestline=GET /project/test/1?pretty=true HTTP/1.1

host=xxx.xx.xxx.xxx:9200

statusCode=200

headers=[Lorg.apache.http.Header;@797419fc

responseBody={

"_index" : "project",

"_type" : "test",

"_id" : "1",

"_version" : 1,

"found" : true,

"_source" : {

"json" : "text"

}

}

出现问题还是多看看官网比较好。附原址:

https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-low-usage-requests.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值