这里我使用java连接器REST API(Spring boot(将数据从SQL服务器抓取到Elasticsearch,当我运行服务时,大多数时候都会出现超时异常错误。我在下面给出了我的示例代码供参考。
@PostMapping("elasticsearch")
public String getdatafromelasticsearch() {
String finalResponse = "";
LOGGER.info("Sync service to get data from Elasticsearch starts - " + new Date());
try {
List<String> odsData= new ArrayList<String>();
odsData= GetSqlresultset();
SimpleDateFormat sm = new SimpleDateFormat(ConstantsHelper.DATE_FORMAT);
String strDate = sm.format(new Date());
GetIndexRequest getRequest = new GetIndexRequest(ConstantsHelper.INDEX_NAME + strDate);
LOGGER.info("Searching index name - " + ConstantsHelper.INDEX_NAME + strDate);
boolean exists = client.indices().exists(getRequest, RequestOptions.DEFAULT);
LOGGER.info("Index exists" + exists);
if (exists) {
DeleteIndexRequest request = new DeleteIndexRequest(ConstantsHelper.INDEX_NAME + strDate);
AcknowledgedResponse deleteIndexResponse = client.indices().delete(request, RequestOptions.DEFAULT);
LOGGER.info("DeleteIndexResponse -" + deleteIndexResponse);
}
for (int i = 0; i < odsData.size(); i++) {
IndexRequest request = new IndexRequest(ConstantsHelper.INDEX_NAME + strDate);
request.id();
String jsonString = odsData.get(i).toString();
request.source(jsonString, XContentType.JSON);
IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
}
finalResponse = ConstantsHelper.SUCCESS_MSG;
finalResponse = Integer.toString(odsData.size());
} catch (Exception ex) {
LOGGER.error(ex.getMessage());
finalResponse = ex.getMessage();
} finally {
LOGGER.info("Final response -" + finalResponse);
LOGGER.info("Sync service to get Elasticsearch ends - " + new Date());
}
return finalResponse;
}
public List<String> GetSqlresultset() throws Exception {
LOGGER.info("Inside getsqlresultset method start");
ResultSet resultSet = null;
List<String> sd = new ArrayList<String>();
String connectionUrl = jdbcUri + databaseName + dbUser + dbPwd;
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
try (Connection connection = DriverManager.getConnection(connectionUrl);
Statement statement = connection.createStatement();) {
String selectSql = "";
// Create and execute a SELECT SQL statement.
selectSql = ConstantsHelper.SELECT_STATEMENT;
resultSet = statement.executeQuery(selectSql);
while (resultSet.next()) {
ResultSetMetaData rsmd = resultSet.getMetaData();
int numColumns = rsmd.getColumnCount();
JSONObject obj = new JSONObject();
for (int i = 1; i <= numColumns; i++) {
String column_name = rsmd.getColumnName(i);
obj.put(column_name, resultSet.getObject(column_name));
}
sd.add(obj.toString());
}
// connection.close();
}
LOGGER.info("Inside getsqlresultset method End");
return sd;
}
错误
<html>
<head>
<title>504 Gateway Time-out</title>
</head>
<body bgcolor="white">
<center>
<h1>504 Gateway Time-out</h1>
</center>
</body>
</html>
方法GetSqlresultset((用于连接到SQL server,我找不到为什么会出现超时异常——这是由于SQL server或将记录逐一索引到Elasticsearch。
注意:我的java服务运行时间超过10分钟。
最后我发现了问题是什么,因为我的tomcat。在我的tomcat中,默认超时为60秒,一旦我们将其增加到2分钟,我就会得到预期的响应。