代码之家  ›  专栏  ›  技术社区  ›  jlb333333

使用CouchDB(1.6.1)List函数输出到csv文件

  •  1
  • jlb333333  · 技术社区  · 9 年前

    我试图掌握在CouchDB 1.6.1中使用列表函数将特定字段输出到csv文件的原理/语法。

    我已经设置了一个简单的html输出,这似乎更容易做到,效果很好。

    view函数如下所示:

    function(doc){
    emit({'A':doc.a, 'B':doc.b, 'C':doc.c.d  .....}, null);}
    

    "function(head, req){
    start({'headers': {
    'Content-Type': 'text/html' }});
    send('<html><body><table>');
    send('<tr><th>A</th><th>B</th><th>C</th></tr>');
    while(row=getRow()){
    send(''.concat( '<tr>', '<td>' + toJSON(row.key.A) + '</td>','<td>' + toJSON(row.key.B) + '</td>','<td>' + toJSON(row.key.C) + '</td>', '</tr>' ));}
    send('</table></body></html>');}"
    

    用于同一视图的csv输出的类似列表函数应如下所示:

    "function(head, req){
    start({'headers': { 'Content-Type': 'text/csv' }});
    send('A' +','+ 'B' +','+'C' + '\\n');
    while(row=getRow()){
    send(''.concat( toJSON(row.key.A)  , toJSON(row.key.B) ,  toJSON(row.key.C) ));};}"
    

    这将导致“ "error":"compilation_error","reason":"Expression does not eval to a function ...."

    在某个网站上,csv列表功能的推荐起点如下:

    function (head, req) {
    start({
    
        “headers”: {
    
          “Content-Type”: “text/csv”
    
         }
    
      });
    
    send(‘Username, Name, Email\n’);
    
    while(row = getRow()) {
    
        send(row.value.username+’,’+row.value.email+’,’+row.value.metadata.name+’\n’);
    
      }
    
    } 
    

    我根本无法让这个结构工作。

    如果您能提供正确的语法,我将不胜感激。

    1 回复  |  直到 9 年前
        1
  •  3
  •   jlb333333    9 年前

    我找到了一个指导原则 here 奥利弗·库罗斯基的幻灯片。

    基本原则是:

    "views": {
    "byPrice": {
    "map": "function(doc){emit(doc.price, [doc.make,doc.year]);};"
    }
    }
    "lists": { 
    "csv": "function(head,req){start({'headers':{'Content-Type':'text/csv'}});send('Make'+','+'Year'+','+'Price'+'\\n');while(row=getRow()){send(row.value+','+row.key+'\\n');}};"
    }