Elasticsearch更新索引

时间:2017-04-07 13:45:15

标签: java elasticsearch indexing

我有弹性的问题:当我尝试通过HTTP PUT使用我的自定义java连接器更新索引时,它只更新了2个文件然后只是卡住了(这个文件响应状态为200,我可以用URL获取它们)。没有错误,没有响应,服务器变得不可用。当我试图从想法IDE评估我的方法时,它说“评估”。哪里有问题?

这是我连接和更新索引的代码。

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;

import java.io.IOException;
import java.sql.*;
import java.time.Duration;


public class Connector {

private static String selectAll = "select * from message";
private static String checkForNull = "SELECT count(*) FROM (SELECT 1 FROM elastic_updates LIMIT 1) AS t";
private static String getLastTimestamp = "SELECT create_date FROM message ORDER BY create_date DESC LIMIT 1";
private static String getLastUpdate = "SELECT last_update FROM elastic_updates ORDER BY last_update DESC LIMIT 1";
private static String url = ("jdbc:postgresql://taggo-prod.cpizgq9tfz51.eu-central-1.rds.amazonaws.com/taggo");
private static String login = ("taggo_admin");
private static String pass = ("welcome2u");

public static void main(String[] args) {
    Statement statement = null;
    Connector connector = new Connector();
    int count = 1;
    HttpClient httpClient = HttpClientBuilder.create().build();
    try (Connection connection = DriverManager.getConnection(url, login, pass)) {
        System.out.println("Connected");

        statement = connection.createStatement();

        ResultSet numOfRecords = statement.executeQuery(checkForNull);
        if (connector.checkForNullValue(numOfRecords)) {
            ResultSet resultSet = statement.executeQuery(selectAll);
            while (resultSet.next()) {
                HttpPut httpPut = new HttpPut("https://search-tag-go-elastic-5vqc4gnxdl77znhdc53sd4la34.eu-central-1.es.amazonaws.com/message/text/" + count + "?pretty");
                String jsonString = connector.createMessage(resultSet);
                StringEntity stringEntity = new StringEntity(jsonString, ContentType.APPLICATION_FORM_URLENCODED);
                httpPut.setEntity(stringEntity);
                HttpResponse response = httpClient.execute(httpPut);
                System.out.println(response);
                count++;
            }
            resultSet.close();
        } else {

        }
        numOfRecords.close();
    } catch (SQLException e) {
        e.printStackTrace();
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

private String createMessage(ResultSet resultSet) throws SQLException, JsonProcessingException {
    ObjectMapper mapper = new ObjectMapper();
    Message message = new Message();
    message.setContent(resultSet.getString("content"));
    message.setCreateDate(resultSet.getString("create_date"));
    message.setActive(Boolean.parseBoolean(resultSet.getString("is_active")));
    message.setLifetime(Duration.ofMillis(resultSet.getLong("lifetime") / 1000));
    message.setMediaContent(resultSet.getString("media_content"));
    String jsonString = mapper.writeValueAsString(message);
    return jsonString;
}

private boolean checkForNullValue(ResultSet resultSet) {
    try {
        resultSet.next();
        String date = resultSet.getString("last_update");
        if (date.isEmpty()) {
            return true;
        } else {
            return false;
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return true;
}

}

0 个答案:

没有答案