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

时间:2019-01-18 19:04:17

标签: scala ignite pojo

I have a case class in Scala as so:

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

I am using Spark + Ignite to have a write through cache that persists to Cassandra. When setting the persistence-settings.xml file to:

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

I receive the following error: Java class 'StateCache' couldn't be used as POJO cause it doesn't have no arguments constructor. I have added in an empty constructor but it does not resolve the error. Any help would be much appreciated.

1 个答案:

答案 0 :(得分:1)

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

  

将对象的每个字段存储为Cassandra表中具有相应类型的列。提供将Cassandra二级索引用于对象字段的功能。 只能用于遵循Java Beans约定的POJO对象 ,并且其字段具有简单Java类型的字段,可以直接映射到相应的Cassandra类型。

您的类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约定的世界进行交互所需要的。