英文:
golang couchbase gocb.RemoveOp - doesnt removes all
问题
我认为我在某个地方犯了一个愚蠢的错误,但是很长时间以来都找不到错误所在 代码很粗糙,我只是在测试一些东西。
它可以删除,但由于某些原因并没有删除所有的文档,我已经重写了逐个删除的方式,这样可以正常工作。
我使用的是Couchbase的官方包 http://github.com/couchbase/gocb
以下是代码:
var items []gocb.BulkOp
myQuery := gocb.NewN1qlQuery([从150万文档中选择283k个文档])
rows, err := myBucket.ExecuteN1qlQuery(myQuery, nil)
checkErr(err)
var idToDelete map[string]interface{}
for rows.Next(&idToDelete) {
items = append(items, &gocb.RemoveOp{Key: idToDelete["id"].(string)})
}
if err := rows.Close(); err != nil {
fmt.Println(err.Error())
}
if err := myBucket.Do(items);err != nil {
fmt.Println(err.Error())
}
这种方式删除了大约70k个文档,我再次运行它,又删除了43k个文档。
然后我只是让它逐个删除,这样它就正常工作了:
//var items []gocb.BulkOp
myQuery := gocb.NewN1qlQuery([从130万文档中选择180k个文档])
rows, err := myBucket.ExecuteN1qlQuery(myQuery, nil)
checkErr(err)
var idToDelete map[string]interface{}
for rows.Next(&idToDelete) {
//items = append(items, &gocb.RemoveOp{Key: idToDelete["id"].(string)})
_, err := myBucket.Remove(idToDelete["id"].(string), 0)
checkErr(err)
}
if err := rows.Close(); err != nil {
fmt.Println(err.Error())
}
//err = myBucket.Do(items)
英文:
I think I did a silly mistake somewhere, but could not figure where for long time already The code is rough, I just testing things.
It deletes, but by some reasons not all documents, I have rewritten to delete it all one by one, and that went OK.
I use official package for Couchbase http://github.com/couchbase/gocb
Here is code:
var items []gocb.BulkOp
myQuery := gocb.NewN1qlQuery([Selecting ~ 283k documents from 1.5mln])
rows, err := myBucket.ExecuteN1qlQuery(myQuery, nil)
checkErr(err)
var idToDelete map[string]interface{}
for rows.Next(&idToDelete) {
items = append(items, &gocb.RemoveOp{Key: idToDelete["id"].(string)})
}
if err := rows.Close(); err != nil {
fmt.Println(err.Error())
}
if err := myBucket.Do(items);err != nil {
fmt.Println(err.Error())
}
This way it deleted ~70k documents, I run it again it got deleted 43k more..
Then I just let it delete one by one, and it worked fine:
//var items []gocb.BulkOp
myQuery := gocb.NewN1qlQuery([Selecting ~ 180k documents from ~1.3mln])
rows, err := myBucket.ExecuteN1qlQuery(myQuery, nil)
checkErr(err)
var idToDelete map[string]interface{}
for rows.Next(&idToDelete) {
//items = append(items, &gocb.RemoveOp{Key: idToDelete["id"].(string)})
_, err := myBucket.Remove(idToDelete["id"].(string), 0)
checkErr(err)
}
if err := rows.Close(); err != nil {
fmt.Println(err.Error())
}
//err = myBucket.Do(items)
答案1
得分: 2
默认情况下,对N1QL的查询使用的一致性级别称为“请求加”。因此,第二次运行查询程序时,将使用在查询时有效的索引更新,而不是考虑所有先前的变更,等待索引更新为止。您可以在Couchbase的开发人员指南中了解更多信息,看起来您需要通过查询的一致性方法将RequestPlus
参数添加到您的myquery中。
这种最终一致性的二级索引和灵活性非常强大,因为它使您作为开发人员能够决定要付出多少一致性的代价,因为索引重新计算是有成本的。
英文:
By default, queries against N1QL use a consistency level called 'request plus'. Thus, your second time running the program to query will use whatever index update is valid at the time of the query, rather than considering all of your previous mutations by waiting until the index is up to date. You can read more about this in Couchbase's Developer Guide and it looks like the you'll want to add the RequestPlus
parameter to your myquery through the consistency method on the query.
This kind of eventually consistent secondary indexing and the flexibility is pretty powerful because it gives you as a developer the ability to decide what level of consistency you want to pay for since index recalculations have a cost.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论