使用CouchDB(1.6.1)List函数输出到csv文件 [英] Using the CouchDB(1.6.1) List function to output to a csv file
问题描述
我正在尝试掌握在CouchDB 1.6.1中使用列表函数将特定字段输出到csv文件的原理/语法.
I am trying to master the principles/syntax of using a list function in CouchDB 1.6.1 to output specific fields to a csv file.
我已经为html设置了一个简单的输出,这似乎更容易实现,并且效果很好.
I have set up a simple output to html and this seems easier to do and it works well.
我想做的就是拥有一个列表函数所需的视图,以从数据库中输出选定的字段并将数据输出到一个csv文件中.
What I want to do is have a view, which the list function requires, to output selected fields from the database and output the data to a csv file.
我似乎无法做的是获取列表函数以从视图输出中读取"特定字段,我成功地这样做了,以获得html输出.
What I can't seem to be able to do is get the list function to 'read' the specific fields from the view output, which I succeded in doing to obtain html output.
视图功能看起来像这样:
The view function looks something like this:
function(doc){
emit({'A':doc.a, 'B':doc.b, 'C':doc.c.d .....}, null);}
html列表函数如下所示:
The html list function would look something like this:
"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输出的类似列表函数应如下所示:
A similar list function for csv output for the same view should look like:
"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函数的多种变体,但都没有成功,只是一堆乱码的文本格式.
I have tried numerous variations of the csv function without success except for a jumbled lot of incorrectly formatted text.
在某些网站上,建议使用csv列表功能的起点是:
A recommended starting point for a csv list function was, on a certain website, given as:
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’);
}
}
我根本无法使这种结构正常工作.
I cannot get this structure to work at all.
对于使用正确语法的一些输入,我将不胜感激.
I would appreciate some input on the correct syntax to use please.
推荐答案
I was able to find a guideline here. A slideshare by Oliver Kurowski.
基本原则是:
"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');}};"
}
效果很好.
谢谢你奥利弗.
这篇关于使用CouchDB(1.6.1)List函数输出到csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!