如何使用golang将这个json(包含菜肴列表)插入到cassandra表中?通过我的实现,我得到了一个 Unmatched column names/values
错误。我想问题出在解组部分,在那里我得到这个订单实体 {1 123 2018-04-09 23:00:00 +0000 UTC [{borsh 1 1} {brokoli 1 2}] cooking}
```
{
"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": "cooking"
}
Cassandra表
create type dish (
name text,
menu_id text,
number int
);
create table orders (
user text,
id text,
created_at timestamp,
dishes list<frozen>,
status text,
primary key(id)
);
订单结构与插入查询实现
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"
}
func saveOrder(mes amqp.Message) error{
var order foodDomain.Order
if err := json.Unmarshal(mes.Body, &order); err != nil {
return err
}
cassandraConn, err := GetCassandraConnection()
if err != nil {
return err
}
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 Emp")
fmt.Println(err)
return err
}
fmt.Println("Successfully added order to cassandra")
return nil
}
暂无答案!
目前还没有任何答案,快来回答吧!