java - 如何在 java 中使用 Elasticsearch Rest api?

标签 java elasticsearch httpclient apache-httpclient-4.x apache-commons-httpclient

我正在使用 Apache Http 客户端来使用 ElasticSearch Rest Api,但我总是收到 HTTP 错误代码 200。请帮忙

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())));
            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();
        }
    }
}

控制台

{
   "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)

这里是elasticsearch sense截图

enter image description here

最佳答案

状态码 200 代表“OK”
check w3c ref


你应该使用

    if(response.getStatusLine().getStatusCode() != 200){
        // Throw exception or something else
    } 

关于java - 如何在 java 中使用 Elasticsearch Rest api?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25117724/

相关文章:

php - 突出显示在 Elasticsearch 和 PHP 中不起作用

c# - WPF:如何在 Windows 之间共享 httpClient

c# - 取消 c# httpClient GetStreamAsync 调用

java - 如何查询按计数分组的对象列表(按最后一天的提及频率)

azure - 弹性云与弹性 Azure 市场模板

java - 不从 Spring Controller 返回 JSP

c# - 使用Elasticsearch.NET重新创建经Elasticsearch过滤的查询

AndroidHttpClient 关闭后无法 getEntity().getContent()

java - 谁能推荐一个高效的 Java UDP 消息传递框架?

java - 使用自定义拆分将变量添加到 ArrayList