Reputation: 117
I have a User Defined Type in Cassandra which I created using following syntax in CQLSH :
CREATE TYPE order_items (
qty int,
name text,
milk_type text,
size text,
price decimal
);
Now in my table, I am storing a list of the type "order_items" so I can store multiple items for one object.
Something like this :
CREATE TABLE order (
order_id uuid PRIMARY KEY,
amount decimal,
location text,
items list<frozen<order_items>>,
status text,
message text
);
If I want to store a record using CQLSH, I can do so with the following syntax,
INSERT INTO order (order_id,amount,location,items,status,message) VALUES
(e7ae5cf3-d358-4d99-b900-85902fda9bb0, 5, 'San Jose',[{qty:2,name:'mocha',milk_type:'whole',size:'small',price:2.5}], 'PLACED','order is in process');
but when I try to do the same using the DataStax Java driver for cassandra, I am not able to provide the user defined type as a list of objects. I could only come up with a string syntax, but obviously it is throwing the above error.
I have tried referring the datastax documentation, but apparently it is still a work in progress : http://docs.datastax.com/en/developer/java-driver/3.1/manual/udts/
Here is my Java syntax:
session.execute(insertorderPstmt.bind(UUID.randomUUID(),new BigDecimal("4"),"Santa Clara","[{qty:2,name:'mocha',milk_type:'whole',size:'small',price:2.5}]","PLACED","in process"));
and the error:
Exception in thread "main" com.datastax.driver.core.exceptions.InvalidTypeException: Invalid type for value 3, column is a list but class java.lang.String provided
Is there anyone who has been able to store custom type lists using java driver?
Upvotes: 0
Views: 2026
Reputation: 12830
Your insert query is not correct.
If you use single quote then use it for everything, and separate all field with coma. If the field is string then enclose it with quote,
Use the below insert query :
INSERT INTO order (
order_id,
amount,
location,
items,
status,
message
) VALUES (
e7ae5cf3-d358-4d99-b900-85902fda9bb0,
5,
'San Jose',
[
{qty:2, name:'mocha', milk_type:'whole', size:'small', price:2.5}
],
'PLACED',
'order is in process'
);
Using Java Driver :
Though you are inserting User define Type(UDT) value, you have create a custom codec or insert value using json
or UDTValue
Here is how you can insert value through UDTValue :
//First get your UserType from cluster object
UserType oderItemType = cluster.getMetadata()
.getKeyspace(session.getLoggedKeyspace())
.getUserType("order_items");
//Now you can create a UDTValue from your UserType order_items and set value
UDTValue oderItem = oderItemType.newValue()
.setInt("qty", 2)
.setString("name", "mocha")
.setString("milk_type", "whole")
.setString("size", "small")
.setDecimal("price", new BigDecimal(2.5));
// Though you define UDT of List Let's create a list and put the value
List<UDTValue> orders = new ArrayList<>();
orders.add(oderItem);
Now you can insert data like below :
//Let your prepared statment be like
PreparedStatement insertorderPstmt = session.prepare("insert into order(order_id,amount,location,items,status,message) values(?, ?, ?, ?, ?, ?)");
//Now you can bind the value and execute query
session.execute(insertorderPstmt.bind(UUID.randomUUID(), new BigDecimal("4"), "Santa Clara", orders, "PLACED", "in process"));
Upvotes: 1