在 Ignite persistence 中用作 POJO 策略时的 Scala case class 构造函数错误

Scala case class constructor error when used as POJO strategy in Ignite persistence

我在 Scala 中有一个案例 class:

class StateCache(greeting: String, first_name: String, last_name: String)

我正在使用 Spark + Ignite 来拥有一个持久保存到 Cassandra 的直写缓存。将持久性-settings.xml 文件设置为:

<persistence keyspace="testing_ignite" table="people_test">
    <keyPersistence class="java.lang.Long" strategy="PRIMITIVE" column="index"/>
    <valuePersistence class="StateCache" strategy="POJO"/>
</persistence>

我收到以下错误: Java class 'StateCache' couldn't be used as POJO cause it doesn't have no arguments constructor。我添加了一个空的构造函数,但它没有解决错误。任何帮助将不胜感激。

根据 POJO 的 documentation(突出显示是我的):

Stores each field of an object as a column having corresponding type in Cassandra table. Provides ability to utilize Cassandra secondary indexes for object fields. Could be used only for POJO objects following Java Beans convention and having their fields of simple java type which could be directly mapped to corresponding Cassandra types.

您的 class StateCache 不符合 Java Beans 规范。要解决此问题,您需要:

代码应该是这样的:

class StateCache(@BeanProperty var greeting: String, 
                 @BeanProperty var first_name: String, 
                 @BeanProperty var last_name: String) {
    def this() {
        this(null,null,null);
    }
}

这不是惯用的 Scala,但这是您与使用 Java 约定的世界互动所需要的。