【问题标题】:Apigee Usergrid: Mass delete option missingApigee Usergrid:缺少批量删除选项
【发布时间】:2014-10-02 01:46:36
【问题描述】:

我正在使用 usergrid 来存储客户项目的数据。它有两个系列汽车陈列室和汽车。到目前为止我很好。但是我有一个场景,我刷新了收集车的主数据。每次执行此操作时,我都必须删除汽车中的所有现有数据,并将其替换为来自主库存系统的传入汽车数据。

现在,有了https://www.npmjs.org/package/usergrid 中的文档,我发现我一次只能摧毁一辆汽车。

car.destroy(function(err){
    if (err){
        //error - car not deleted
        //winston log - tbd
    } else {
        //success - car deleted
    }
});

这对于较小的陈列室是可以的,但较大的多品牌陈列室有各种各样的汽车 - 有时甚至多达 50 个不同的品种(8 个汽车品牌 * 大约 8 个不同的选项)。

是否有批量删除选项?如果我在这里遗漏了一些东西,有人可以指点我一份文档吗?

附:我是 usergrid 的新手,如果这是一个重复的问题,请标记并指向正确的 url

【问题讨论】:

    标签: javascript node.js apigee usergrid


    【解决方案1】:

    如果您愿意,我已经编写了一个并行运行删除请求的 Node.js 批量删除器。删除 1000 个实体大约需要 3 分钟。

    这是always up-to-date gist,还有一份给 SO:

    // Installation 
    
    // 1. Install Node.js http://nodejs.org/download/
    // 2. In Terminal, cd (navigate) to the directory where you saved this file
    // 3. Run 'npm install request async'
    // 4. Edit the script config below with your token, org, app, and collection name.
    // 5. To run the script, at the Terminal prompt, run 'node api_baas_deleter.js'
    
    // Config
    
    var access_token = "{token}";
    var as_basepath = "http://api.usergrid.com/{org}/{app}/"; // You need the trailing slash!
    var collection = "{collection_name}";
    
    // End Config
    
    var request = require('request');
    var async = require('async');
    
    var authstring = "access_token=" + access_token;
    
    var total = 0;
    var startTime = Date.now();
    
    function deleteRecords(callback) {
        request.get({
            url: as_basepath + collection + "?" + authstring,
            json: true
        }, function(e, r, body) {
            if (body.count === undefined) {
                var err = "Error: invalid endpoint. Check your basepath and collection name.";
                console.log(err);
                if (typeof(callback) === 'function') {
                    callback(err)
                }
            } else {
                // console.log("Found " + body.count + " entities");
                if (body.count > 0) {
                    var deletes = [];
                    for (var i = 0; i < body.count; i++) {
                        deletes.push({
                            url: as_basepath + collection + "/" + body.entities[i].uuid + "?" + authstring,
                            json: true
                        });
                        console.log("Deleting " + body.entities[i].uuid)
                    }
                    async.each(deletes, function(options, callback) {
                        request.del(options, function(e, r, body) {
                            if (r.statusCode === 200) {
                                total++;
                            }
                            callback(e);
                        });
                    }, function(err) {
                        setTimeout(function() {
                            deleteRecords(collection, function(e) {
                                callback(e);
                            });
                        }, 600); // Mandatory, since it seems to not retrieve entities if you make a request in < 600ms
                    });
                } else {
                    var timeInMinutes = minutesFromMs(Date.now() - startTime);
                    console.log("Deleted " + total + " entities in " + timeInMinutes + " minute" + ((timeInMinutes > 1 || timeInMinutes < 1) ? "s" : ""));
                    if (typeof(callback) === 'function') {
                        callback()
                    }
                }
            }
        });
    }
    
    function minutesFromMs(time) {
        return Math.round(((time % 86400000) % 3600000) / 60000).toString();
    }
    
    deleteRecords();
    

    【讨论】:

    • 非常感谢您的出色工作。但我已经使用了其他解决方案。然而,这个解决方案教会了我很多关于节点中我不知道的可能性。谢谢你。不幸的是,我在这个论坛上很穷,我什至不能为你的努力投票/
    【解决方案2】:

    Usergrid Node SDK 中目前没有批量删除功能,但您可以创建一个。这就是我在 Node SDK 中添加猴子补丁删除查询功能的方式:

    Usergrid.client.prototype.delete = function(opts, callback) {
      if (_.isFunction(opts)) { callback = opts; opts = undefined; }
    
      if (!opts.qs.q) { opts.qs.q = '*'; }
    
      var options = {
        method: 'DELETE',
        endpoint: opts.type,
        qs: opts.qs
      };
      var self = this;
      this.request(options, function (err, data) {
        if (err && self.logging) {
          console.log('entities could not be deleted');
        }
        if (typeof(callback) === 'function') {
          callback(err, data);
        }
      });
    };
    

    希望对您有所帮助! 斯科特

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-13
      • 1970-01-01
      相关资源
      最近更新 更多