简体   繁体   English

将数据从Cloudant / CouchDB导出到CSV

[英]Exporting data from Cloudant/CouchDB to CSV

Having some minor issues exporting data to CSV from Cloudant. 从Cloudant导出数据到CSV时出现一些小问题。 Currently using the CSV function found here: https://developer.ibm.com/clouddataservices/2015/09/22/export-cloudant-json-as-csv-rss-or-ical/ 当前使用的CSV函数位于以下位置: https : //developer.ibm.com/clouddataservices/2015/09/22/export-cloudant-json-as-csv-rss-or-ical/

The issue is some data was added later with 2-3 more fields. 问题是后来添加了一些数据,并增加了2-3个字段。 When this downloads the documents it just places the information one right after the other and it can't account for some older data missing fields so data gets misaligned. 当下载文档时,它只是将信息一个接一个地放置,并且无法解决某些较旧的数据丢失字段,因此数据会对齐。

I've tried creating functions that try and detect if the field exists, and if it doesn't set it to an empty string. 我尝试创建一些函数,这些函数尝试检测该字段是否存在,以及是否未将其设置为空字符串。

Here's what I have tried which gives me the error: {"error":"compilation_error","reason":"Expression does not eval to a function. 这是我尝试过的错误提示:{“ error”:“ compilation_error”,“ reason”:“ Expression不等同于函数。


 // output HTTP headers
 start({
   headers: {  'Content-Type': 'text/csv'  },
 });


 // iterate through the result set
 while(row = getRow()) {

   // get the doc (include_docs=true)
   var doc = row.doc;

   // if this is the first row
   if (first) {

     // output column headers
     send(Object.keys(doc).join(',') + 'n');
     first = false;
   }

   // build up a line of output
   var line = '';

   // iterate through each row
   //for(var i in doc) {

     if (doc.hasOwnProperty('customerNumber')) {
     } else {
       doc.customerNumber = '';
     }

     if (doc.hasOwnProperty('affiliateNumber')) {
     } else {
       doc.affiliateNumber = '';
     }

     if (doc.hasOwnProperty('dunsNumber')) {
     } else {
       doc.dunsNumber = '';
     }

     if (doc.hasOwnProperty('dunsDomestic')) {
     } else {
       doc.dunsDomestic = '';
     }

     if (doc.hasOwnProperty('dunsGlobal')) {
     } else {
       doc.dunsGlobal = '';
     }

     if (doc.hasOwnProperty('countryCode')) {
     } else {
       doc.countryCode = '';
     }

     if (doc.hasOwnProperty('companyName')) {
     } else {
       doc.companyName = '';
     }

     if (doc.hasOwnProperty('countryPreapprovedAmt')) {
     } else {
       doc.countryPreapprovedAmt = '';
     }

     if (doc.hasOwnProperty('preapprovedAmt')) {
     } else {
       doc.preapprovedAmt = '';
     }

     if (doc.hasOwnProperty('currency')) {
     } else {
       doc.currency = '';
     }

     if (doc.hasOwnProperty('expirationDate')) {
     } else {
       doc.expirationDate = '';
     }

     if (doc.hasOwnProperty('blacklist')) {
     } else {
       doc.blacklist = '';
     }

     line += doc.customerNumber + ',' + doc.affiliateNumber + ',' + doc.dunsNumber+ ',' + doc.dunsDomestic+ ',' + doc.dunsGlobal+ ',' + doc.countryCode+ ',' + doc.companyName+ ',' + doc.countryPreapprovedAmt+ ',' + doc.preapprovedAmt+ ',' + doc.currency+ ',' + doc.expirationDate+ ',' + doc.blacklist;
   //}
   line += 'n';

   // send  the line
   send(line);
 }
};


When it comes across a piece of data that doesn't have all of those fields, it will detect it. 当遇到不具有所有这些字段的数据时,它将检测到它。 Assign an empty string so the data aligns when downloading the csv. 分配一个空字符串,以便在下载csv时对齐数据。

If you want to generate CSV files from relatively flat documents from CouchDB/Cloudant, you have two options: 如果要从CouchDB / Cloudant的相对平坦的文档生成CSV文件,则有两个选择:

  1. Using CouchDB "list" functions, as described in the blog post you linked. 使用CouchDB“列表”功能,如您链接的博客文章所述。
  2. Use a utility to export the data as JSON and convert to CSV on the client side 使用实用程序将数据导出为JSON并在客户端转换为CSV

Option 2 can be facilitated by the open-source couchimport utility which comes with a couchexport companion tool: 选项2可以通过带有ouchExport配套工具的开源Couchimport实用程序来实现:

Data is exported on the command line and can be piped to a file for further analysis. 数据在命令行上导出,可以通过管道传输到文件中以进行进一步分析。

couchexport --db mydb > mydb.csv

If the documents are not in the correct format a "filter" function can be provided to coerce the data into the correct form. 如果文档格式不正确,则可以提供“过滤器”功能以将数据强制转换为正确的格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM