如何在Java中使用Elasticsearch Rest api? [英] How to use Elasticsearch Rest api in java?
本文介绍了如何在Java中使用Elasticsearch Rest api?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Java代码
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.util.Scanner;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
public class ApacheHttpClientPost {
public static void main(String [] args){
String path =C:\\Tools\\ ElasticSearchApi\\javadoc.txt,filecontent =;
ApacheHttpClientPost apacheHttpClientPost = new ApacheHttpClientPost();
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost(http:// localhost:9200 / versioneg / message / _percolate);
filecontent = apacheHttpClientPost.readFileContent(path);
System.out.println(filecontent);
StringEntity input = new StringEntity(filecontent);
input.setContentType(application / json);
postRequest.setEntity(input);
HttpResponse response = httpClient.execute(postRequest);
if(response.getStatusLine()。getStatusCode()!= 201){
throw new RuntimeException(Failed:HTTP error code:+ response.getStatusLine()。getStatusCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity()。getContent())));
字符串输出;
System.out.println(从服务器输出.... \\\
);
while((output = br.readLine())!= null){
System.out.println(output);
}
httpClient.getConnectionManager()。shutdown();
} catch(MalformedURLException e){
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
}
}
private String readFileContent(String pathname)throws IOException {
文件文件=新建文件(路径名);
StringBuilder fileContents = new StringBuilder((int)file.length());
扫描仪扫描仪=新扫描仪(文件);
String lineSeparator = System.getProperty(line.separator);
try {
while(scanner.hasNextLine()){
fileContents.append(scanner.nextLine()+ lineSeparator);
}
return fileContents.toString();
} finally {
scanner.close();
}
}
}
控制台
{
doc:{
os:Linux,
product
name:abc,
version:10.1,
configversion:1,
arch:32,
license 商业,
db:{
@type:Oracle
}
}
}
}
线程main中的异常java.lang.RuntimeException:失败:HTTP错误代码:200
在com.informatica.zanshin.esapi.utils.ApacheHttpClientPost.main(ApacheHttpClientPost.java:31)
这里是弹性搜索意义截图
解决方案
状态码200代表'OK'
检查w3c ref
您应该使用
if(response .getStatusLine()。getStatusCode()!= 200){
//抛出异常或其他东西
}
I am using Apache Http client for making use of ElasticSearch Rest Api, but I am always getting HTTP error code as 200. Please help
Java code
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.util.Scanner;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
public class ApacheHttpClientPost {
public static void main(String[] args) {
String path="C:\\Tools\\ElasticSearchApi\\javadoc.txt", filecontent="";
ApacheHttpClientPost apacheHttpClientPost = new ApacheHttpClientPost();
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost("http://localhost:9200/versioneg/message/_percolate");
filecontent=apacheHttpClientPost.readFileContent(path);
System.out.println(filecontent);
StringEntity input = new StringEntity(filecontent);
input.setContentType("application/json");
postRequest.setEntity(input);
HttpResponse response = httpClient.execute(postRequest);
if (response.getStatusLine().getStatusCode() != 201) {
throw new RuntimeException("Failed : HTTP error code : " + response.getStatusLine().getStatusCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
httpClient.getConnectionManager().shutdown();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private String readFileContent(String pathname) throws IOException {
File file = new File(pathname);
StringBuilder fileContents = new StringBuilder((int)file.length());
Scanner scanner = new Scanner(file);
String lineSeparator = System.getProperty("line.separator");
try {
while(scanner.hasNextLine()) {
fileContents.append(scanner.nextLine() + lineSeparator);
}
return fileContents.toString();
} finally {
scanner.close();
}
}
}
Console
{
"doc": {
"os": "Linux",
"product": {
"name": "abc",
"version": 10.1,
"configversion": 1,
"arch": 32,
"license": "commercial",
"db": {
"@type": "Oracle"
}
}
}
}
Exception in thread "main" java.lang.RuntimeException: Failed : HTTP error code : 200
at com.informatica.zanshin.esapi.utils.ApacheHttpClientPost.main(ApacheHttpClientPost.java:31)
Here is elasticsearch sense screenshot
解决方案
Status Code 200 stands for 'OK'
check w3c ref
You should use
if(response.getStatusLine().getStatusCode() != 200){
// Throw exception or something else
}
这篇关于如何在Java中使用Elasticsearch Rest api?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文