利用 dashDB 服务用 Ruby 创建商业智能和分析服务
 
- UID
- 1066743
|

利用 dashDB 服务用 Ruby 创建商业智能和分析服务
IBM Bluemix™ 中包含的 服务(以前称为 Analytics Warehouse 服务)为商业智能和分析任务提供了一个功能强大、易于使用和灵活的平台。dashDB 服务是一个企业级托管服务,受到经过内存优化的、列式组织的 BLU Acceleration 数据仓库技术的支持。只需点击几下 Bluemix UI,就可以为您的应用程序创建一个随时可用的商业智能和分析服务。然后,通过一系列的步骤创建一个使用了 dashDB 服务的简单的基于图表的应用程序,并在 Bluemix 上部署它。
开始之前要构建本文中的应用程序,您需要:
- 熟悉 编程语言
- 熟悉以下 Ruby 模块:
- :Ruby 的 Web 应用程序库
- :针对 IBM Data Servers 的 Ruby driver/Rails 适配器
- :一个用于封装 Google Chart API 的简单的 Ruby 包装器
- 对使用 cf cloud foundry 命令行工具非常熟悉
循序渐进地构建一个使用了 服务的基于 Ruby 的 Sinatra 应用程序,并在 Bluemix 上部署它。
第 1 步. 创建一个基本的 Sinatra 应用程序- 为您的应用程序创建一个 Gemfile,它列出了该应用程序的必要 gems:
1
2
3
4
5
| source 'https://rubygems.org'
gem 'sinatra'
gem 'ibm_db'
gem 'googlecharts'
gem 'rack'
|
- 安装依赖项:
- 使用 get '/' 方法创建一个简单的 Sinatra 应用程序,以呈现索引页面。让我们将该文件命名为 bluaccl.rb。在用户查看 root 页面时会调用该方法。
1
2
3
4
5
6
7
8
9
10
11
| require 'rubygems'
require 'sinatra'
require 'ibm_db'
require 'json'
require 'gchart'
Tilt.register Tilt::ERBTemplate, 'html.erb' #Set template engine as erb for Sinatra
get '/' do
erb :index
end
|
- 在 views 文件夹下,创建一个包含通过 get '/' 方法呈现的问候消息的 index.html.erb 文件:
1
2
3
| <html>
<h1>Hello Sinatra</h1>
</html>
|
- 创建一个名为 config.ru 的 rackup 文件:
1
2
| require './bluaccl'
run Sinatra::Application
|
- 使用以下命令运行您的应用程序:
转到链接 http://localhost:9292 并查看您的 Sinatra 应用程序。
 第 2. 查询 dashDB 服务现在,您已经准备好使用 ibm_db gem 从您的应用程序访问 dashDB 服务,然后对它执行查询。
- 使用 ibm_db gem 访问 dashDB 服务。当您的应用程序在 Bluemix 上运行时,从 VCAP_SERVICES 环境变量中检索数据库凭证。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| #Parse VCAP_SERVICES to get Analytics Warehouse Service credentials
if(ENV['VCAP_SERVICES'])
# we are running inside PaaS, access database details from VCAP_Services
$db = JSON.parse(ENV['VCAP_SERVICES'])["AnalyticsWarehouse"]
$credentials = $db.first["credentials"]
$host = $credentials["host"]
$username = $credentials["username"]
$password = $credentials["password"]
$database = $credentials["db"]
$port = $credentials["port"]
else
# we are running local, provide local DB credentials
$host = "localhost"
$username = "bludbuser"
$password = "password"
$database = "BLUDB"
$port = 50000
end
|
- 连接到 dashDB 服务,执行查询,并使用 ibm_db gem 的 API 处理结果集。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| require 'ibm_db'
def getDataFromDW
#Connect to database using parsed credentials
conn = IBM_DB.connect "DATABASE=#{$database};HOSTNAME=#{$host} ORT=#{$port} ROTOCOL=TCPIP;UID=#{$username} WD=#{$password};", '', ''
#Run the analytic SQL
stmt = IBM_DB.exec conn, $profitAnalysisSQL
data = {}
while(res = IBM_DB.fetch_assoc stmt)
if data.has_key?(res['PRODUCT'])
data[res['PRODUCT']][res['YEAR']] = res['PROFIT']
else
profit = {}
profit[res['YEAR']] = res['PROFIT']
data[res['PRODUCT']] = profit
end
end
IBM_DB.close conn
return data
end
|
第 3 步. 使用 Googlecharts 创建一个条形图接下来,我们将描述如何基于从 服务中检索的数据,通过使用 来绘制一个条形图。Google Charts 提供了各种选项来生成多种类型的图表,比如折线图、柱状图或饼图。
此代码示例使用了Ruby 模块(一个封装 Google Chart API 的简单包装器),为我们从数据库中检索的数据绘制了一个条形图。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
| def renderBarGraph data
array2011 = [] #Array group that contains profits for Brands respectively for year 2011
array2012 = []
array2013 = []
productNames = []
#Render a Bar chart that shows profits of each Product Brand in comparison year-to-year
data.each do |product,profitHash|
productNames << product
profitHash.each do |year, profit|
if(year == 2011)
array2011 << profit
elsif (year == 2012)
array2012 << profit
else
array2013 << profit
end
if(profit > max)
max = profit
end
end
end
#Render the Bar chart using the gchart library and return the img html tag for display
Gchart.bar(
:title => " rofit by Product Brand",
:data => [array2011, array2012, array2013],
:background => 'efefef', :chart_background => 'CCCCCC',
:bar_colors => '0000DD,00AA00,EE00EE',
:stacked => false,
:size => '600x400',
:bar_width_and_spacing => '15,0,30',
:legend => ['2011', '2012','2013'],
:axis_with_labels => 'x,y',
:axis_labels => [productNames.join('|'), [0,(max/2).to_f,max.to_f].join('|')],
#:format => 'file', :filename => 'custom_filename.png') #To save to a file
:format => 'image_tag',:alt => " rofit by brand img") #To be rendered as an image on web page
end
|
第 4 步. 信息汇总现在,您需要将前面各小节中的代码示例汇总成一个功能性的应用程序。
首先,将前面各小节中的代码示例放入一个名为 bluaccl.rb 的文件中,并配置应用程序的 'get' 方法来调用 getDataFromDW 和 renderBarGraph 函数。
接下来,修改 index.html.erb 文件,以便提供有关应用程序的信息和一个执行查询,并显示图表的按钮。
在 DevOps Services (JazzHub) 存储库中访问这些步骤的代码。
完成这些步骤后,我们已经准备好在 Bluemix 上部署应用程序。
第 5 步. 在 Bluemix 上部署应用程序- 登录到 。在 Bluemix 的 Catalog 选项卡上,选择 Ruby Sinatra 运行时来创建一个 Ruby Sinatra 应用程序:
 - 在 Create Application 表单中指定 AppName。例如,bluaccel
- 将 dashDB 服务的一个实例绑定到应用程序。 在 Bluemix 的 Dashboard 选项卡中单击该应用程序并选择 Add a new service。在服务集中,选择 dashDB 服务,并将它添加到应用程序:
 - 将应用程序部署到 Bluemix.
- 访问应用程序的路由来查看正在运行的应用程序。 单击应用程序的左侧导航窗口上的 Overview:
 - 要在新的选项卡或窗口中启动应用程序,请单击应用程序的超级链接并查看索引页面:
 - 让我们来看一下我们的应用程序中的报表。单击 Show profit by Product 来查看 Profit by Product Brand 条形图:
 结束语在这个示例应用程序,您学习了如何轻松地在 Bluemix 上访问企业级 dashDB 服务。只需几次点击,您就可以部署一个使用了 dashDB 功能的应用程序。这种很容易快速上手的 Ruby 应用程序将会响应 Web 请求,并使用 ibm_db 和 Googlecharts 模块来呈现从数据仓库挖掘的结果。 |
|
|
|
|
|