通过节点js递归查询多个CSV

时间:2014-01-20 19:40:03

标签: node.js csv file-io

我在writing matched lines with node js in CSV上问了一个问题并得到了满意的答复。现在我正在通过节点进入我的新手冒险的下一级别。我想设置模拟rdb的递归查询,有点(这是我希望节点有类似Perl DBD::CSV的东西。

更新:我觉得我对节点世界中的事物的异步性有所了解,需要了解它是如何工作的。这是我想要做的 -

  1. 查询所有匹配食物的食物文件。
  2. 对于上面#1中的每种匹配食物,找到营养素文件中的所有营养素。
  3. 对于上述#2中的每种匹配营养素,在营养素定义文件中找到它们的定义。
  4. 将整个数据结构作为嵌套的JSON返回并将其交给可视化工具(命令行或html,当我最终将整个鼠标的包裹放入RESTful框架,例如expressjs时。
  5. 是的,如果我将所有内容都放入SQLite,我可以更加轻松地完成这一切,但我是一个痛苦的傻瓜。这将是理解节点如何处理流的好方法。

    到目前为止,这是我的代码

    #!/usr/bin/env node
    
    var Argv = require("optimist").argv,
        Csv = require("csv"),
        Fs = require("fs");
    
    var db = {
        "dir": "/../data/sr26",
        "foods": {
            "file": "FOOD_DES.txt",
            "columns": [
                "NDB_No", "FdGrp_Cd", "Long_Desc", "Shrt_Desc", "ComName",
                "ManufacName", "Survery", "Ref_desc", "Refuse", "SciName",
                "N_Factor", "Pro_Factor", "Fat_Factor", "CHO_Factor"
            ],
            "PK": "NDB_No",
            "search": "Long_Desc"
        },
        "nutrients": {
            "file": "NUT_DATA.txt",
            "columns": [
                "NDB_No", "Nutr_No", "Nutr_Val", "Num_Data_Pts", "Std_Error",
                "Src_Cd", "Deriv_Cd", "Ref_NDB_No", "Add_Nutr_Mark", 
                "Num_Studies", "Min", "Max", "DF", "Low_EB", "Up_EB", 
                "Stat_cmt", "AddMod_Date", "CC"
            ],
            "PK": "NDB_No",
            "search": "NDB_No"
        },
        "nutrient definitions": {
            "file": "NUTR_DEF.txt",
            "columns": [
                "Nutr_No", "Units", "Tagname", "NutrDesc", "Num_Dec", "SR_Order"
            ],
            "PK": "Nutr_No",
            "search": null
        },
        "query": function(table, value, done) {
            var re = new RegExp(value);
    
            Csv()
            .from.stream(
                Fs.createReadStream(__dirname + db.dir + "/" + db[table].file),
                {
                    "delimiter": "^",
                    "quote": "~",
                    "columns": db[table].columns
                }
            )
            .transform(function(row, index, callback) {
                if (row[ db[table].search ].search(re) != -1) {
                    process.nextTick(function() {
                         callback(null, row)
                    });
                }
                else {
                    callback(null, null);
                }
            })
            .on("record", function(row, index) {
                console.log("done is " + done);
                if (done == true) {
                    console.log(JSON.stringify(row));
                }
                else {
                    console.log("found " + table + " '" + value + "' with PK: " + row[ db[table].PK ]);
                    console.log("\tnow querying 'nutrients' for " + row[ db[table].PK ] + "\n");
                    db.query("nutrients", row[ db[table].PK ], true);
                }
            });
        }
    };
    
    if (Argv.table) {
        console.time("searching");
        db.query(Argv.table, Argv.value, false);
        console.timeEnd("searching");
    }
    
    $ bin/app.js --table foods --value "Dandelion green"
    searching: 4ms
    done is false
    found foods 'Dandelion green' with PK: 11207
        now querying 'nutrients' for 11207
    
    done is false
    found foods 'Dandelion green' with PK: 11208
        now querying 'nutrients' for 11208
    
    done is false
    found foods 'Dandelion green' with PK: 11782
        now querying 'nutrients' for 11782
    

    正如你所看到的,运行上面的吐痰,嗯,真的没什么。我期待营养价值在第二个循环中打印出来“(完成是真的)”,但“完成”永远不会设置为 true 。在我更好地学习节点的过程中,我寻求上述的具体和一般指导。

0 个答案:

没有答案