Date post: | 04-Jun-2018 |
Category: |
Documents |
Upload: | thinh-tran-van |
View: | 215 times |
Download: | 0 times |
of 33
8/13/2019 462616 ch14_ch14d
1/33
8/13/2019 462616 ch14_ch14d
2/33
special features of %&' %erver data mining that you can use to e!ploit data
mining programming to the fullest. The sample code, along with versions in
isual 5 .3T, is available at wiley.com6tang6hapter14.
*n this chapter, you learn about78 A9*s and their application to data mining
8 :sing Analysis %ervices A9*s
8 reating and managing data mining ob$ects using A)
8 (ata mining client programming with A()(.3T
8
8/13/2019 462616 ch14_ch14d
3/33
for Data Mining introduces the conce+t of data mining
models as database objects.
XM-A XM- for Anal$sis A communication +rotocol and XM-format for communicating 1ith an
anal$tical server inde+endent of an$
+latform.
ADO
Active (ata b$ects A(- was created to assist the isual 2asic programmer in
accessing data residing in databases. The A( libraries wrap the ' (2
interfaces into ob$ects that are easier to program against. 2ecause ' (2 for
(ata )ining specifies that a data mining provider is first an ' (2 provider,
A( can be used to e!ecute data mining >ueries $ust as it does relationaldatabase >ueries.
A( reduces the comple!ity of ' (2 interfaces to three essential ob$ects7
the connection, the command, and the record set. The connection objectis used
to connect to the server and to issue schema rowset >ueries. The command object
is used to e!ecute ()+ statements and optionally retrieve their results, and the
record set objectcontains the result of any data returning >ueries.
ADO.ET
A(.3T is the managed data access layer. *t was created to allow managed
languages, such as isual 2asic .3T and 5, to access data, much as A( was
created for native languages. The philosophy of A(.3T is somewhat differentfrom that of A( in that A(.3T is designed to wor# in a disconnected
mode, where data can be accessed and manipulated without maintaining an active
connection to the server.
8/13/2019 462616 ch14_ch14d
4/33
dataadapters@@mdeither the generic adapter that is supplied with A(.3T or
a provider"specific adapter such as the %&'(ataAdapter. ?or direct data access,
A(.3T uses a datareader, which is similar in concept to the A( record set,
returned from its command ob$ect.
ADOMD.ET
A()(.3T A(.3T @@nd )ultidimensional- is a managed data provider
implementing the dataadapter and datareader interfaces of A(.3T specifically
for Analysis %ervices, ma#ing it faster and more memory"efficient than the
generic A(.3T ob$ects. *n addition to the standard A(.3T interfaces,
A()(.3T contains data mining and 'A9"specific ob$ects, ma#ing
programming data mining client applications easier.
The )ining%tructure, )ining)odel, and )iningolumncollections ma#e it easy
to e!tract the metadata describing the ob$ects on the server. The
)iningontent3ode ob$ect allows for the programmatic browsing of mining
models and can be accessed from the root of the content hierarchy or randomly
from any node in the content.
NOTE
There also exists a native version of ADOMD.NET, appropriately named ADOMD.
This interface is maintained mostly for backward compatibility with !" erver
#$$$ and does not contain any ob%ects or interfaces for data minin& pro&rammin&.
Server ADOMD
%erver A()( is an ob$ect model for accessing Analysis %erver ob$ects, both
data mining and 'A9, directly on the server. *t is intended for use in user"
defined functions, described later in this chapter.
AMO
A), or Analysis )anagement b$ects, is the main management interface for
Analysis %ervices. *t replaces the %&' %erver BBB interface, (ecision %upport
b$ects (%-, which is still maintained for bac#ward compatibility, but has not
been updated to ta#e advantage of all the new features of %&' %erver BBC.
'i#e A()(.3T, A) contains the )ining%tructures, )ining)odels, and
)iningolumnscollections, and the li#e. owever, whereas A()(.3T is for
browsing and >uerying, A) is for creating and managing. All the operationsyou perform in the user interfaces of the 2*
8/13/2019 462616 ch14_ch14d
5/33
TIP
'o( sho(ld (se ADOMD.NET when writin& data minin& client applications except
when .NET is not available. Otherwise, (se ADO )or O"E D*+ for indows
applications, or plain -M"A for thin client applications. or applications in
which yo( will be creatin& new models or mana&in& existin& models, (se AMO.
NOTE
ee *ooks Online for f(ll doc(mentation and samples of all A/0s (sed by Analysis
ervices.
Using Analysis Services APIs
uired references7
Table 14.! Anal"sis Servi#es Re$eren#es
API TYPE REFERENCES
ADO Native Microsoft ActiveX Data Objects
ADOMD.N! Managed Microsoft.Anal$sis%ervices.Adomd&lient
%erver
ADOMD
Managed Microsoft.Anal$sis%ervices.Adomd%erver
AMO Managed Microsoft.Anal$sis%ervices
Microsoft.Data4arehouse.5nterfaces
D%O Native Microsoft Decision %u++ort Objects 6
To ma#e your coding easier you can add code li#e the following to the top of
your source files so that you don/t have to specify the fully >ualified name for
every ob$ect.
1*.NET
Imports Microsoft.AnalysisServices
5
8/13/2019 462616 ch14_ch14d
6/33
Using MicrosoftAnalysisServices to Create an!
Manage Mining Mo!els
*n this section, we will use A) to create and manage models using the
)ovielic# database to analy=e premium channel use across different
generations of customers. *f your programming interest lies only in embedding
data mining into client applications, you can s#ip this section.
The simplest way to create mining models is to use ()+ statements such as
DAT )*3*3E )('and *3%DT *3Twith any of the command interfaces
such as A(, A(.3T, or A()(.3T.
8/13/2019 462616 ch14_ch14d
7/33
7igure 89.8 Partial AMO object hierarch$
8/13/2019 462616 ch14_ch14d
8/33
AMO %asi#s
A) is a rather straightforward ob$ect model placed on top of the +)'
representation of Analysis %ervices ob$ects. *n addition to providing a convenientA9*, A) also provides basic validation and methods to update, change, and
monitor ob$ects on the server.
NOTE
To add AMO code to yo(r pro%ect, yo( need to add references to two assemblies2
)icrosoft.Analysis%ervicesand )icrosoft.(ata
8/13/2019 462616 ch14_ch14d
9/33
8/13/2019 462616 ch14_ch14d
10/33
S'! Createata!ase(
im svr As Server
im %! As ata!ase
Create server o!)ect an% connect
svr = *e+ Server(
svr.Connect("location=localhost"
Create %ata!ase an% set properties
%! = *e+ ata!ase(
%!.*ame = "MovieClic,"
%!.I = "MovieClic,"
A%% %ata!ase an% commit to server
svr.ata!ases.A%%(%!
%!.Up%ate(
isconnect from server svr.isconnect(
n% S'!
-isting 89.8 Database creation
Creating Data Access Objects
After you have a database ob$ect, the ne!t step is to create (atasource and
(atasourceiew (%- ob$ects. The (atasource ob$ect is fairly trivial, consisting
of little more than a connection string to your database. The (% is a bit more
complicated. The main element of the (% is the schema, which is a standard
(atasetob$ect augmented with custom properties.
To load a schema into a (%, you create data adapters for each of the tables
you wish to load and add their schemas into a dataset. 0ou then add any
relationships necessary and finally add the dataset to a (%, which is then added
to the A) database. 'isting 14. demonstrates this procedure by creating a
(atasource for the )ovielic# data and a (% that can be used to create mining
models with a nested table needed for analysis of movie channels.
S'! CreateataAccess-!)ects(y/al %! As ata!ase
Create relational %ataso'rce
im %s As *e+ #elationalataSo'rce("MovieClic,"0"MovieClic,"
%s.ConnectionString = "1rovi%er=S2$-$;ata So'rce=localhost;" & _ "Initial Catalog=MovieClic,;Integrate% Sec'rity=3r'e"
%!.ataSo'rces.A%%(%s
Create connection to %ataso'rce to e4tract schema to %ataset
im %set As *e+ ataSet(
im cn As *e+ S5lConnection("ata So'rce=localhost;" & _
8/13/2019 462616 ch14_ch14d
11/33
"Initial Catalog=MovieClic,;Integrate% Sec'rity=3r'e"
Create %ata a%apters from %ata!ase ta!les an% loa% schemas
im %aC'stomers As *e+ S5lataA%apter("Select 6 from S'rvey"0 cn %aC'stomers.7illSchema(%set0 Schema3ype.Mappe%0 "C'stomers"
im %aChannels As *e+ S5lataA%apter("Select 6 from Channels"0 cn
%aChannels.7illSchema(%set0 Schema3ype.Mappe%0 "Channels"
A%% relationship !et+een C'stomers an% Channels
im %rC'stomerChannels As *e+ ata#elation("C'stomerChannels"0 _
%set.3a!les("C'stomers".Col'mns("S'rvey3a,enI"0 _
%set.3a!les("Channels".Col'mns("S'rvey3a,enI"
%set.#elations.A%%(%rC'stomerChannels
Create the S/0 a%% the %ataset0 an% a%% to the %ata!ase
im %sv As *e+ ataSo'rce/ie+lataSo'rce/ie+("MovieClic,"0
"MovieClic,"
%sv.ataSo'rceI = "MovieClic," %sv.Schema = %set.Clone(
Up%ate the %ata!ase to create the o!)ects on the server.
%!.Up%ate(Up%ate-ptions.4pan%7'll
n% S'!
-isting 89. Data Access Object creation
The (% of 'isting 14. contains the customer table and the channels table, but
the models you want to build need more specific information than is present in
the raw data7 the customers/ generation and a list of only the premium movie
channels they watch. To accomplish this, you need to modify the code to add acalculated column to the ustomers table and swap out the hannels table with a
named >uery returning only the limited set of channels you are interested in.
'isting 14.G contains reate(ataAccessb$ects modified with a named
calculation and named >uery.
S'! CreateataAccess-!)ects(y/al %! As ata!ase
Create relational %ataso'rce
im %s As *e+ #elationalataSo'rce("MovieClic,"0"MovieClic,"
%s.ConnectionString = "1rovi%er=S2$-$;ata So'rce=localhost;" & _
"Initial Catalog=MovieClic,;Integrate% Sec'rity=3r'e"
%!.ataSo'rces.A%%(%s
Create connection to %ataso'rce to e4tract schema to %ataset im %set As *e+ ataSet(
im cn As *e+ S5lConnection("ata So'rce=localhost;" & _
"Initial Catalog=MovieClic,;Integrate% Sec'rity=3r'e"
Create the c'stomers %ata a%apter +ith the
calc'late% col'mn appen%e%
8/13/2019 462616 ch14_ch14d
12/33
im %aC'stomers As *e+ S5lataA%apter("S$C3 60 " & _
"(CAS 89* (Age : " & _
" 89* (Age ?= < A* Age : @
8/13/2019 462616 ch14_ch14d
13/33
%sv.Schema = %set.Clone(
%!.ataSo'rce/ie+s.A%%(%sv
Up%ate the %ata!ase to create the o!)ects on the server. %!.Up%ate(Up%ate-ptions.4pan%7'll
n% S'!
-isting 89.; &reating calculated columns and named
8/13/2019 462616 ch14_ch14d
14/33
an% a%%ing a ,ey col'mn to the neste% ta!le.
im 1ayChannels As *e+ 3a!leMiningStr'ct'reCol'mn _
("1ayChannels"0 "1ayChannels"
im Channel As *e+ ScalarMiningStr'ct'reCol'mn _ ("Channel"0 "Channel"
Channel.3ype = MiningStr'ct'reCol'mn3ypes.3e4t
Channel.Content = MiningStr'ct'reCol'mnContents.Eey
Channel.IsEey = 3r'e
Channel.EeyCol'mns.A%%("1ayChannels"0 "Channel"0 -le!3ype./arChar
1ayChannels.Col'mns.A%%(Channel
ms.Col'mns.A%%(1ayChannels
A%% the MiningStr'ct're to the %ata!ase.
%!.MiningStr'ct'res.A%%(ms
ms.Up%ate(
n% S'!
-isting 89.9 &reating the mining structure
NOTE
'o( may wonder why yo( specify that the col(mn content is Heyand also have to
set the *sHeyproperty to True. This is d(e to the extensibility in the content types
defined in the O"E D* for Data Minin& specification. 5(rrently Analysis ervices
s(pports three types of keys2 :ey, :ey Time, and :ey e4(ence. ;avin& a
separate *sHeyproperty allows yo( to take advanta&e of this extensibility in the
f(t(re.
Creating the Mining Models
?inally, you are at the point where you can create the models you wish to use to
analy=e your customers. *n addition to a collection of columns, a structure
contains a collection of models. ?or each model, you add the columns you wish
from the structure and set their usage to Hey, 9redict, or 9redictnly. olumns
without a specified usage are assumed to be *nput, so you do not need to
e!plicitly set them. olumns that you want the algorithm to ignore, you simply
do not add to the model.
'isting 14.C demonstrates how to create two models inside the structure you
previously built. A parameteri=ed cluster model is created and then a tree model
is built from a copy of that model.
S'! CreateMo%els(y/al ms As MiningStr'ct're
im Cl'sterMo%el As MiningMo%el
im 3reeMo%el As MiningMo%el
im mmc As MiningMo%elCol'mn
Create the Cl'ster mo%el an% set the
8/13/2019 462616 ch14_ch14d
15/33
algorithm an% parameters.
Cl'sterMo%el = ms.CreateMiningMo%el(3r'e0 _
"1remi'm eneration Cl'sters"
Cl'sterMo%el.Col'mns.Clear( Cl'sterMo%el.Algorithm = "Microsoft_Cl'sters"
Cl'sterMo%el.Algorithm1arameters.A%%("C$US3#_C-U*3"0
8/13/2019 462616 ch14_ch14d
16/33
algoritms installe' as ell. A''itionall"/ ea# algoritm s(&&orts a variet" o$
&arameters ose 'e$a(lt val(es ma" var" 'e&en'ing on te server
#on$ig(ration.
Te )*3*3EI%D*%an' )*3*3EI9ADA)TD%s#ema rosets
'es#ribe' in Ca&ter ! #ontain 'es#ri&tions o$ te available algoritms an' teir
#a&abilities. o( #an (se an" #lient #omman' API to a##ess tese s#emas/ or/
even better/ "o( #an (se te ob*e#t mo'el &rovi'e' in ADOMD.ET to iterate
5(i#2l" tro(g te server6s 'ata mining #a&abilities. Te $olloing #o'e
'emonstrates o to iterate tro(g te mining servi#es an' teir res&e#tive
&arameters.
S'! iscoverServices(
im cn As *e+ A%om%Connection("location=localhost"
im ms As MiningService
im mp As MiningService1arameter
cn.-pen(
7or ach ms In cn.MiningServices Console.8rite$ine("ServiceH " & ms.*ame
7or ach mp In ms.Availa!le1arameters
Console.8rite$ine(" 1arameterH " & mp.*ame & _
" efa'ltH " & mp.efa'lt/al'e
*e4t
*e4t
cn.Close(
n% S'!
Processing Mining Models
The code for processing an ob$ect is trivial, consisting only of the 9rocessmethod
called with the appropriate options. *n the e!ample program, you could processan individual model, the mining structure, or the entire database as you choose.
owever, because processing can be a rather lengthy tas#, it would be nice to
receive progress messages from the server for the duration. 'uc#ily, the A)
contains a Trace ob$ect to handle this type of server interaction. 'isting 14.F
demonstrates setting up a progress trace for a processing operation.
S'! 1rocessata!ase(y/al svr As Server0 y/al %! As ata!ase
im t As 3race
im e As 3racevent
Create the trace o!)ect to trace progress reports
an% a%% the col'mn containing the progress %escription.
t = svr.3races.A%%(
e = t.vents.A%%(3raceventClass.1rogress#eportC'rrent
e.Col'mns.A%%(3raceCol'mn.3e4tata
t.Up%ate(
A%% the han%ler for the trace event.
8/13/2019 462616 ch14_ch14d
17/33
A%%9an%ler t.-nvent0 A%%ress-f 1rogress#eport9an%ler
3ry
Start the trace process of the %ata!ase0 then stop it. t.Start(
%!.1rocess(1rocess3ype.1rocess7'll
t.Stop(
Catch e4 As 4ception
n% 3ry
#emove the trace from the server.
t.rop(
n% S'!
S'! 1rogress#eport9an%ler(y/al sen%er As -!)ect0 _
y/al e As 3raceventArgs
l!l1rogress.3e4t = e(3raceCol'mn.3e4tata
n% S'!
-isting 89.> Processing the database 1ith +rogress re+orts
Deploying Mining Models
After creating your models, you may find that you need to move them around to
different servers; for e!ample, you may need to move them from an analytical
server to a production server for embedding into line"of"business applications, or
maybe simply to share a model with a colleague who cannot physically access
your servers.Analysis %ervices provides a robust bac#up and restore A9* in A).
owever, these A9*s are geared more toward 'A9 ob$ects than toward data
mining ob$ects. The A9*s contain many options that are unnecessary for data
mining and at the same time operate solely at the database level, which is
generally too coarse for most data mining operations.
(ue to the mismatch in functionality provided and the functionality re>uired in
A), the deployment of data mining ob$ects is handled through ()+ using a
command A9*. :sing the ()+ +9DTand *)9DTcommands, you can select
the single model that performs best out of the forest of candidate models you
created and deploy it alone, rather than deploying the entire database. 'isting
14.J demonstrates using A()( to transfer individual models from your
current server to your production server.
S'! 3ransferMo%el(
Create connections to the so'rce an% %estination server.
im cnSo'rce As *e+ A%om%Connection("location=localhost;" & _
"Initial Catalog=MovieClic,"
im cnest As *e+ A%om%Connection("location=1ro%'ctionServer;" & _
8/13/2019 462616 ch14_ch14d
18/33
Initial Catalog=MovieClic,"
3ry
4port the mo%el to a share on the %estination server. im cm%4port As *e+ A%om%Comman%
cm%4port.Connection = cnSo'rce
cm%4port.Comman%3e4t = "1-#3 MI*I* M-$ eneration3ree " & _
"3- JJ1ro%'ctionServerJ3ransferJeneration3ree.a!, " & _
"8I39 1ASS8-#= My1ass+or%"
cnSo'rce.-pen(
cm%4port.4ec'te*on2'ery(
Import the mo%el into the c'rrent %ata!ase on the
%estination server.
im cm%Import As *e+ A%om%Comman%
cm%Import.Connection = cnest
cm%Import.Comman%3e4t = "IM1-#3 7#-M " & _
" KcHJ3ransferJeneration3ree.a!,L " & _
" 8I39 1ASS8-#= My1ass+or% "
cnest.-pen(
cnest.4ec'te*on2'ery(
Catch e4 As 4ception
n% 3ry
cnSo'rce.Close(
cnest.Close(
n% S'!
-isting 89.? 2+orting and im+orting of mining models
*n this e!ample, you simply move one model between servers. The +9DT
command is fle!ible enough to e!port multiple models or entire mining structures
as well. *f you need to reprocess the models on the destination server, you can
append *3':( (93(3*% to the +9DTcommand, and the necessary
(atasource and (% ob$ects will be included in the e!port pac#age.
NOTE
D(e to the fact that O"A/ ob%ects do not s(pport ob%ect8level importin& and
exportin&, O"A/ minin& models cannot be exported (sin& the +9DTcommand.
Setting Mining Permissions
After the models are built, processed, and deployed, you need to assign
permissions so that they can be accessed by client applications. 9ermissions in
Analysis %ervices are managed by the coordination of two ob$ects7 a Doleob$ect,
which belongs to the database and contains a list of members, and a 9ermission
ob$ect belonging to the protected ob$ect, which refers to a role and specifies the
http://smb//ProductionServer/Transfer/GenerationTree.abkhttp://smb//ProductionServer/Transfer/GenerationTree.abk8/13/2019 462616 ch14_ch14d
19/33
access permissions of that role. 'isting 14.K demonstrates creating a role and
assigning permissions.
S'! SetMo%el1ermissions(y/al %! As ata!ase0 y/al mm As MiningMo%el
Create a ne+ role an% a%% mem!ers.
im r As *e+ #ole("Mo%el#ea%er"0 "Mo%el#ea%er"
r.Mem!ers.A%%(*e+ #oleMem!er ("M-/IC$ICEJamiemac"
r.Mem!ers.A%%(*e+ #oleMem!er ("M-/IC$ICEJBhaotang"
A%% the role to the %ata!ase an% 'p%ate
%!.#oles.A%%(r
r.Up%ate(
Create a permission o!)ect referring to the role.
im mmp As *e+ MiningMo%el1ermission(
mmp.*ame = "Mo%el#ea%er"
mmp.I = "Mo%el#ea%er"
mmp.#oleI = "Mo%el#ea%er"
Assign access rights to the permission.
mmp.#ea% = #ea%Access.Allo+e%
mmp.Allo+ro+sing = 3r'e
mmp.Allo+rill3hro'gh = 3r'e
mmp.Allo+1re%ict = 3r'e
A%% permissions to the mo%el an% 'p%ate
mm.MiningMo%el1ermissions.A%%(mmp
mm.Up%ate(
n% S'!
-isting 89.@ Assigning mining model +ermissions
#ro$sing an! %&erying Mining Mo!els
reating and deploying models is only the beginning. The real fun starts when
you ta#e the power of the learned #nowledge of your models and embed that
directly into your applications. 0ou can recommend products, manage inventory,
forecast revenue, validate data, and perform countless other tas#s limited only by
your data and your imagination.
Instant Embe''e' Pre'i#tions
%ince %&' %erver (ata )ining is built upon e!isting standards, it is possible to
embed data mining results into an application while writing a single line of code.
:sing the data access ob$ects in the isual %tudio form designer and a data
8/13/2019 462616 ch14_ch14d
20/33
mining >uery prepared from the 9rediction &uery 2uilder, you can embed the
results using the following steps.
1. :sing the 2* or %&' uery in the9rediction &uery 2uilder. %witch to %&' view and copy the generated
>uery.
. *n isual %tudio, add an le(b(ataAdapterto a form.
G. *n the (ata Adapter onfiguration uery into the te!t bo!. lic# the
Advanced ptions button and clear the Eenerate *nsert, :pdate, and
(elete %tatements chec# bo!.C. ?inish the wi=ard, setting any other desired options and ignoring any
warnings.
F. Eenerate a (ata%et by right"clic#ing the le(b(ataAdapteryou created
and selecting Eenerate (ata%et.
J. Add a (ataErid control to your uery e!ecution. Deaders familiar with
8/13/2019 462616 ch14_ch14d
21/33
A(.3T will notice that the only differences between the A9*s thus far are the
names of the data access classes.
1rivate S'! Single#es'lt2'ery(
Create connection an% comman% o!)ects.
im cn As *e+ A%om%Connection("location=localhost; " & _
"Initial Catalog=MovieClic,"
im cm% As *e+ A%om%Comman%(
InitialiDe comman% +ith 5'ery
cm%.Connection = cn
cm%.Comman%3e4t = "S$C3 1re%ict(eneration " & _
"7#-M Oeneration 3reesP *A3U#A$ 1#IC3I-* -I* " & _
"S$C3 (S$C3 9- AS Channel U*I-* " & _
"S$C3 Sho+time AS Channel as 1ayChannels as t"
-pen connection an% +rite res'lt to %e!'g +in%o+
cn.-pen(
K4ec'teScalar is not s'pporte% in the #3M version of
im rea%er As A%om%ata#ea%er
#ea%er = cm%.4ec'te#ea%er(
#ea%er.#ea%(
e'g.8rite$ine(rea%er.et/al'e(
8/13/2019 462616 ch14_ch14d
22/33
IM rea%er AS A%om%ata#ea%er
cn.-pen(
rea%er = cm%.4ec'te#ea%er(
8rite fiel% names to %e!'g +in%o+
im i As Integer
7or i = < 3o rea%er.7iel%Co'nt F N
e!'g.8rite(rea%er.et*ame(i & "Jt"
*e4t
e!'g.8rite$ine(""
Iterate res'lts to %e!'g +in%o+
8hile rea%er.#ea%
7or i = < 3o rea%er.7iel%Co'nt F N
e!'g.8rite(rea%er.et/al'e(i.3oString(
*e4t
e!'g.8rite$ine(""
n% 8hile
Close rea%er an% connection
rea%er.Close(
cn.Close(
n% S'!
-isting 89.8/ 5terating a multi+le*ro1 result
*n the last e!ample, you flatten the results of a nested table >uery for ease of
iteration. *n some situations, however, flattening the results is not practical, for
e!ample when you have a >uery returning multiple nested tables, or even nested
tables inside nested tables. 'isting 14.11 demonstrates how to iterate the resultsof the previous e!ample with the ?'ATT3(#eyword removed.
im neste%rea%er As A%om%ata#ea%er
8hile rea%er.#ea%(
neste%rea%er = rea%er.et#ea%er(
8/13/2019 462616 ch14_ch14d
23/33
engine. To use named parameters in your >uery, you are forced to use
A()(.3T. 'isting 14.1 demonstrates your data mining >uery using named
parameters.
InitialiDe comman% +ith parameteriDe% 5'ery
cm%.Comman%3e4t = "S$C3 1re%ict9istogram(eneration " & _
"7#-M Oeneration 3reesP *A3U#A$ 1#IC3I-* -I* " & _
"(S$C3 (S$C3 QChannelN AS Channel U*I-* " & _
"S$C3 QChannelR AS Channel as 1ayChannels as t"
InitialiDe parameters an% a%% to comman%
im ChannelN As *e+ A%om%1arameter(
im ChannelR As *e+ A%om%1arameter(
ChannelN.1arameter*ame = "QChannelN"
ChannelR.1arameter*ame = "QChannelR"
cm%.1arameters.A%%(ChannelN
cm%.1arameters.A%%(ChannelR
Set parameter val'es
cm%.1arameters("QChannelN"./al'e = "9-"
cm%.1arameters("QChannelR"./al'e = "Sho+time"
-isting 89.8 Data mining
8/13/2019 462616 ch14_ch14d
24/33
"S9A1 QCase3a!le T " & _
"A11* ( Q*este%3a!le T " & _
"#$A3 C'stI to C'stI AS Channels " & _
"as t"
InitialiDe parameters an% a%% to comman%
im case1aram As *e+ A%om%1arameter(
im neste%1aram As *e+ A%om%1arameter(
case1aram.1arameter*ame = "Case3a!le"
neste%1aram.1arameter*ame = "*este%3a!le"
cm%.1arameters.A%%(case1aram
cm%.1arameters.A%%(*este%1aram
Set parameter val'es
cm%.1arameters("Case3a!le"./al'e = case3a!le
cm%.1arameters("*este%3a!le"./al'e = neste%3a!le
-isting 89.8; Data mining
8/13/2019 462616 ch14_ch14d
25/33
7igure 89. Data mining object hierarch$ in ADOMD.N!
As you can see from the ob$ect model, you can simply connect to the server
and iterate over any of the data mining ob$ects without having to resort to schema
>ueries. A nice benefit to application developers is that if a connected user does
not have access to a particular ob$ect, that ob$ect will simply not appear in its
collection, as if it didn/t e!ist.
The most interesting ability you gain by using the A()(.3T ob$ect model
is the ability to iterate mining model content in a natural, hierarchical, manner
using ob$ects instead of trying to unravel the flat schema rowset form. :sing thisob$ect model ma#es it easy to write comple! programs to e!plore or display the
content to your users. ?or e!ample, an interesting problem for the )icrosoft
(ecision Trees algorithm is this7 given an attribute, find all of the trees that
contain a split on that attribute.
8/13/2019 462616 ch14_ch14d
26/33
'isting 14.14 demonstrates using the content ob$ect model to e!plore trees to
find splits on a specified attribute. ?irst, you identify all child nodes of the root
that represents trees and then recursively chec# the children of the trees to see
whether their marginal rule contains the re>uested attribute. 2y loo#ing at the
node type rather than at the algorithm used, this function will wor# against any
model containing trees, whether it uses the )icrosoft (ecision Trees algorithm,
the )icrosoft Time %eries algorithm, or any third"party tree"based algorithms.
I%entify all the attri!'tes that split
on a specifie% attri!'te.
S'! 7in%Splits(y/al cn As A%om%Connection0 _
y/al Mo%el*ame As String0 y/al Attri!'te*ame As String
7in% the specifie% mo%el.
im mo%el As MiningMo%el
mo%el = cn.MiningMo%els(Mo%el*ame
If Is*'ll(mo%el 3hen #et'rn
$oo, for the attri!'te in all mo%el trees.
im no%e As MiningContent*o%e
7or ach no%e In mo%el.Content.Item(uery
8/13/2019 462616 ch14_ch14d
27/33
S$C3 1re%ict(eneration0 1re%ict*o%eI%(eneration
to retrieve the *( of the node used to generate the prediction, and feed the result
into a function li#e that in 'isting 14.1C.
7'nction et1re%iction#eason(y/al mo%el As MiningMo%el0 _
y/al *o%eI As String As String
im no%e As MiningContent*o%e
no%e = mo%el.et*o%e7romUni5'e*ame(*o%eI
If Is*'ll(no%e 3hen 3hro+ *e+ System.4ception("*o%e not fo'n%"
ret'rn no%e.escription;
n% 7'nction
-isting 89.8= :etrieving node descri+tion
Store' Pro#e'(res
A()(.3T provides an e!cellent ob$ect model for accessing server ob$ects
and browsing content. owever, there are some ma$or drawbac#s. ?or the
?ind%plitsmethod in 'isting 14.14, you need to bring the entire content from the
server to the client to determine the list. A model with 1,BBB trees and 1,BBB
nodes per tree would re>uire the marshaling of over 1,BBB,BBB rows, even if only
a handful of trees referenced the desired attribute. Also, in the
Eet9redictionDeason function, even though you can access the desired node
directly using Eet3ode?rom:ni>ue3ame, you are still causing a round"trip to the
server on each call; performing this operation in batch is not recommended.
There is a solution to these problems. Analysis %ervices in %&' %erver BBC
supports stored procedures that can be written in any managed language such as
5, 2.3T, or managed MM. The ob$ect model, A()(M, is almost identical
to that of A()(.3T, ma#ing conversion between the two models simple. The
clear advantage of A()(M is that all of the content is available on the server,
and you can return only the information you need to the server. 0ou can call
:(?s by themselves, using the A''synta! or as part of a ()+ >uery. ?or
e!ample, the following >uery
CA$$ MySprocs.3ree9elpers.7in%Splits(Keneration 3reesL0L9-L
calls a stored procedure directly and simply returns the result, whereas the >uery
S$C3 1re%ict(eneration0
MySprocs.3ree9elpers.et1re%iction#eason(1re%ict*o%eI%(eneration
8/13/2019 462616 ch14_ch14d
28/33
calls a stored procedure for every row returned from the prediction >uery. *n this
case, the >uery will return the prediction result plus the e!planation of the result
for every row.
CALLI+ ,%A AD E8CEL 97CTIOS AS STORED PROCED7RES
I$ "o( ave Mi#roso$t O$$i#e installe' on te same ma#ine as "o(r Anal"sis
Servi#es server/ "o( #an leverage te $(n#tions o$ ,is(al %asi# $or A&&li#ations
:,%A; an' E0#el as store' &ro#e'(res insi'e "o(r DM8 5(eries.
9or e0am&le/ "o( #an #onvert te &re'i#tion o(t&(t to loer#ase li2e tis Data mining stored +rocedures
8/13/2019 462616 ch14_ch14d
32/33
Executing Queries Inside Stored Procedures
A common use of a stored procedure is to encapsulate a >uery for easy reuse. ?or
e!ample, if your application needed to predict Eeneration, but you needed thefle!ibility to change the model that was being used or add additional business
logic, you could write a procedure that e!ecutes the >uery and redeploy the
procedure as necessary without changing the application layer.
%erver A()( allows the e!ecution of ()+ >ueries using the same ob$ects
that you would use with A()(.3T, the only e!ception being that you do not
have to specify a connection, since you are already connected. Desults from the
>uery can be copied into a (ataTable , or you can simply return the (ataDeader
returned by !ecuteDeader. 'isting 14.1J demonstrates the >uery from 'isting
14.L implemented as a :(?.
Imports Microsoft.AnalysisServices.A%om%Server
Imports System.ata
1'!lic Class MyClass
:Safe3o1repare(3r'e? _
1'!lic 7'nction 1re%icteneration( as A%om%ata#ea%er
im cm% As *e+ A%om%Comman%(
InitialiDe comman% +ith 5'ery
cm%.Comman%3e4t = "S$C3 1re%ict(eneration " & _
"7#-M Oeneration 3reesP *A3U#A$ 1#IC3I-* -I* " & _
"(S$C3 (S$C3 9- AS Channel U*I-* " & _
"S$C3 Sho+time AS Channel as 1ayChannels as t"
#et'rn res'lt to client
#et'rn cm%.4ec'te#ea%er(n% S'!
-isting 89.8? 2ecuting a DMX uery inside the stored procedure, and you
wouldn/t have to change >ueries embedded inside your application. f course,
you can parameteri=e your >uery as demonstrated in 'isting 14.1.
NOTE
tored proced(res cannot be (sed to implement sec(rity in Analysis ervices. The
sec(rity context of the c(rrent (ser is (sed to determine the access to the ob%ects
inside the Analysis ervices server. That is, any (ser callin& a proced(re that
4(eries a minin& model who does not read permission on that model will receive a
permission error. imilarly, a (ser callin& the Eet9redictionDeason9D from
"istin&
8/13/2019 462616 ch14_ch14d
33/33
Deploying and Debugging Stored Procedure Assemblies
After you have compiled and built your stored procedure, you need to deploy the
procedure to your Analysis %erver so that you can call it from ()+. To add a.3T assembly to your Analysis %ervices pro$ect, you right"clic# the Assemblies
folder in the %olution !plorer and select 3ew Assemble Deference.
ueries.
:sing these A9*s, you can create intelligent applications of your own. The
logic of your application can involve dynamically creating mining models to
solve user"defined problems. *t can apply the predictive power of the data mining
algorithms or e!amine the learned content of the mining models to provide new
insights and new abilities to your users. And finally, you can leverage your server
in your application by writing user"defined functions that have access to all of the
server resources through a .3T programming model.