How to use Cassandra Map type in Java

Krishna :

I have created the table in the keyspace like

CREATE TABLE IF NOT EXISTS myks.users (user_id text, device_ids map<text,text>);

I'm trying to insert the below data using `spring-boot-starter-data-cassandra` library in Spring boot

{
    "userId" : "krishna",
    "deviceIds": {
        "DeviceId_0": "TYNLSANID7",
        "DeviceId_1": "julasjasd8",
        "DeviceId_2": "iu89074hasd",
        "DeviceId_3": "Lenovo hjyas|asdqnkasd"
    }
}

and below is the entity class

@Column("user_id")
@CassandraType(type = DataType.Name.TEXT)
private String userId;

@Column("device_ids")
@CassandraType(type = DataType.Name.MAP)
private Map<String, String> deviceIds= new HashMap<>();

I'm getting the below exception when inserting the data into table

org.springframework.dao.InvalidDataAccessApiUsageException: Expected [2] type arguments for property ['deviceIds'] of type ['interface java.util.Map'] in entity [com.kri.entity.Users]; actual was [0]
NiVeR :

As specified in the documentation you should provide the typeArguments property in the annotation definition when you are working with Collection, and in specific for Map data type you should provide two DataType Name properties, one for the key and one for the value resipectively:

@CassandraType(type = DataType.Name.MAP,typeArguments = { DataType.Name.TEXT,DataType.Name.TEXT} 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=328454&siteId=1