簡體   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