2

I have a UDT in Cassandra and I have a table that has an array of these UDTs in its schema. Here's a sample:

CREATE TYPE keyspace.test_type(
    x float, 
    y float)

In my schema I have

 CREATE TABLE test_table(
     key text,
     test_array list<FROZEN <test_type>>,
     PRIMARY KEY (key))

Inside my go package I built a struct:

type Test_type struct{
     x float32
     y float32
}

Then I have a function that essentially returns a list of this test_type: []test_type, let's call it my_array.

When I try to do the insert using gocql like such:

 err := gocql.Session.Query('INSERT into test_table (key,test_array)
                             VALUES (?,?)', 'key', my_array).Exec()

I get a bunch of null values instead of my array. Essentially the test_type does not correctly map to the UDT that I created is my assumption.

Essentially my question is how do you map a struct in go to a udt such that the type is recognized properly. .

h94
  • 155
  • 8

2 Answers2

1

Maybe it isn't important, but I have read on https://docs.datastax.com/en/cql/3.3/cql/cql_reference/collection_type_r.html that it isn't

CREATE TABLE test_table( key text, test_array list<FROZEN test_type>>, PRIMARY KEY (key))

but

CREATE TABLE test_table( key text, test_array list<FROZEN <test_type>>, PRIMARY KEY (key))

the correct definition of the table. I haven't the way to try your code, tell me if it change something.

An other thing that I have read is in Update a collection type of a custom type in cassandra I think you should define your custom type in Cassandra, because you have a list of custom type.

falberto89
  • 163
  • 9
  • Hey, you are correct..This was a syntax error on my part. Take a look at the answer that I posted. The change I suggest there correctly wrote the data into Cassandra :) – h94 Oct 16 '18 at 23:12
  • I have seen. My doubt now is about the definition of the type. Why have you declared test_type with x and y as float in Cassandra? Aren't they maps in Golang? – falberto89 Oct 16 '18 at 23:21
  • You can do that as well. However, in my use case I wanted to represent it as a struct instead of a map. – h94 Oct 17 '18 at 02:59
0

Per: Converting Go struct to JSON

The problem was resolved by renaming the fields so it exports. That way gocql can see the data type x & y.

Simply renaming xto X and y to Y. Will do the trick

h94
  • 155
  • 8