【问题标题】:how to bulk insert with different key in Cochbase and node js如何在 Cochbase 和节点 js 中使用不同的键批量插入
【发布时间】:2016-11-17 11:46:24
【问题描述】:

我想导入一些更改的 MySQL 数据。像json里面的join和json。 我可以一一插入它。但这需要很多时间。 我通过以下代码来做到这一点。这个代码在for循环中

config.bucket.insert('User|'+page._id, JSON.stringify(page), function(err, res) {

                    if (err) {
                     console.log('User|'+page._id+' operation failed  '+ err);
                      return;
                    }
                    console.log('User|'+page._id);

                });

所以,我需要。 如何在 Cochbase 和 node js 中使用不同的键进行批量插入??

page._id 是不同的键。我在插入 json 中也需要它

【问题讨论】:

    标签: node.js couchbase


    【解决方案1】:

    我很确定这就是您正在寻找的: https://github.com/couchbaselabs/devguide-examples/blob/master/nodejs/bulkES6.js

    function bulkUpdateAsyncPattern(){
      return new Promise(
          (resolve, reject) => {
    
              var completed = 0;
              var runFlag = false;
              var startTime = process.hrtime();
    
              // Function for modify one document, during bulk loop.  Notice,
              // this is only in scope for bulkUpdateAsyncPattern
              function modifyOne() {
    
                  // First Check if the bulk pattern loop is done
                  if (completed >= totalDocs && !runFlag) {
                      runFlag = true;
                      var time = process.hrtime(startTime);
                      console.log("====");
                      console.log("  Bulk Pattern Processing Loop Took: " + parseInt((time[0] * 1000) +
                              (time[1] / 1000000)) + " ms for: " + getMultiArray.length +
                          " items");
                      resolve();
                  } else {
                      if (completed <= totalDocs) {
    
                          // Modify One Document
                          bucket.mutateIn('test' + completed, 0, 0)
                          .counter('rev', 1, false)
                          .execute( function(err, res) {
                              if (err) console.log("  Error modifying:", err.message);
    
                              // This will fire WHEN and only WHEN a callback is received.
                              if (res) {
                                  // Increment completed count
                                  completed++;
    
                                  // Recursive call to modify
                                  modifyOne();
                              }
                          });
                      }
                  }
              }
              // The loop that sets up a "buffer" of queued operations
              // This sets up a number of requests always in the buffer waiting to execute
              for (var i = 0; i < opsGroup; ++i) {
                  modifyOne();
              }
          });
    }
    

    【讨论】:

    • 顺便说一句,这不是 Multiple insert Couchbase with node.js 的重复,因为 API 已更改。
    • 我正在一个一个地做,但如果我的数据超过 15K,这需要时间。花了30分钟。还有其他方法吗? @Roi Katz
    • 你试过批处理吗?
    • 是的,我试试。但我找不到如何作为批处理插入。 @Roi Katz
    • 从 NojeJS SDK 2.x 版开始,不再有 setMulti。您可以做到的唯一方法是收集文档并按照顶部和开发指南示例中的描述触发它。基本上这就是 setMulti 所做的 - 但是它不对应于 sdks 语言的统一。尝试在这里阅读 - developer.couchbase.com/documentation/server/current/sdk/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-25
    • 2014-12-08
    • 2021-05-06
    • 2021-02-21
    • 1970-01-01
    • 2014-04-13
    相关资源
    最近更新 更多