Using Database Toolbox to connect to and query Cosmos DB

4 次查看(过去 30 天)
I am new to Matlab's Database Toolbox and have limited experiences with databases in general. There is an Azure Cosmos DB that I would like to connect to and run SQL queries against. I saw MATLAB Interface for Azure Cosmos DB, but it's complex and I'm hoping there's a way to connect straight from the Database Toolbox. I am using R2020a, could update if needed though there doesn't seem to any mention of improvements related to Cosmos in the Database Toolbox release notes.
Any help on setting up the connection to the database, and running an example SQL query, would be much appreciated.

采纳的回答

KAE
KAE 2021-4-8
编辑:KAE 2021-4-8
In case it helps someone else: I got extensive help from Mathworks on using the MATLAB Interface for Azure Cosmos DB and am now able to run SQL queries on my Cosmos DB and pull the results into MATLAB. Below is example code based on modifying what Mathworks provided. You can only run this after you install all the third party software required by the Interface.
%% Add Azure Cosmos DB SQL paths
% Change next line to the directory where you installed the Azure startup.m file
myAzurePath = ['C:\MyAzureInstallDirectory\matlab-azure-cosmos-db-master\' ...
'matlab-azure-cosmos-db-master\Software\MATLAB\SQL\'];
run([myAzurePath 'startup.m']) % Should see "Adding Azure Cosmos DB SQL Paths"
%% Set up the client & config
databaseId = 'MyDatabaseName'; % Change to your database name
database = azure.documentdb.Database(databaseId);
docClient = azure.documentdb.DocumentClient(); % Create a document client
%% Confirm that you can connect to database
options = azure.documentdb.RequestOptions();
databaseLink = ['/dbs/',database.getId()];
if docClient.existsDatabase(databaseLink, options)
disp('Connected successfully to database')
end
collectionId = 'MyContainerName'; % Change to your container name
collectionLink = ['/dbs/',databaseId,'/colls/',collectionId];
% Can ignore any log complaints that appear here
%% Query to retrieve first few database records
options = azure.documentdb.FeedOptions();
options.setEnableCrossPartitionQuery(true);
% Edit this example query, which retrieves the first 10 records
queryStr = ['SELECT TOP 10 * FROM MyContainerName c'];
response = docClient.queryDocuments(collectionLink, queryStr, options);
responseCellArray = response.getQueryCellArray();
iRecord = 1; % Get the first of the 10 records retreived
FirstRecord = jsondecode(responseCellArray{iRecord}.toJson); % Output structure containing first record
  3 个评论
Karthik
Karthik 2022-8-11
Hi Kae, We're trying to follow the instructions listed for cosmodb connection and this is the error message we keep getting when we try to run the code with the connection settings that you have recommended
Unable to resolve the name 'com.microsoft.azure.documentdb.Database'
Please let me know if there's anything else we need to check. Thank you !!
KAE
KAE 2022-8-15
I would say, contact support@mathworks.com for additional help since I am using this somewhat blindly. There are many details that seem to have to be correct for it to work so their help can save you time.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Database Toolbox 的更多信息

产品


版本

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by