使用gocql向cassandra插入一个切片

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

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

  1. create type dish (
  2. name text,
  3. menu_id text,
  4. number int
  5. );
  6. create table orders (
  7. user text,
  8. id text,
  9. created_at timestamp,
  10. dishes list<frozen<dish>>,
  11. status text,
  12. primary key(id)
  13. );

go结构:

  1. type Order struct {
  2. User string `json:"user"`
  3. Id string `json:"id"`
  4. CreatedAt time.Time `json:"created_at"`
  5. Dishes []Dish `json:"dishes"`
  6. Status string `json:"status"` //pending OR completed
  7. }
  8. type Dish struct {
  9. Name string `json:"name"`
  10. MenuId string `json:"menu_id"`
  11. Number int `json:"number"`
  12. }

json码:

  1. {
  2. "user": "1",
  3. "id": "123",
  4. "created_at": "2018-04-09T23:00:00Z",
  5. "dishes": [
  6. {
  7. "name": "borsh",
  8. "menu_id": "1",
  9. "number": 1
  10. },
  11. {
  12. "name": "brokoli",
  13. "menu_id": "1",
  14. "number": 2
  15. }
  16. ],
  17. "status": "pending"
  18. }

插入:

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

插入后,表如下所示:

  1. 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

暂无答案!

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

相关问题