未找到请求操作的编解码器:[TEXT <-> java.time.LocalDate]

发布于 2025-01-11 09:35:29 字数 793 浏览 0 评论 0 原文

这是我用来填充数据库中的列的代码。

DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSS");
JSONObject publishedObj = jsonObject.optJSONObject("created");
                    if(publishedObj != null){
                        String dateStr = publishedObj.getString("value");
                        book.setPublishedDate(LocalDate.parse(dateStr,dateFormat));
                    }

下面是数据需要去的列的实例变量:

@Column("published_date")
@CassandraType(type = CassandraType.Name.DATE)
private LocalDate publishedDate;

我收到的错误消息:

com.datastax.oss.driver.api.core.type.codec.CodecNotFoundException: Codec not found for requested operation: [TEXT <-> java.time.LocalDate]

请有人帮忙。 谢谢你!!

This is the code which I am using to fill the column in the db.

DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSS");
JSONObject publishedObj = jsonObject.optJSONObject("created");
                    if(publishedObj != null){
                        String dateStr = publishedObj.getString("value");
                        book.setPublishedDate(LocalDate.parse(dateStr,dateFormat));
                    }

Below is the instance variable of the column where the data needs to go:

@Column("published_date")
@CassandraType(type = CassandraType.Name.DATE)
private LocalDate publishedDate;

Error Message which i am getting:

com.datastax.oss.driver.api.core.type.codec.CodecNotFoundException: Codec not found for requested operation: [TEXT <-> java.time.LocalDate]

Can please someone help.
Thankyou!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薄荷港 2025-01-18 09:35:29

我可以用上面的代码重现该错误。为了解决这个问题,我对 book_by_id 表进行了 ALTER 编辑,添加了两个新列:

ALTER TABLE book_by_id ADD pubdate2 TEXT;
ALTER TABLE book_by_id ADD pubdate3 DATE;

如下所示:

@Column("pubdate2")
@CassandraType(type = CassandraType.Name.TEXT)
private String publishedDate2;

@Column("pubdate3")
@CassandraType(type = CassandraType.Name.DATE)
private LocalDate publishedDate3;

这些列的 BookEntity类 解析并设置日期如下所示:

DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSS");
String dateStr = "2022-03-03T09:52:33.235555";
musicBook.setPublishedDate2(LocalDate.parse(dateStr,dateFormat).toString()); 
musicBook.setPublishedDate3(LocalDate.parse(dateStr,dateFormat));
        
template.insert(musicBook);

tl;dr;

published_date 重新定义为 DATE 类型,它将起作用。此外,日期/时间应该以日期/时间类型存储在数据库中。

请注意,Cassandra 不允许您修改列的数据类型。此外,过去事实证明,Cassandra 快速连续删除和添加同名列的过程会出现问题。我建议添加一个新命名的 DATE 类型列,并重新加载其数据。或者重新创建表(使用正确的数据类型)并重新加载数据。

I can reproduce that error with your code above. To remedy it, I have ALTERed the book_by_id table with two new columns:

ALTER TABLE book_by_id ADD pubdate2 TEXT;
ALTER TABLE book_by_id ADD pubdate3 DATE;

My BookEntity class for those columns looks like this:

@Column("pubdate2")
@CassandraType(type = CassandraType.Name.TEXT)
private String publishedDate2;

@Column("pubdate3")
@CassandraType(type = CassandraType.Name.DATE)
private LocalDate publishedDate3;

The code to parse and set the date looks like this:

DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSS");
String dateStr = "2022-03-03T09:52:33.235555";
musicBook.setPublishedDate2(LocalDate.parse(dateStr,dateFormat).toString()); 
musicBook.setPublishedDate3(LocalDate.parse(dateStr,dateFormat));
        
template.insert(musicBook);

tl;dr;

Redefine published_date as a DATE type, and it will work. Besides, dates/times should be stored in date/time types in databases.

Note that Cassandra won't allow you to modify a column's data type. Also, the process of dropping and adding a column with the same name in quick succession has proven to be problematic with Cassandra in the past. I'd advise adding a newly named column of a DATE type, and reloading its data. Or recreate the table (with the correct data types) and reload the data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文