SequoiaDB擴容的方法有不少,本文主要介紹了幾種擴容實踐及代碼樣例。json
創建數據域dom
db.createDomain("domainbefore",["datagroup1","datagroup2","datagroup3"], {AutoSplit:true});
創建集合空間spa
db.createCS("pri5_image",{Domain:"domainbefore"});
創建主表集合排序
db.pri5_image.createCL("atnnotate",{ShardingKey:{"dated":1},IsMainCL:true});
創建子表集合get
db.pri5_image.createCL("atnnotate2016",{ShardingKey:{"name":1},ShardingType:"hash"}); db.pri5_image.createCL("atnnotate2017",{ShardingKey:{"name":1},ShardingType:"hash"});
attach子表集合string
db.pri5_image.atnnotate.attachCL("pri5_image.atnnotate2016",{LowBound:{dated:"2016-01-01"},UpBound:{dated:"2017-01-01"}}); db.pri5_image.atnnotate.attachCL("pri5_image.atnnotate2017",{LowBound:{dated:"2017-01-01"},UpBound:{dated:"2018-01-01"}});
查看主表編目信息hash
> db.snapshot(SDB_SNAP_CATALOG,{"Name":"pri5_image.atnnotate"}); {…省略 "CataInfo": [ { "ID": 1, "SubCLName": "pri5_image.atnnotate2016", "LowBound": {"dated": "2016-01-01"}, "UpBound": {"dated": "2017-01-01"} }, { "ID": 2, "SubCLName": "pri5_image.atnnotate2017", "LowBound": { "dated": "2017-01-01" }, "UpBound": { "dated": "2018-01-01" } } ], "IsMainCL": true, "Name": "pri5_image.atnnotate", "ShardingKey": { "dated": 1 } …省略 } 此時主表包含兩個子表,以dated爲分區鍵
查看子表集合分佈的數據組it
子表pri5_image.atnnotate2017和pri5_image.atnnotate2016數據組信息同樣,在此省略,此外省略了CataInfo的部分。date
> db.snapshot(SDB_SNAP_CATALOG,{"Name":"pri5_image.atnnotate2016"}); {…省略 "CataInfo": [ { "GroupName": "datagroup1", "LowBound": {"": 0}, "UpBound": {"": 1365} }, { "GroupName": "datagroup2", "LowBound": {"": 1365}, "UpBound": {"": 2730} }, { "GroupName": "datagroup3", "LowBound": {"": 2730}, "UpBound": {"": 4096} } ], "MainCLName": "pri5_image.atnnotate", "Name": "pri5_image.atnnotate2016" …省略 }
各導入10000條dated 在2016年和2017年的數據file
查看數據量
> db.pri5_image.atnnotate.count(); 20000 > db.pri5_image.atnnotate2016.count(); 10000 > db.pri5_image.atnnotate2017.count(); 10000
新增數據組datagroup四、datagroup5和datagroup6
添加至原數據域中
db.getDomain("domainbefore").alter({Groups:['datagroup1', 'datagroup2', 'datagroup3', 'datagroup4', 'datagroup5', 'datagroup6']})
新建子表
db.pri5_image.createCL("atnnotate2016_2",{ShardingKey:{"name":1},ShardingType:"hash"}); db.pri5_image.createCL("atnnotate2017_2",{ShardingKey:{"name":1},ShardingType:"hash"});
查看新建子表的編目信息
子表pri5_image.atnnotate2017_2和pri5_image.atnnotate2016_2數據組信息同樣,在此省略
> db.snapshot(SDB_SNAP_CATALOG,{"Name":"pri5_image.atnnotate2016_2"});
{
…省略
"CataInfo": [
{
"GroupName": "datagroup1",
"LowBound": {"": 0},
"UpBound": {"": 682}
},
{
"GroupName": "datagroup2",
"LowBound": {"": 682},
"UpBound": {"": 1364}
},
{
"GroupName": "datagroup3",
"LowBound": {"": 1364},
"UpBound": {"": 2046}
},
{
"GroupName": "datagroup4",
"LowBound": {"": 2046},
"UpBound": {"": 2728}
},
{
"GroupName": "datagroup5",
"LowBound": {"": 2728},
"UpBound": {"": 3410}
},
{
"GroupName": "datagroup6",
"LowBound": {"": 3410},
"UpBound": {"": 4096}
}
],
"AutoSplit": true
}
新的子表創建在了包含新增的數據組的總共6個數據組上。
建立管道文件pri5_image_atnnotate2016.json
mknod pri5_image_atnnotate2016.json p
導出原表pri5_image.atnnotate2016數據,導入到pri5_image.atnnotate2016_2
nohup sdbexprt -s localhost -p 11810 --type json --file ./pri5_image_atnnotate2016.json -c pri5_image -l atnnotate2016 --fields '_id,id,name,doubled,longd,boold,dated' & >/dev/null nohup sdbimprt -s localhost -p 11810 --type json --file ./pri5_image_atnnotate2016.json -c pri5_image -l atnnotate2016_2 --fields '_id oid,id int,name string,doubled double,longd long,boold bool,dated string' & >/dev/null
查看pri5_image.atnnotate2016_2數據量
> db.pri5_image.atnnotate2016_2.count(); 10000
按照id字段排序,各導出1000條pri5_image.atnnotate2016和pri5_image.atnnotate2016_2數據,進行diff,確認導出導入的數據是否一致
sdb "db.pri5_image.atnnotate2016.find().sort({"_id":1}).limit(1000)">>pri5_image_atnnotate2016.rec sdb "db.pri5_image.atnnotate2016_2.find().sort({"_id":1}).limit(1000)">>pri5_image_atnnotate2016_2.rec diff pri5_image_atnnotate2016.rec pri5_image_atnnotate2016_2.rec
detach原子表pri5_image.atnnotate2016,attach 新子表pri5_image.atnnotate2016
編寫腳本delta-t.js,計算detach原表和attach新子表的耗時
var db = new Sdb();
var rdate = new Date();
var begin = rdate.getSeconds()*1000+rdate.getMilliseconds();
db.pri5_image.atnnotate.detachCL("pri5_image.atnnotate2016");
db.pri5_image.atnnotate.attachCL("pri5_image.atnnotate2016_2",{LowBound:{dated:"2016-01-01"},UpBound:{dated:"2017-01-01"}});
var ldate = new Date();
var end = ldate.getSeconds()*1000+ldate.getMilliseconds();
println(end-begin);
執行腳本
sdb –f delta-t.js
過程耗時41毫秒,detachCL舊錶和attachCL新表過程很快,應用幾乎無感
刪除原子表pri5_image.atnnotate2016,釋放空間給後面的子表數據遷移
db.pri5_image.dropCL("pri5_image.atnnotate2016")
進行下一個子表pri5_image.atnnotate2017的擴容,步驟和上述步驟一致,再也不詳述。
和第一種方案環境準備章節一致,在如下部分的擴容步驟和第一種方案不一樣。
新增數據組datagroup四、datagroup5和datagroup6
添加至原數據域中
db.getDomain("domainbefore").alter({Groups:['datagroup1', 'datagroup2', 'datagroup3', 'datagroup4', 'datagroup5', 'datagroup6']})
新建子表
db.pri5_image.createCL("atnnotate2018",{ShardingKey:{"name":1},ShardingType:"hash",AutoSplit:false,Group:"datagroup4"});
查看子表編目信息,省略了部分信息
> db.snapshot(SDB_SNAP_CATALOG,{"Name":"pri5_image.atnnotate2018"}); { …省略 "Name": "pri5_image.atnnotate2018", "CataInfo": [ { "GroupName": "datagroup4", "LowBound": {"": 0}, "UpBound": {"": 4096} } ], "AutoSplit": false }
使用split拆分數據到數據組datagroup5和datagroup上
db.pri5_image.atnnotate2018.split("datagroup4","datagroup6",{"id":2730},{"id":4096}) db.pri5_image.atnnotate2018.split("datagroup4","datagroup5",{"id":1365},{"id":2730})
查看拆分後的子表編目信息
> db.snapshot(SDB_SNAP_CATALOG,{"Name":"pri5_image.atnnotate2018"}); {…省略 "AutoSplit": false, "CataInfo": [ { "GroupName": "datagroup4", "LowBound": {"": 0}, "UpBound": {"": 1365} }, { "GroupName": "datagroup5", "LowBound": {"": 1365}, "UpBound": {"": 2730} }, { "GroupName": "datagroup6", "LowBound": {"": 2730}, "UpBound": {"": 4096} } ], "Name": "pri5_image.atnnotate2018", …省略 } 新增的子表均勻切分到了新增的數據組上。
attach到主表
db.pri5_image.atnnotate.attachCL("pri5_image.atnnotate2018",{LowBound:{dated:"2018-01-01"},UpBound:{dated:"2019-01-01"}});
若是還有更多子表須要添加,重複以上步驟,進行擴容。
和第一種方案環境準備章節一致,在如下部分的擴容步驟和第一種方案不一樣。
新建域
db.createDomain("domainafter",["datagroup4","datagroup5","datagroup6"], {AutoSplit:true});
創建集合空間
db.createCS("pri5_image_2",{Domain:"domainafter"});
建立子表
db.pri5_ image2.createCL("atnnotate2018",{ShardingKey:{"name":1},ShardingType:"hash"});
查看新建子表的編目信息
省略了部分輸出信息
> db.snapshot(SDB_SNAP_CATALOG,{"Name":"pri5_image2.atnnotate2018"}); { "CataInfo": [ { "GroupName": "datagroup4", "LowBound": {"": 0}, "UpBound": {"": 1365} }, { "GroupName": "datagroup5", "LowBound": {"": 1365}, "UpBound": {"": 2730} }, { "GroupName": "datagroup6", "LowBound": {"": 2730}, "UpBound": {"": 4096} } ], "AutoSplit": true }
attach到主表
db.pri5_image.atnnotate.attachCL("pri5_image_2.atnnotate2018",{LowBound:{dated:"2018-01-01"},UpBound:{dated:"2019-01-01"}});
查看主表編目信息
> db.snapshot(SDB_SNAP_CATALOG,{"Name":"pri5_image.atnnotate"}); { "CataInfo": [ { "SubCLName": "pri5_image.atnnotate2017", "LowBound": {"dated": "2017-01-01"}, "UpBound": {"dated": "2018-01-01"} }, { "SubCLName": "pri5_image.atnnotate2016", "LowBound": {"dated": "2016-01-01"}, "UpBound": {"dated": "2017-01-01"} }, { "SubCLName": "pri5_image_2.atnnotate2018", "LowBound": {"dated": "2018-01-01"}, "UpBound": {"dated": "2019-01-01"} } ], "EnsureShardingIndex": true, "IsMainCL": true, "Name": "pri5_image.atnnotate", "ShardingKey": {"dated": 1}, "ShardingType": "range", } 新增的子表均勻切分到了新增的數據組上。
若是還有更多子表須要添加,重複以上步驟,進行擴容。
建立集合空間及集合
>db.createCS("foo").createCL("bar",{ShardingKey:{"id":1},ShardingType:"hash"});
查看編目信息
> db.snapshot(SDB_SNAP_CATALOG,{"Name":"foo.bar"}); { "CataInfo": [ { "GroupName": "datagroup3", "LowBound": {"": 0}, "UpBound": {"": 4096} } ] }
開始切分
> db.foo.bar.split("datagroup3","datagroup1",{"id":0},{"id":2048});
查看編目信息
> db.snapshot(SDB_SNAP_CATALOG,{"Name":"foo.bar"}); { "CataInfo": [ { "GroupName": "datagroup1", "LowBound": {"": 0}, "UpBound": {"": 2048} }, { "GroupName": "datagroup3", "LowBound": {"": 2048}, "UpBound": {"": 4096} } ] } 分區鍵在0-2048範圍內的被切分到了datagroup1上。