【发布时间】:2016-07-17 23:32:51
【问题描述】:
我有以下代码,我将来自 Elasticsearch 的 json 数据存储在存储在本地驱动器中的文件中。相反,我想将数据存储在 Arraylist 中,以便将其传递给 UI 以在前端显示。
try {
HttpGet request = new HttpGet("http://localhost:9200/indexname/_search?pretty=1&size=100&q=name:%22Test%22");
HttpResponse resp = client.execute(request);
HttpEntity entity = resp.getEntity();
byte[] buffer = new byte[1024];
if (entity != null) {
InputStream input = entity.getContent();
FileWriter file = new FileWriter("/localpath/data.json");
try {
int bytesRead = 0;
BufferedInputStream in = new BufferedInputStream(input);
while ((bytesRead = in.read(buffer)) != -1) {
String chunk = new String(buffer, 0, bytesRead);
System.out.println(chunk);
file.write(chunk);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try { file.close(); inputStream.close(); } catch (Exception ignore) {}
}
}
}
JSON 数据格式为:
{
"took": 9,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 6.3519955,
"hits": [
{
"_index": "indexname",
"_type": "status",
"_id": "eb825358-93d7-4a2f-9227-9b060cbc323a",
"_score": 6.3519955,
"_source": {
"name": "Test",
"ID": "ABCD",
"Number": "23222",
"SecCode": "124"
}
},
{
"_index": "indexname",
"_type": "status",
"_id": "eb825358-93d7-4a2f-9227-9b060cbc323b",
"_score": 6.3519955,
"_source": {
"name": "Test",
"ID": "CDEF",
"Number": "5678",
"SecCode": "120"
}
}
]
}
}
在我的数组列表中,我只想存储 ID 和数字。
任何帮助将不胜感激?谢谢
【问题讨论】:
标签: java json arraylist elasticsearch