Deploy Graph Database Application with MATLAB Compiler
This example shows how to write a script to analyze data stored in a graph database, and deploy the script as a standalone application. Write code that connects to the Neo4j® database, imports data from the database into MATLAB®, analyzes the data, and closes the database connection. Then, you can deploy the code by compiling it as a standalone application by using the Standalone Application Compiler (MATLAB Compiler) app and running the application on other machines.
Ensure that you have administrator privileges on the other machines to run the standalone application.
Create Function in MATLAB
Write a MATLAB script named findShortestPathBetweenPeople.m
and save it in a file location of your choice. The script contains the findShortestPathBetweenPeople
function, which returns the distance between two people in a graph network. The function performs these actions:
Connects to a Neo4j database running on the local machine
Imports graph data and converts it to a directed graph
Performs the shortest path analysis
Closes the database connection
type findShortestPathBetweenPeople.m
function distance = findShortestPathBetweenPeople(userA,userB) % FINDSHORTESTPATHBETWEENPEOPLE The findShortestPathBetweenPeople function % connects to a Neo4j® database, imports data from the database into % MATLAB®, finds the shortest path between two people, and closes the % database connection. %% % Create a Neo4j connection object |neo4jconn| using the URL % |http://localhost:7474/db/data|, user name |neo4j|, and password % |matlab|. url = 'http://localhost:7474/db/data'; username = 'neo4j'; password = 'matlab'; neo4jconn = neo4j(url,username,password); %% % Find all the |Person| nodes and all the relationships associated with % each |Person| node using |searchGraph|. social_graphdata = searchGraph(neo4jconn,{'Person'}); %% % Using the table |social_graphdata.Nodes|, access the |name| property for % each node that appears in the |NodeData| variable of the table. % % Assign the table |social_graphdata.Nodes| to |nodestable|. nodestable = social_graphdata.Nodes; %% % Assign the row names for each row in the table |nodestable| to % |rownames|. rownames = nodestable.Properties.RowNames; %% % Access the |NodeData| variable from |nodestable| for each row. |nodedata| % contains an array of structures. nodedata = [nodestable.NodeData{rownames}]; %% % To retrieve the |name| field from each structure, index into the array. % |nodenames| is a cell array of character vectors that contains node names. nodenames = {nodedata(:).name}; %% % Create the |digraph| object |social_graph| using the % |neo4jStruct2Digraph| function with the graph data stored in % |social_graphdata| and the node names stored in |nodenames|. social_graph = neo4jStruct2Digraph(social_graphdata,'NodeNames',nodenames); %% % Find the shortest path between |UserA| and |UserB| using |shortestpath|. [~,distance] = shortestpath(social_graph,userA,userB); %% % Close the database connection. close(neo4jconn)
Create Standalone Application Using Application Compiler App
On the MATLAB Apps tab, on the far right of the Apps section, click the arrow to open the apps gallery. Under Application Deployment, click Standalone Application Compiler.
After you open the app, the Create Compiler Task dialog box prompts you to add a compiler task to a new or an existing MATLAB® project. For this example, select Start a new project and create a compiler task and create a new project in your working folder. For more information on creating and using MATLAB® projects, see Create Projects.
A new compiler task named StandaloneDesktopApp1 opens in the Editor. You can compile code for other deployment targets by opening the Compiler Task Manager app or going to the Manage Tasks tab and creating a new compiler task.
For this example, in the Main File section of the compiler task, click Add Main File and select findShortestPathBetweenPeople.m
. In the Project panel, the file now has the labels Design and Main Function. To set up the rest of the standalone application, see Specify Build Options (MATLAB Compiler).
See Also
neo4j
| searchGraph
| neo4jStruct2Digraph
| close
Topics
- Create Functions in Files
- Create Standalone Application from MATLAB (MATLAB Compiler)
- Deploy Applications and MATLAB Runtime on Network Drives (MATLAB Compiler)
- Testing Failures (MATLAB Compiler)