0

大きな XML ファイル (サイズは約 25 MB) を処理し、データをドキュメントに整理して MongoDB にインポートする必要があります。

問題は、xml ドキュメントに約 5 ~ 6 種類の要素があり、それぞれに約 10,000 行あることです。

タイプ a の xml ノードを 1 つフェッチした後、タイプ b、c、d などの対応する要素をフェッチする必要があります。

ノードでやろうとしていること:

  1. タイプ a のすべての行をフェッチします。
  2. 行ごとに、xpath を使用して対応する関連行を見つけ、ドキュメントを作成します。
  3. mongodb にドキュメントを挿入する

タイプ a の行が 10k 行ある場合、2 番目のステップは 10k 回実行されます。物事が永遠にかからないように、これを並行して実行しようとしています。したがって、async.forEach は完璧なソリューションのように見えました。

async.forEach(rowsA,fetchA);

私の fetchrelations 関数はこのようなものです

var fetchA = function(rowA) {
//covert the xml row into an object 
    var obj = {};
    for(i in rowA.attributes) {
    attribute = rowA.attributes[i];
    if(attribute.value === undefined) 
        continue;
    obj[attribute.name] = attribute.value;
    }
    console.log(obj.someattribute);
    //first other related rows, 
    //callback inserts the modified object with the subdocuments
    findRelations(obj,function(obj){
        insertA(obj,postInsert);
    });
};

これを実行しようとすると、コード内の console.log は 1.5 秒ごとに約 1 回しか実行されず、期待どおりにすべての行に対して並列に実行されません。過去 2 時間、頭を悩ませ、これを理解しようとしてきましたが、何が間違っているのかわかりません。

私はノードにあまり慣れていないので、しばらくお待ちください。

4

1 に答える 1

1

async がイテレータ関数 ( ) に渡すコールバック関数を宣言して呼び出していないように見えますfetchA例については、 forEach のドキュメントを参照してください。

あなたのコードはおそらくもっと似ている必要があります...

var fetchA = function(rowA, cb) {
//covert the xml row into an object 
    var obj = {};
    for(i in rowA.attributes) {
    attribute = rowA.attributes[i];
    if(attribute.value === undefined) 
        cb();
    obj[attribute.name] = attribute.value;
    }
    console.log(obj.someattribute);
    //first other related rows, 
    //callback inserts the modified object with the subdocuments
    findRelations(obj,function(obj){
        insertA(obj,postInsert);
        cb();  // You may even need to call this within insertA or portInsert if those are asynchronous functions.
    });
};
于 2013-01-04T15:13:42.850 に答える