繁体   English   中英

将数据从Cloudant / CouchDB导出到CSV

[英]Exporting data from Cloudant/CouchDB to CSV

从Cloudant导出数据到CSV时出现一些小问题。 当前使用的CSV函数位于以下位置: https : //developer.ibm.com/clouddataservices/2015/09/22/export-cloudant-json-as-csv-rss-or-ical/

问题是后来添加了一些数据,并增加了2-3个字段。 当下载文档时,它只是将信息一个接一个地放置,并且无法解决某些较旧的数据丢失字段,因此数据会对齐。

我尝试创建一些函数,这些函数尝试检测该字段是否存在,以及是否未将其设置为空字符串。

这是我尝试过的错误提示:{“ 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);
 }
};


当遇到不具有所有这些字段的数据时,它将检测到它。 分配一个空字符串,以便在下载csv时对齐数据。

如果要从CouchDB / Cloudant的相对平坦的文档生成CSV文件,则有两个选择:

  1. 使用CouchDB“列表”功能,如您链接的博客文章所述。
  2. 使用实用程序将数据导出为JSON并在客户端转换为CSV

选项2可以通过带有ouchExport配套工具的开源Couchimport实用程序来实现:

数据在命令行上导出,可以通过管道传输到文件中以进行进一步分析。

couchexport --db mydb > mydb.csv

如果文档格式不正确,则可以提供“过滤器”功能以将数据强制转换为正确的格式。

暂无
暂无

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

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