Unable to obtain Instant from TemporalAccessor: {},ISO resolved to 2018-01-01 of type java.time.format.Parsed

huangapple go评论73阅读模式
英文:

Unable to obtain Instant from TemporalAccessor: {},ISO resolved to 2018-01-01 of type java.time.format.Parsed

问题

我已经创建了一个具有以下映射的 Elasticsearch(版本 7.8.1)文档:

{
    "transaction": {
        "mappings": {
            "properties": {
                "_class": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "settlementEntries": {
                    "type": "nested",
                    "properties": {
                        "settlementDate": {
                            "type": "date",
                            "format": "uuuu-MM-dd"
                        },
                        "settlementId": {
                            "type": "long"
                        }
                    }
                },
                "transactionId": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                }
            }
        }
    }
}

我正在使用 yyyy-MM-dd 格式来存储 settlementDate。当我使用 CURL -X GET localhost:9200/transaction/_search 进行查询时,我能够看到数据。但是,当我尝试通过 Spring Boot 进行相同操作时,出现错误。我的实体类如下:

public class TransactionBo {

    @Id
    private String transactionId;
    @Field(type = FieldType.Nested)
    private SettlementEntryBo settlementEntries;

}

public class SettlementEntryBo {

    @Id
    private Long settlementId;
    @Nullable
    @Field(type = FieldType.Date, format = DateFormat.custom, pattern = "uuuu-MM-dd")
    private Date settlementDate;

}

从我所能理解的情况来看,问题可能出现在以下映射部分:

@Field(type = FieldType.Date, format = DateFormat.custom, pattern = "uuuu-MM-dd")
private Date settlementDate;

[错误片段]:

2020-09-05 02:51:18.105 ERROR 10080 --- [nio-8090-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.time.DateTimeException: Unable to obtain Instant from TemporalAccessor: {}, ISO resolved to 2016-01-01 of type java.time.format.Parsed] with root cause

java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: InstantSeconds
英文:

I've made a Elasticsearch(version 7.8.1) document with the following mappings:

{
    "transaction": {
        "mappings": {
            "properties": {
                "_class": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                
                },
                "settlementEntries": {
                    "type": "nested",
                    "properties": {
                        "settlementDate": {
                            "type" : "date",
                            "format" : "uuuu-MM-dd"
                        },
                        "settlementId": {
                            "type": "long"
                        }
                    }
                },
                "transactionId": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                }
            }
        }
    }
}

I'm using the format yyyy-MM-dd to store settlementDate. When I'm querying using using curl, I' able to see the data using CURL -X GET localhost:9200/transaction/_search. But, when I'm trying to do the same via springboot it's throwing an error.My entities are:

public class TransactionBo {

	@Id
	private String transactionId;
	@Field(type = FieldType.Nested)
	private SettlementEntryBo settlementEntries;
	
}

and

public class SettlementEntryBo {

	@Id
	private Long settlementId;
	@Nullable
    @Field(type = FieldType.Date, format = DateFormat.custom, pattern = "uuuu-MM-dd")
private Date settlementDate;

}

From what I can figure out the issue lies in the mapping here:

@Field(type = FieldType.Date, format = DateFormat.custom, pattern = "uuuu-MM-dd")
private Date settlementDate;

[ERROR SNIPPET]:

2020-09-05 02:51:18.105 ERROR 10080 --- [nio-8090-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.time.DateTimeException: Unable to obtain Instant from TemporalAccessor: {},ISO resolved to 2016-01-01 of type java.time.format.Parsed] with root cause

java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: InstantSeconds

答案1

得分: 1

将您的模式更改为使用_uuuu_而不是_yyyy_;这个在这里有记录,这是Elasticsearch中负责这个更改的部分:https://www.elastic.co/guide/en/elasticsearch/reference/current/migrate-to-java-time.html#java-time-migration-incompatible-date-formats

我还看到另外一件事:

您正在使用只包含年份、月份和日期的日期格式。那是一个简单的日期。但java.util.Date不是一个日期,而是一个在UTC时区内的时间点,包括时间戳。

所以,您应该像Ole在他的评论中建议的那样,将您的属性类型java.time.LocalDate进行更改。这些类是在Java 8中引入的,以克服java.util.Date的所有不足之处。

英文:

Change your pattern to use uuuu instead of yyyy; this is documented here, the change in Elasticsearch responsible for this: https://www.elastic.co/guide/en/elasticsearch/reference/current/migrate-to-java-time.html#java-time-migration-incompatible-date-formats

Another thing I see:

You are using a date format that contains just the year, month and day. That's a plain date. But java.util.Date is not a date, but an instant in time in UTC zone - including a timestamp.

So you should change your property type java.time.LocalDate like Ole suggested in his comment. These classes were introduced back in Java 8 to surcome all the deficiencies that java.util.Datehas.

huangapple
  • 本文由 发表于 2020年9月5日 04:13:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63747516.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定