场景:今天第一次要用ovsdb-client transact插入一行数据,之前也没接触过数据库,自己琢磨了半天,下面总结几种格式以及写法。
格式一:
先看schema文件里面的LogFetch表结构:
"LogFetch": {
"columns": {
"session_id": {
"type": "string",
"ephemeral": true
},
"filename": {
"type": "string",
"ephemeral": true
},
"target": {
"type": "string",
"ephemeral": true
},
"duration": {
"type": "integer",
"ephemeral": true
},
"status": {
"type": "string",
"ephemeral": true
},
"message": {
"type": "string",
"ephemeral": true
},
"upload_url": {
"type": "string",
"ephemeral": true
}
}
}
-
分析:如上图所示,LogFetch 这个表有七列,类型用 type 字段规定了,ephemeral意思是这些数据是临时的,会被清除。
命令行格式:
ovsdb-client transact '["Open_vSwitch",{"op":"insert","table":"LogFetch","row":{"session_id":"","filename":"","target":"","duration":0,"status":"","mes
sage":"","upload_url":""},"uuid-name":"uuid_logfetch"}]'
标准json格式:
[
"Open_vSwitch",
{
"op":"insert",
"table":"LogFetch",
"row":{
"session_id":"",
"filename":"",
"target":"",
"duration":0,
"status":"",
"message":"",
"upload_url":""
},
"uuid-name":"uuid_logfetch"
}
]
- 分析:op 代表的是操作类型,table 代表我要操作的表的名称,row代表我要插入一行(之前我困惑为什么为什么表结构里面定义的是列,这里写的是行,后来我想到了这是一张表,就懂了),insert 动作需要一个uuid-name代表每一行数据。
格式二:
"Device": {
"columns": {
"log_fetch": {
"type": {
"key": {
"type": "uuid",
"refTable": "LogFetch"
},
"min": 0,
"max": "unlimited"
}
},
"maxRows": 1,
"isRoot": true
}
- 分析:直接看 log_fetch 这列,这列的类型是键值对,键是 uuid,值引用的是 LogFetch 的uuid,min 和 max 代表最多最少可以有几对这样的键值对,maxRows 代表这个表最多只有一行(其实我自己也没明白,为什么最多只有1行,我还能不停的插入数据,如果有大佬看见了可以解释一下吗),isRoot 代表根表(代表根数据,其他表可以根据主键外键等方式与根表相连)
命令行格式:
["Open_vSwitch",{"op":"insert","table":"LogFetch","row":{"session_id":"","filename":"","target":"","duration":0,"status":"","message":"","upload_url":""},"uuid-name":"uuid_logfetch"},{"op":"mutate","table":"Device","where":[],"mutations":[["log_fetch","insert",["set",[["named-uuid","uuid_logfetch"]]]]]}]
标准json格式:
[
"Open_vSwitch",
{
"op":"insert",
"table":"LogFetch",
"row":{
"session_id":"",
"filename":"",
"target":"",
"duration":0,
"status":"",
"message":"",
"upload_url":""
},
"uuid-name":"uuid_logfetch"
},
{
"op":"mutate",
"table":"Device",
"where":[
],
"mutations":[
[
"log_fetch",
"insert",
[
"set",
[
[
"named-uuid",
"uuid_logfetch"
]
]
]
]
]
}
]
- 分析:mutate 代表的是修改(为什么不用"op":"insert"呢,因为我的Device实际上不止一个log_fetch,用 insert 的话就要把所有列都赋值一遍),where 代表的是要修改的行(为空就是所有行),在插入的时候,用set表示插入一组数据,其中 named-uuid 是一个标签(代表transact命令中插入新行获得的uuid),这个 uuid 的值为 uuid_logfetch。