0

如何使用 JestResult.getSourceAsObjectList 映射字段“@timestamp”?实际上,我不知道如何映射名称以“@”开头的任何字段。它一直设置为“null”。

例如,如果 Elasticsearch 查询返回

{
    "_id": "Vhv2OE1SNSeSg285UYQRQ",
    "@version": "1",
    "@timestamp": "2014-12-19T01:18:06.454Z"
    "type": "hdfs"
}

并且有一个匹配的 POJO Java 类

import io.searchbox.annotations.JestId;

public class ElasticsearchLog {

    @JestId
    private String _id;
    private String version;
    private String timestamp;
    private String type;

    @Override
    public String toString() {
        return "ElasticsearchLog{" +
                "_id='" + _id + '\'' +
                ", version='" + version + '\'' +
                ", timestamp='" + timestamp + '\'' +
               ", type='" + type + '\''
            '    }';

}

然后做

import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.JestResult;
import io.searchbox.client.config.HttpClientConfig;
import io.searchbox.core.Search;
import java.util.List;

public class JESTClient {
    public static void main(String[] args) throws Exception {
        String clusterIP = "localhost";
        String port = "9200";

        //setup client
        JestClientFactory factory = new JestClientFactory();
        factory.setHttpClientConfig(new HttpClientConfig
                .Builder("http://" + clusterIP + ":" + port)
                .multiThreaded(true)
                .build());
        JestClient client = factory.getObject();

        Search search = new Search.Builder("{  \"query\": { \"match_all\": {} } }")
                .addIndex("_all")
                .build();
        JestResult result = client.execute(search);
        List<ElasticsearchLog> resultLogs = result.getSourceAsObjectList(ElasticsearchLog.class);
        for(ElasticsearchLog log: resultLogs){
            System.out.println(log);
        }

打印出来

ElasticsearchLog{_id='Vhv2OE1SNSeSg285UYQRQ', version='null', timestamp='null', type='hdfs'}

所以“@version”和“@timestamp”没有被正确映射。

JsonObject 本身似乎可以使用“@”符号:

import com.google.gson.JsonObject;

public static void main(String[] args){
        JsonObject testy = new JsonObject();
        testy.addProperty("@timestamp", "zzz");
        System.out.println(testy.get("@timestamp"));
}

输出:“zzz”

JestResult 的正确用法是什么。getSourceAsObjectList 映射名称以“@”开头的 json 字段?

注意:这是为了

<dependency>
    <groupId>io.searchbox</groupId>
    <artifactId>jest</artifactId>
    <version>0.1.4</version>
</dependency>
4

1 回答 1

1

全力支持救援!

来自 support@searchly.com 的电子邮件回复

Ferhat Sobay 回复:

这里的问题是@version 需要一个Java 字段作为'@version',这对于Java lang 是不可能的。@SerializedName 注释来拯救,不得不挖一点但有效!

所以试试下面;

public class ElasticsearchLog {

  @JestId
  private String _id;
  @SerializedName("@version")
  private String version;
  @SerializedName("@timestamp")
  private String timestamp;
  private String type;

  @Override
  public String toString() {
  return "ElasticsearchLog{" +
  "_id='" + _id + '\'' +
  ", version='" + version + '\'' +
  ", timestamp='" + timestamp + '\'' +
  ", type='" + type + '\''
  ' }';

}

最好的, @Ferhat

于 2014-12-23T18:27:50.250 回答