LoginSignup
3
3

More than 3 years have passed since last update.

QuickSightでデータを可視化しつつCognitoのユーザを使って認証する

Posted at

QuickSightを使ってみてどの程度簡単に可視化ができるのかを試しつつ、QuickSightの認証を既存の仕組み(Cognito)を利用して簡略化できないか、という観点で調べてみました。

やりたいこと

  1. どの程度簡単に可視化できるのかを試したい
  2. QuickSightの認証を簡略化したい

どの程度簡単に可視化できるのかを試したい

QuickSight用のデータを用いては意味がないので、Elasitcsearch / Kibana可視化用のサンプルデータをQuickSightに読み込ませてどうなるかを試してみたいと思います。

今回利用したサンプルデータはこちら:

Loading sample data - Elasticsearch -
https://www.elastic.co/guide/en/kibana/current/tutorial-load-dataset.html

こちらにある3つのデータセットをQuickSightで可視化してみました。

  • The complete works of William Shakespeare, suitably parsed into fields
  • A set of randomly generated log files

データの可視化をしてみる

Shakespeare.jsonデータの可視化

データをCSV化する

ダウンロードできるデータは全てJSONデータなので、一旦CSVに変換します。Elasticsearchが読み込める形式になっているので、割と強引に変換をかけます。

必ずインデックス情報が存在するので、一旦削ります。こんな感じのやつを正規表現で削除します。またread_jsonの「orient='records'」で読み込めるように、配列形式に変更します。

shakespeare.json
{"index":{"_index":"shakespeare","_id":0}}
{"type":"act","line_id":1,"play_name":"Henry IV", "speech_number":"","line_number":"","speaker":"","text_entry":"ACT I"}
{"index":{"_index":"shakespeare","_id":1}}
{"type":"scene","line_id":2,"play_name":"Henry IV","speech_number":"","line_number":"","speaker":"","text_entry":"SCENE I. London. The palace."}
{"index":{"_index":"shakespeare","_id":2}}
{"type":"line","line_id":3,"play_name":"Henry IV","speech_number":"","line_number":"","speaker":"","text_entry":"Enter KING HENRY, LORD JOHN OF LANCASTER, the EARL of WESTMORELAND, SIR WALTER BLUNT, and others"}
{"index":{"_index":"shakespeare","_id":3}}

shakespeare_tmp_01.json
[{"type":"act","line_id":1,"play_name":"Henry IV", "speech_number":"","line_number":"","speaker":"","text_entry":"ACT I"},
{"type":"scene","line_id":2,"play_name":"Henry IV","speech_number":"","line_number":"","speaker":"","text_entry":"SCENE I. London. The palace."},
{"type":"line","line_id":3,"play_name":"Henry IV","speech_number":"","line_number":"","speaker":"","text_entry":"Enter KING HENRY, LORD JOHN OF LANCASTER, the EARL of WESTMORELAND, SIR WALTER BLUNT, and others"},
{"type":"line","line_id":4,"play_name":"Henry IV","speech_number":1,"line_number":"1.1.1","speaker":"KING HENRY IV","text_entry":"So shaken as we are, so wan with care,"},

はい、これでPandasで読み込める形式になりました。CSV変換をかけます。

convert.py
import os
import sys

sys.path.append(os.path.join(os.path.abspath(os.path.dirname(__file__)), '../libs'))

import pandas as pd

df = pd.read_json(sys.argv[1], orient='records')

df.to_csv(sys.argv[2], index=False)
shakespeare.csv
line_id,line_number,play_name,speaker,speech_number,text_entry,type
1,,Henry IV,,,ACT I,act
2,,Henry IV,,,SCENE I. London. The palace.,scene
3,,Henry IV,,,"Enter KING HENRY, LORD JOHN OF LANCASTER, the EARL of WESTMORELAND, SIR WALTER BLUNT, and others",line
4,1.1.1,Henry IV,KING HENRY IV,1,"So shaken as we are, so wan with care,",line
5,1.1.2,Henry IV,KING HENRY IV,1,"Find we a time for frighted peace to pant,",line

QuickSightに読み込ませてみる

S3に先ほどのcsvファイルをアップロードして、QuickSightに読み込ませてみます。

QuickSightにはS3の読み込みが可能なように、適当なバケットを作成してファイルをアップロードしました。

スクリーンショット 2019-04-21 16.49.24.png

可視化の際に読み込ませたManifestファイルは以下になります。

{ 
    "fileLocations": [                                                    
          {"URIPrefixes": 
              [
              "https://s3.amazonaws.com/quicksight-sample/shakespeare.csv"
              ]
          }
     ],
     "globalUploadSettings": {
       "format": "CSV",
       "textqualifier": "\"",
       "delimiter": ","
   }
}

読み込ませたら普通にグラフで可視化することはできました。

スクリーンショット 2019-04-21 16.53.08.png
スクリーンショット 2019-04-21 16.53.23.png
スクリーンショット 2019-04-21 22.33.54.png

まずまず簡単なようです。次に時系列データになるであろうログファイルを使って可視化してみます。

logs.jsonlデータの可視化

同様の手順でQuickSightに読み込めるようにします。(省略)

QuickSightに読み込ませてみる

以下の処理を行ってCSVを作成後、Manifestファイルを作成して読み込ませました。

  1. JSONリスト化(同上)
  2. CSV変換(同上)
  3. ヘッダ名から「@」を除く
  4. ダブルクォートのエスケープ
logs.csv
message,tags,timestamp,version,agent,bytes,clientip,extension,geo,headings,host,ip,links,machine,memory,phpmemory,referer,relatedContent,request,response,spaces,url,utc_time,xss
"185.124.182.126 - - [2015-05-18T09:03:25.877Z] \"GET /canhaz/gemini-7.gif HTTP/1.1\" 404 804 \"-\" \"Mozilla/5.0 (X11; Linux x86_64; rv:6.0a1) Gecko/20110421 Firefox/6.0a1\"","['success', 'info']",2015-05-18T09:03:25.877Z,1,Mozilla/5.0 (X11; Linux x86_64; rv:6.0a1) Gecko/20110421 Firefox/6.0a1,804,185.124.182.126,gif,"{'coordinates': {'lat': 36.518375, 'lon': -86.05828083}, 'src': 'PH', 'dest': 'MM', 'srcdest': 'PH:MM'}","['<h3>f-i-j-nl-ng</h5>', 'http://facebook.com/success/lodewijk-van-den-berg']",motion-media.theacademyofperformingartsandscience.org,185.124.182.126,"['daniel-tani@facebook.com', 'http://nytimes.com/security/kathryn-sullivan', 'www.nytimes.com']","{'os': 'win xp', 'ram': 3221225472}",,,http://twitter.com/error/william-shepherd,"[{'url': 'http://www.laweekly.com/news/cbs-crew-rat-fink-2368032', 'og:type': 'article', 'og:title': 'CBS Crew Rat Fink', 'og:description': 'Near a couple of auto body shops (and a sharp new Space Invader mosaic that we&#039;ll post soon) near Temple and Westmoreland is a CBS wall with a nice Rat ...', 'og:url': 'http://www.laweekly.com/news/cbs-crew-rat-fink-2368032', 'article:published_time': '2008-01-14T08:05:26-08:00', 'article:modified_time': '2014-10-28T14:59:52-07:00', 'article:section': 'News', 'article:tag': 'Mark Mauer', 'og:image': 'http://IMAGES1.laweekly.com/imager/cbs-crew-rat-fink/u/original/2430299/img_2049.jpg', 'og:image:height': '360', 'og:image:width': '480', 'og:site_name': 'LA Weekly', 'twitter:title': 'CBS Crew Rat Fink', 'twitter:description': 'Near a couple of auto body shops (and a sharp new Space Invader mosaic that we&#039;ll post soon) near Temple and Westmoreland is a CBS wall with a nice Rat ...', 'twitter:card': 'summary', 'twitter:image': 'http://IMAGES1.laweekly.com/imager/cbs-crew-rat-fink/u/original/2430299/img_2049.jpg', 'twitter:site': '@laweekly'}, {'url': 'http://www.laweekly.com/news/push-and-retna-in-koreatown-2368043', 'og:type': 'article', 'og:title': 'Push and Retna in Koreatown', 'og:description': 'Yeah, I originally had this posted this morning as Push &amp; Ayer - Sorry. It looked like a Retna piece, but I saw the Ayer in there and thought that must ...', 'og:url': 'http://www.laweekly.com/news/push-and-retna-in-koreatown-2368043', 'article:published_time': '2008-01-29T07:28:32-08:00', 'article:modified_time': '2014-10-28T14:59:54-07:00', 'article:section': 'News', 'article:tag': 'Shelley Leopold', 'og:image': 'http://IMAGES1.laweekly.com/imager/push-and-retna-in-koreatown/u/original/2430376/img_3671.jpg', 'og:image:height': '360', 'og:image:width': '480', 'og:site_name': 'LA Weekly', 'twitter:title': 'Push and Retna in Koreatown', 'twitter:description': 'Yeah, I originally had this posted this morning as Push &amp; Ayer - Sorry. It looked like a Retna piece, but I saw the Ayer in there and thought that must ...', 'twitter:card': 'summary', 'twitter:image': 'http://IMAGES1.laweekly.com/imager/push-and-retna-in-koreatown/u/original/2430376/img_3671.jpg', 'twitter:site': '@laweekly'}, {'url': 'http://www.laweekly.com/news/asylm-ruets-pdb-on-santa-monica-2368012', 'og:type': 'article', 'og:title': 'Asylm, Ruets, PDB on Santa Monica', 'og:description': 'Not a new piece, but a well-hidden gem a little south of Santa Monica Blvd. in an alley off of Heliotrope or Edgemont. I&#039;ve been sitting on this for a w...', 'og:url': 'http://www.laweekly.com/news/asylm-ruets-pdb-on-santa-monica-2368012', 'article:published_time': '2008-04-22T15:11:15-07:00', 'article:modified_time': '2014-10-28T14:59:48-07:00', 'article:section': 'News', 'article:tag': 'Culture and Lifestyle', 'og:image': 'http://images1.laweekly.com/imager/asylm-ruets-pdb-on-santa-monica/u/original/2430137/img_5027.jpg', 'og:image:height': '360', 'og:image:width': '480', 'og:site_name': 'LA Weekly', 'twitter:title': 'Asylm, Ruets, PDB on Santa Monica', 'twitter:description': 'Not a new piece, but a well-hidden gem a little south of Santa Monica Blvd. in an alley off of Heliotrope or Edgemont. I&#039;ve been sitting on this for a w...', 'twitter:card': 'summary', 'twitter:image': 'http://images1.laweekly.com/imager/asylm-ruets-pdb-on-santa-monica/u/original/2430137/img_5027.jpg', 'twitter:site': '@laweekly'}, {'url': 'http://www.laweekly.com/news/laurence-tribe-tangles-with-cbs-and-la-city-hall-2396867', 'og:type': 'article', 'og:title': 'Laurence Tribe Tangles with CBS and L.A. City Hall', 'og:description': 'The United States Court of Appeals for the Ninth Circuit&rsquo;s Courtroom 3 - a miniature auditorium with comfortable, smoked salmon-colored seats - wa...', 'og:url': 'http://www.laweekly.com/news/laurence-tribe-tangles-with-cbs-and-la-city-hall-2396867', 'article:published_time': '2008-06-04T14:16:10-07:00', 'article:modified_time': '2014-11-26T14:43:59-08:00', 'article:section': 'News', 'og:site_name': 'LA Weekly', 'twitter:title': 'Laurence Tribe Tangles with CBS and L.A. City Hall', 'twitter:description': 'The United States Court of Appeals for the Ninth Circuit&rsquo;s Courtroom 3 - a miniature auditorium with comfortable, smoked salmon-colored seats - wa...', 'twitter:card': 'summary', 'twitter:site': '@laweekly'}]",/canhaz/gemini-7.gif,404,this   is   a   thing    with lots of     spaces       wwwwoooooo,https://motion-media.theacademyofperformingartsandscience.org/canhaz/gemini-7.gif,2015-05-18 09:03:25.877,"<script>console.log(\"xss\")</script>"
"79.1.14.87 - - [2015-05-18T12:28:25.013Z] \"GET /canhaz/james-mcdivitt.gif HTTP/1.1\" 200 774 \"-\" \"Mozilla/5.0 (X11; Linux i686) AppleWebKit/534.24 (KHTML, like Gecko) Chrome/11.0.696.50 Safari/534.24\"","['success', 'info']",2015-05-18T12:28:25.013Z,1,"Mozilla/5.0 (X11; Linux i686) AppleWebKit/534.24 (KHTML, like Gecko) Chrome/11.0.696.50 Safari/534.24",774,79.1.14.87,gif,"{'coordinates': {'lat': 35.16531472, 'lon': -107.9006142}, 'src': 'GN', 'dest': 'US', 'srcdest': 'GN:US'}","['<h3>charles-bolden</h5>', 'http://www.slate.com/success/barry-wilmore']",motion-media.theacademyofperformingartsandscience.org,79.1.14.87,"['george-nelson@twitter.com', 'http://facebook.com/info/anatoly-solovyev', 'www.www.slate.com']","{'os': 'osx', 'ram': 8589934592}",,,http://www.slate.com/warning/b-alvin-drew,[],/canhaz/james-mcdivitt.gif,200,this   is   a   thing    with lots of     spaces       wwwwoooooo,https://motion-media.theacademyofperformingartsandscience.org/canhaz/james-mcdivitt.gif,2015-05-18 12:28:25.013,"<script>console.log(\"xss\")</script>"

QuickSightに読み込ませたところ、問題なくグラフ化ができました。

スクリーンショット 2019-04-22 0.20.56.png

QuickSightの認証を簡略化、Cognitoと連携させる

可視化まではとても簡単にできましたが、あくまでこれはQuickSightにアクセス時にサインアップしたアカウントで見ることになるので、実際の運用を考えると、Cognitoあたりと連携してくれないと運用が大変そうです。

ということで、Cognitoで連携できそうなこのOSSを利用します。

README.mdがきちんと書かれているので、その通りに実施すればひとまずは問題なさそうです。実際実施してみたところ、問題が特になくCognitoとQuickSightの連携をすることができました。

スクリーンショット 2019-04-22 8.25.57.png

設定をしてログイン画面にアクセスするだけで上記の画面が表示されました。最初のサインアップは必要ですが、その後すぐにログインできるようになりました。

スクリーンショット 2019-04-22 8.31.05.png

どうやらCognitoで連携する用のIAMロールが作成され、そのロールでログイン、アクセスができるようです。IAMのユーザ毎にロールが作成されるかと思いましたが、このOSSを利用した場合はIAMロールは共通のようです。この辺りは権限管理を考えたい場合は独自で実装などするしかなさそうですね。

まとめ

QuickSightは非常に簡単なBIツールでした。各種AWSサービスとも連携が簡単なので、サクッと大量ではないデータで何か分析結果を見せたい場合は便利そうですね。

3
3
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
3
3