LoginSignup
1
2

More than 3 years have passed since last update.

CloudantのデータをCSVでダウンロードする方法

Posted at

はじめに

IBM Cloud上のCloudantに保存したデータをCSVに変換してダウンロードする方法です。
Cloudantにアクセスして、保存されてるJsonをCSVに変換するプログラムを書くという方法もありますが、ここではCloudantのDesign Document Viewというものを使って、CSVに変換する方法を解説します。

Design Document Viewを使うと、ブラウザーから直接ダウンロードができるようになります。

この内容を元に実施した手順です。
https://developer.ibm.com/clouddataservices/2015/09/22/export-cloudant-json-as-csv-rss-or-ical/

前提

IBM Cloud上にCloudantを作成する手順は含みません。

  • 事前に、「Cloudant-csvtest」というCloudantのインスタンスを作成しています。
    cloudant01.png

  • Cloudant Dashobardから、「csvtest」というDatabaseを作成しています。
    cloudant02.png

サンプルデータ

以下のようなJSONデータをデータベースに保存しています。

{
  "_id": "89a66bae620c33d653a0c02f2add5b4a",
  "_rev": "1-bba9e83dbb192ea0c05e13f0d9296322",
  "product": "Cut",
  "customer": "Lady Catherine de Bourgh",
  "date": "2015-09-29 16:00:00",
  "duration": 1,
  "stylist": "Mandy",
  "branch": "Stockport",
  "cost": 25,
  "paid": false
}

cloudant03.png

Design Document Viewの作成

「Design Documents」から「New View」をクリックします。
cloudant04.png

_design/ と Index name に任意の名前を付けて「Create Document and then Build Index」をクリックします。
※ここで指定する名前は後ほどCSVダウンロードで使います。
cloudant05.jpg

こんな感じになります。
1行目がDesign View、2行目がサンプルデータです。
Design ViewのDocumentをクリックして内容を編集します。
cloudant06.jpg

コードの埋め込み

以下の部分にコードを書いていきます。
cloudant07.jpg

以下のコードをコピーして、

  "lists": {
    "csvtest": "function(head, req) { var row; var first = true; start({ headers: { 'Content-Type': 'text/csv'},}); while(row = getRow()) { var doc = row.doc;  if (first) {  send(Object.keys(doc).join(',') + '\\n'); first = false;} var line = '';  for(var i in doc) { if (line.length > 0) { line += ','; } var val = doc[i]; if (typeof val == 'string' && val.indexOf(',') > -1) { line += '\"' + val.replace(/\"/g, '\"\"') + '\"';  } else { line += val; } } line += '\\n'; send(line); } }"
  },

注意

  • JavaScriptで書く必要があります。
  • 改行やタブを含めることはできません。
  • コード内で必要となる改行はバックスラッシュ(\)でエスケープする必要があります。
  • 全て1行に記述する必要があります。
    そのためとても読みにくい。。。
    手間ですが、予め他のエディタで正常に動くことを確認してからここにコピペすると良いと思います。

こんな感じにそのままペースとし、「Save Changes」をクリックします。
cloudant08.jpg

CSVダウンロード

その後、以下のようなURLにアクセスするとデータがCSVとなってダウンロードされます。

https://f9c48dfb-2248-*************-bluemix.cloudant.com/csvtest/_design/view1/_list/csvtest/new-view1?include_docs=true

f9c48d から始まる値:CloudantのExternal Endpoint(CloudantのURL)
csvtest : データベース名
_desgin:固定
view1:Design view名
_list:固定
csvtest:コピペした時に指定したlists内の名前
new-view1:索引名
include_docs=true:ドキュメントを含めるというオプション

CSV例

_id,_rev,product,customer,date,duration,stylist,branch,cost,paid
e1cd4c4348a57af329e79ec693ff394b,1-bba9e83dbb192ea0c05e13f0d9296322,Cut,Lady Catherine de Bourgh,2015-09-29 16:00:00,1,Mandy,Stockport,25,false

補足

読みにくかったコードを読みやすく整形したのがこちら。

function(head, req) {
  var row;
  var first = true;

  // 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) {
      // comma separator
      if (line.length > 0) {
        line += ',';
      }
      // output the value, ensuring values that themselves
      // contain commas are enclosed in double quotes
      var val = doc[i];
      if (typeof val == 'string' && val.indexOf(',') >  -1) {
        line += '"' + val.replace(/"/g,'""') + '"';
      } else {
        line += val;
      }
    }
    line += 'n';
    // send  the line
    send(line);
  }
};

お断り

このサイトの掲載内容は私自身の見解であり、必ずしも所属会社の立場、戦略、意見を代表するものではありません。 記事は執筆時点の情報を元に書いているため、必ずしも最新情報であるとはかぎりません。 記事の内容の正確性には責任を負いません。自己責任で実行してください。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2