使用gocql向cassandra插入一个切片

ztyzrc3y  于 2021-06-09  发布在  Cassandra
关注(0)|答案(0)|浏览(267)

我有一张表,上面有一个udt的列表。当我尝试插入数据时,表中的所有字段都已正确填充,但列表中只包含空值。
类型和表格:

create type dish (
    name text, 
    menu_id text, 
    number int
);

create table orders (
    user text, 
    id text, 
    created_at timestamp, 
    dishes list<frozen<dish>>, 
    status text,
    primary key(id)
);

go结构:

type Order struct {
    User      string    `json:"user"`
    Id        string    `json:"id"`
    CreatedAt time.Time `json:"created_at"`
    Dishes    []Dish    `json:"dishes"`
    Status    string    `json:"status"` //pending OR completed
}

type Dish struct {
    Name   string `json:"name"`
    MenuId string `json:"menu_id"`
    Number int    `json:"number"`
}

json码:

{
    "user": "1",
    "id": "123",
    "created_at": "2018-04-09T23:00:00Z",
    "dishes": [
        {
            "name": "borsh",
            "menu_id": "1",
            "number": 1
        },
        {
            "name": "brokoli",
            "menu_id": "1",
            "number": 2
        }
    ],
    "status": "pending"
}

插入:

if err := cassandraConn.Query("INSERT INTO orders(id, created_at, dishes, status, user) VALUES(?, ?, ?, ?, ?)",
        order.Id, order.CreatedAt, order.Dishes, order.Status, order.User).Exec(); err != nil {
        fmt.Println("Error while inserting order:")
        return err
}

插入后,表如下所示:

123 | 2018-04-09 23:00:00.000000+0000 | [{name: null, menu_id: null, number: null}, {name: null, menu_id: null, number: null}] | pending |    1

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题