Go, Cassandra(cql) & UDT

602 views
Skip to first unread message

Baxter

unread,
May 22, 2015, 9:19:55 AM5/22/15
to golan...@googlegroups.com
Hello @all,

i testing in the moment the cqlgo package and have a problem to insert data for a udt column.

Maybe, it´s here a person that have a solution, i hope...

I will insert following Data into the column: {name: "test1, available: 1}, {name: "test2, available: 2}, {name: "test3, available: 3}

for this a a create a struct and try this with following call:


type Rooms struct {
        name        string  `cql:"name"`
        available   int     `cql:"available"`
}
    
var rooms []Rooms
    
rooms = append(rooms, Rooms{name:"ground", available:grundflaeche})
rooms = append(rooms, Rooms{name:"coveredarea", available:uflaeche})
rooms = append(rooms, Rooms{name:"usablearea", available:nutzflaeche})
rooms = append(rooms, Rooms{name:"agricultural", available:lnutzflaeche})
rooms = append(rooms, Rooms{name:"livingspace", available:wohnflaeche})


if i call a insert statement in  become the error:

cannot marshal []main.Rooms into immodb.rooms{name=varchar,available=int}

 i had testing a lot, but nothing works;-(

Thanks
Baxter

Ian Davis

unread,
May 22, 2015, 9:25:01 AM5/22/15
to golan...@googlegroups.com
 
On Fri, May 22, 2015, at 01:02 PM, Baxter wrote:
cannot marshal []main.Rooms into immodb.rooms{name=varchar,available=int}
 
 i had testing a lot, but nothing works;-(
 
Have you tried making name and available exported fields on your Rooms type? Reflection based marshalling requires fields to be exported.
 
Ian

Baxter

unread,
May 22, 2015, 9:36:41 AM5/22/15
to golan...@googlegroups.com
Thank, for your reply..
 
I had this on cassandra side:

CREATE TYPE rooms (
    name text,
    available int
); 

CREATE TABLE test (
id uuid PRIMARY KEY,
rooms frozen<rooms>,
);

CREATE INDEX test_rooms_idx ON test (rooms);

Konstantin Khomoutov

unread,
May 22, 2015, 12:53:48 PM5/22/15
to Baxter, golan...@googlegroups.com
On Fri, 22 May 2015 06:36:41 -0700 (PDT)
Baxter <baxte...@googlemail.com> wrote:

> Thank, for your reply..
[...]

Ian rather meant Go's rules for "exported" (visible in places other
than the current package) identifiers [1]. In other words, capitalize
the first letters of the names of the fields of your struct and see if
that works.

I would also advise you to start small and try reading [1] as a whole
and try working through a bunch of tutorials to understand the basic
rules of the language.

On a side note, if the essense of a reply wasn't clear to you, you
should have better stated it loud and clear -- there's nothing wrong
with admitting you didn't understand something.

1. https://golang.org/doc/effective_go.html#names

Konstantin Khomoutov

unread,
May 22, 2015, 1:08:21 PM5/22/15
to Baxter, golan...@googlegroups.com
On Fri, 22 May 2015 06:36:41 -0700 (PDT)
Baxter <baxte...@googlemail.com> wrote:

> Thank, for your reply..
[...]

Consider also reading [1], especially the part about decoding -- it
explains basic rules most Go packages which make use of
reflection [2, 3] appy when deciding what data to serialize into which
struct fields, and back.

1. http://blog.golang.org/json-and-go
2. http://golang.org/pkg/reflect/
3. http://blog.golang.org/laws-of-reflection

Baxter

unread,
May 24, 2015, 3:17:39 PM5/24/15
to golan...@googlegroups.com, baxte...@googlemail.com
Ok, i had read a lot of stuff on the weekend but i´m only one step forward....


So, i can add one dataset into table but not more... For example, this works:


type RoomsItem struct {
    Room  string `cql:"room"`
    Available int `cql:"available"`
}

func main() {
  
    srv, err := newCqlSession()
    if err != nil {
        log.Fatal(err)
    }

    rooms := &RoomsItem{
        Room:  "main",
        Available: 1,
        
    }

    err = srv.Query("INSERT INTO objects(id, rooms) VALUES(?, ?)", gocql.TimeUUID(), rooms).Exec()
    if err != nil {
log.Fatal(err)

    }
}

With this example i had insert "{"room":"main", "Available": 1}" to the table, fine... so but i will add more than one;-) ( {"room":"main", "Available": 1},{"room":"bla", "Available": 5}....)

for this, i had change to this:

type RoomsItem struct {
    Room  string `cql:"room"`
    Available int `cql:"available"`
}

type Rooms struct {
    Items []RoomsItem
}

func main() {
  
    srv, err := newCqlSession()
    if err != nil {
        log.Fatal(err)
    }
    
    items := []RoomsItem{}
    rooms := Rooms{items}
    
    rooms.AddItem(RoomsItem{Room:"ground", Available:1})
    
    err = srv.Query("INSERT INTO objects(id, rooms) VALUES(?, ?)", gocql.TimeUUID(), rooms).Exec()
    if err != nil {
log.Fatal(err)
}
    
}

func (room *Rooms) AddItem(item RoomsItem) []RoomsItem {
room.Items = append(room.Items, item)
return room.Items
}

And now. i become the failure "cannot marshal main.Rooms into immodb.objects{room=varchar,available=int}"???

I´m really confussed;-))

chris.b...@swiftkey.com

unread,
Jun 12, 2015, 9:33:11 AM6/12/15
to golan...@googlegroups.com
Hi, it looks like from your last example that you are trying to insert a list of rooms into a single rooms column which the driver doesn't know how to convert to the wire format. Instead try using a batch or executing each query one by one for each row.
Reply all
Reply to author
Forward
0 new messages