www.gusucode.com > database 案例源码程序 matlab代码 > database/ImportLargeDataDatabaseDatastoreTallArrayExample.m

    %% Analyze Large Data in Database Using Tall Arrays
% This example shows how to create a |<docid:database_ug.bufomil
% DatabaseDatastore>| object and analyze large data using a tall table.
% This example determines the minimum arrival delay using a large set of
% flight data that is stored in a database.
%
% You can access large data sets and create a tall array using a
% |DatabaseDatastore| object with Database Toolbox(TM). To visualize data
% in the tall array, see <docid:import_export.bvflpql>.
% 
% Alternatively, you can write a MapReduce algorithm that defines the
% chunking and reduction of the data. For an example of using MapReduce,
% see <docid:database_examples.example-ex81756746>.
%
% The |DatabaseDatastore| object does not support using a parallel pool
% with Parallel Computing Toolbox(TM) installed. To analyze data using tall
% arrays or run MapReduce algorithms, set the global execution environment
% to be the local MATLAB(R) session.

%% Create |DatabaseDatastore| Object
% Set the global execution environment to be the local MATLAB(R) session.
mapreducer(0);

%%
% The file |airlinesmall.csv| contains the large set of flight data. Load
% this file into a Microsoft(R) SQL Server(R) database table
% |airlinesmall|. This table contains 123,523 records.

%%
% Using a JDBC driver, create a database connection |conn| to a
% Microsoft(R) SQL Server(R) database with Windows(R) authentication.
% Specify a blank user name and password. Here, the code assumes that you
% are connecting to a database |toy_store|, a database server |dbtb04|, and
% port number |54317|.

conn = database('toy_store','','','Vendor','Microsoft SQL Server', ...
    'Server','dbtb04','PortNumber',54317,'AuthType','Windows');

%% 
% Create a |DatabaseDatastore| object |dbds| using the database connection
% |conn| and SQL query |sqlquery|. This SQL query retrieves arrival-delay
% data from the table |airlinesmall|.

sqlquery = 'select ArrDelay from airlinesmall';

dbds = databaseDatastore(conn,sqlquery,'ReadSize',50000);

%%
% |databaseDatastore| executes the SQL query |sqlquery|.

%% Find Minimum Arrival Delay Using Tall Array
% Because the |DatabaseDatastore| object |dbds| returns a table, create a
% tall table |tt|.
tt = tall(dbds);

%%
% Find the minimum arrival delay |minArrDelayValue|. 
minArrDelay  = min(tt.ArrDelay); 

%%
% |minArrDelay| contains the unevaluated minimum arrival delay. To return
% the output value, use |gather|. For details about deferring evaluation,
% see <docid:import_export.bvciqpo>.
minArrDelayValue = gather(minArrDelay)

%%
% Besides determining a minimum, tall arrays support many other functions.
% For a list of supported functions, see <docid:import_export.bvc7_x4-1>.

%% Close |DatabaseDatastore| Object and Database Connection
close(dbds)