在 Bluemix 上用 Ruby 创建一个具有 BLU Acceleration 的商业智能和分析服务
- UID
- 1066743
|
在 Bluemix 上用 Ruby 创建一个具有 BLU Acceleration 的商业智能和分析服务
简介IBM Bluemix™ 中提供的 BLU Acceleration Service 服务为商业智能和分析提供了一个强大的、易于使用的、敏捷的平台。BLU Acceleration Service 是一个企业级的托管服务,受到内存中优化的、按列组织的 BLU Acceleration 数据仓库技术的支持。只需在 Bluemix UI 上单击几下,就可以为您的应用程序创建一个随时可用的商业智能和分析服务。然后,通过一系列的步骤,我们可以创建一个使用 BLU Acceleration 服务的基于图表的简单应用程序,并在 Bluemix 上部署它。
开始之前为了构建本文中的应用程序,您需要:
- 熟悉 编程语言
- 熟悉以下 Ruby 模块:
- :Ruby Web 应用程序库
- :IBM 数据服务器的 Ruby 驱动程序/导轨适配器
- :基于 Google Chart API 的一个简单的 Ruby 包装程序
- 熟悉使用 Cloud Foundry 命令行工具 cf
在具体的过程中,您要在 Bluemix 上构建和部署一个基于 Ruby 的 Sinatra 应用程序,该应用程序使用了 BLU Acceleration 服务。
步骤 1. 创建一个基本的 Sinatra 应用程序- 为您的应用程序创建一个 Gemfile,列出这个应用程序所需的 gem:
1
2
3
4
5
| source 'https://rubygems.org'
gem 'sinatra'
gem 'ibm_db'
gem 'googlecharts'
gem 'rack'
|
- 安装依赖关系:
- 使用 get '/' 方法创建一个简单的 Sinatra 应用程序,用它来渲染 index 页面。我们将该文件命名为 bluaccl.rb。 在用户查看根页面时调用此方法。
1
2
3
4
5
6
7
8
9
| 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 文件夹下,创建一个 index.html.erb 文件,该文件包含一个通过 get '/' 方法渲染的问候信息:
1
2
3
| <html>
<h1>Hello Sinatra</h1>
</html>
|
- 创建一个 rackup 文件,将它命名为 config.ru:
1
2
| require './bluaccl'
run Sinatra::Application
|
- 使用以下命令运行您的应用程序:
访问链接 http://localhost:9292 并查看您的 Sinatra 应用程序。
步骤 2.对 BLU Acceleration 服务发出查询现在,您可以使用 ibm_db gem 从您的应用程序访问 BLU Acceleration 服务,然后对它运行查询。
- 使用 ibm_db gem 访问 BLU Acceleration 服务。当您的应用程序在 Bluemix 上运行时,从 VCAP_SERVICES 环境变量检索数据库凭据。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| #Parse VCAP_SERVICES to get BluAcceleration Service credentials
if(ENV['VCAP_SERVICES'])
# we are running inside PaaS, access database details from VCAP_Services
$db = JSON.parse(ENV['VCAP_SERVICES'])["BLUAcceleration-10.5.3 Rev. A Build 0.1"]
$credentials = $db.first["credentials"]
$host = $credentials["host"]
$username = $credentials["username"]
$password = $credentials["password"]
$database = "SAMPLEDB" #use $credentials["database"] to point to your Database from VCAP_SERVICES.
# In this example we are going against the BLUAcceleration SAMPLEDB database
$port = $credentials["port"]
else
# we are running local, provide local DB credentials
$host = "localhost"
$username = "bludbuser"
$password = "password"
$database = "SAMPLEDB"
$port = 50000
end
|
- 连接到 BLU Acceleration 服务,执行查询,并使用 ibm_db gem 的 API 处理结果集。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| 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 从 BLU Acceleration 检索的 来绘制条形图。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
| 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 文件,提供关于应用程序的信息,并提供一个用来执行查询并显示图表的按钮。
在 JazzHub 存储库中访问这些步骤的代码。
完成这些步骤后,我们已经准备好在 Bluemix 上部署该应用程序了。
步骤 5. 在 Bluemix 上部署应用程序- 登录到 。在 Bluemix 的 Catalog 选项卡上,选中 Ruby Sinatra 运行时,以便创建一个 Ruby Sinatra 应用程序:
- 在 Create Application 表单中指定 AppName。例如,bluaccel
- 将 BLU Acceleration 服务的实例绑定到该应用程序。 在 Bluemix 的 Dashboard 选项卡上单击该应用程序,然后选中 Add a new service。 从 Services 集合中选中 BLUAcceleration 服务,并将它添加到应用程序:
- 将应用程序部署到 Bluemix
- 访问您的应用程序的路径,查看正在运行的应用程序。 单击您的应用程序的左侧导航窗格中的 Overview:
- 要在新选项卡或窗口中启动应用程序,请单击您的应用程序的超链接,并查看 index 页面:
- 让我们来看看应用程序中的报告。单击 Show profit by Product 查看 Profit by Product Brand 条形图:
结束语在这个示例应用程序中,您学习了如何轻松地访问 Bluemix 上的企业级 BLU Acceleration 服务。只需单击几下,就可以部署一个使用 BLU Acceleration 数据仓库特性的应用程序。 这个快捷的 Ruby 应用程序会响应 Web 请求,并使用 ibm_db 和 Googlecharts 模块显示从数据仓库中挖掘的数据。 |
|
|
|
|
|