Search code examples
ignite

Codec not found for requested operation: [list<varchar> <-> java.nio.HeapByteBuffer]


I am having an issue with storing data to Cassandra table from apache ignite when I am trying to insert into a column of list data type in Cassandra

Cassandra table:

CREATE TABLE business_categories (
id int,
category_name TEXT,
sub_categories list<TEXT>,
PRIMARY KEY(category_name, id)
);

xml file:

<persistence keyspace="ignite" table="business_categories">
    <keyspaceOptions>
        REPLICATION = {'class' : 'SimpleStrategy', 'replication_factor' : 1}
        AND DURABLE_WRITES = true
    </keyspaceOptions>
    <tableOption>
        comment = 'Cache test'
        AND read_repair_chance = 0.2
    </tableOption>
    <keyPersistence class="com.cache.business.model.BusinessCategoriesKey" strategy="POJO"/>
    <valuePersistence class="com.cache.business.model.BusinessCategoriesValue" strategy="POJO"/>
</persistence>

key class object:

public class BusinessCategoriesKey implements Serializable {

    private static final long serialVersionUID = 581472167344584014L;
    private int id;
    private String category_name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getCategory_name() {
        return category_name;
    }

    public void setCategory_name(String category_name) {
        this.category_name = category_name;
    }

}

value class object:

public class BusinessCategoriesValue implements Serializable {

    private static final long serialVersionUID = -1694694702874919854L;
    private List<String> sub_categories = new ArrayList<>();

    public List<String> getSub_categories() {
        return sub_categories;
    }

    public void setSub_categories(List<String> sub_categories) {
        this.sub_categories = sub_categories;
    }

    public static long getSerialversionuid() {
        return serialVersionUID;
    }

}

I am getting the below error message

Caused by: com.datastax.driver.core.exceptions.CodecNotFoundException: Codec not found for requested operation: [list <-> java.nio.HeapByteBuffer]


Solution

  • The sub_categories field is a java.util.List and it seems Apache Ignite does not provide a direct mapping to appropriate Cassandra type for such kind of Java types. So, this field could be persisted into Cassandra only if you manually specify all mapping details for the object type and if field type itself is implementing java.io.Serializable interface. In such case, the field will be persisted into a separate table column as a blob.

    Please try to modify your code in the following way:

    CREATE TABLE business_categories (
        id int,
        category_name text
        sub_categories blob,
        PRIMARY KEY(category_name, id)
    );
    

    Persistence descriptor:

    <persistence keyspace="ignite" table="business_categories">
    <keyspaceOptions>
        REPLICATION = {'class' : 'SimpleStrategy', 'replication_factor' : 1}
        AND DURABLE_WRITES = true
    </keyspaceOptions>
    <tableOption>
        comment = 'Cache test'
        AND read_repair_chance = 0.2
    </tableOption>
    
    <keyPersistence class="com.cache.business.model.BusinessCategoriesKey" strategy="POJO"/>
    
    <valuePersistence class="com.cache.business.model.BusinessCategoriesValue"
                      strategy="POJO"
                      serializer="org.apache.ignite.cache.store.cassandra.serializer.JavaSerializer">
        <field name="sub_categories" column="sub_categories"/>
    </valuePersistence>
    

    You can find additional details here: Cassandra Integration Examples