Send data to Zerobus Ingest from MATLAB
The MATLAB Interface for Databricks is a MathWorks package. MathWorks labels its Zerobus support as Preview and says the interface may change without notice. To use it, install the package as described in Step 2. For the MATLAB class reference and the release notes, see the MathWorks Zerobus documentation.
Maintained by: MathWorks, in the mathworks/MATLAB-Interface-for-Databricks repository. Databricks maintains the Zerobus Ingest steps.
This page shows how to write rows from MATLAB to a table in Unity Catalog with Zerobus Ingest. The MATLAB Interface for Databricks includes a databricks.Zerobus client. You create the client with a service principal's client ID and secret, and then call its insert method with a JSON array of rows. Each call sends those rows to the table through the Zerobus Ingest REST API, so you don't need to run a message bus between MATLAB and the table.
When to use this path
- You work in MATLAB or Simulink and want records from a MATLAB workflow in a Zerobus Ingest table.
- Your records are JSON. The client accepts JSON payloads only.
For a client in a language other than MATLAB, or a high-throughput client built from scratch, use a Zerobus Ingest SDK instead. See Write a client.
How it works
- Interface:
databricks.Zerobussends eachinsertcall as one HTTP POST to the Zerobus Ingest REST endpoint for your table. The body is the JSON payload that you pass in. - Authentication: The client exchanges the service principal's client ID and secret for an OAuth token. The token request names the catalog, the schema, and the table, and asks for
SELECTandMODIFYon that table. Tokens last one hour. With the default settings, the client requests a new token before eachinsertcall. For the token details, see Configure authentication. - Delivery: Zerobus Ingest can deliver a record more than one time when the client retries a record that isn't acknowledged. The MATLAB client doesn't retry a failed
insertcall, so your code decides whether to retry. A retry can write the same row twice. See Delivery guarantees. - Schema: Zerobus Ingest checks each record against the table schema. By default, it rejects records with fields that don't match the table. To capture those fields instead, configure a rescue column.
Requirements
- MATLAB R2024b or later: The MATLAB Interface for Databricks requires R2024b or later.
- Supported platform: The MathWorks support matrix lists Windows, Linux, and macOS for the package's other workflows. It doesn't list Zerobus, so platform support for the Zerobus workflow isn't confirmed. Check the support matrix before you install.
- MATLAB Interface for Databricks: Installed, with
setuprun for your workspace. See Step 2. - Workspace in a supported region: A Databricks workspace in a region that supports Zerobus Ingest. See Availability.
- Service principal: With an OAuth secret and the Workspace access entitlement. See Configure authentication.
- Target table: In Unity Catalog, and meets the Table requirements.
Create the table and grant access
Run the statements in a Databricks notebook or the SQL editor. The table matches the MATLAB example in Step 4:
CREATE TABLE main.default.air_quality (device_name STRING, temp INT, humidity LONG);
Grant the service principal access to the catalog, the schema, and the table. Replace <service-principal-client-id> with the service principal's client ID:
GRANT USE CATALOG ON CATALOG main TO `<service-principal-client-id>`;
GRANT USE SCHEMA ON SCHEMA main.default TO `<service-principal-client-id>`;
GRANT MODIFY, SELECT ON TABLE main.default.air_quality TO `<service-principal-client-id>`;
Grant MODIFY and SELECT by name. Don't rely on ALL PRIVILEGES alone. The client's token request doesn't set the operations field that lets ALL PRIVILEGES satisfy a Zerobus write token. These statements are from the MathWorks Zerobus example.
Install the interface and run setup
The MathWorks README gives two install paths for MATLAB Desktop. The first is to download the .mltbx file from the Assets section of a release, then right-click it in the Current Folder browser and select Install.
The second is to paste this into the MATLAB Command Window:
mltbxFile = websave(fullfile(tempdir, "matlab-databricks.mltbx"), ...
"https://github.com/mathworks/MATLAB-Interface-for-Databricks/releases/latest/download/matlab-databricks.mltbx");
if isMATLABReleaseOlderThan("R2026b")
matlab.addons.install(mltbxFile)
else
mpminstall(mltbxFile)
end
To check the installed version, run matlab.addons.installedAddons. The MathWorks README gives this check for both install paths.
Then run setup in MATLAB. It guides you through the workspace URL, authentication, and connection settings. For the supported authentication methods, see Authentication in the MathWorks repository. This step also records the workspace that the Zerobus client uses.
Get the Zerobus Ingest endpoint
By default, the client builds the endpoint from your workspace ID and the region of the Unity Catalog metastore:
https://<workspace-id>.zerobus.<region>.cloud.databricks.com
To find your workspace ID and region, see Get your workspace URL and Zerobus Ingest endpoint.
The default lookup reads the workspace ID from the setup profile and calls the Unity Catalog metastore summary for the region. If that call fails, the client stops with an error. To skip the lookup, pass the endpoint with the endpoint argument in Step 4.
Create the Zerobus client
Create the client with the service principal's credentials. The table names match the table you created in Step 1:
z = databricks.Zerobus(catalog="main", schema="default", table="air_quality", clientId="<client-id>", clientSecret="<client-secret>")
Keep the client secret out of your code. The MathWorks example recommends the MATLAB loadenv and setsecret functions for this.
The client also accepts an endpoint argument, which sets the Zerobus Ingest URL from Step 3. Replace <zerobus-endpoint> with that URL:
z = databricks.Zerobus(catalog="main", schema="default", table="air_quality", endpoint="<zerobus-endpoint>", clientId="<client-id>", clientSecret="<client-secret>")
Insert rows
Build a JSON array of rows and pass it to insert. The example inserts two rows:
payload = ['[{ "device_name": "device_num_1", "temp": 28, "humidity": 60 },', newline ...
'{ "device_name": "device_num_2", "temp": 25, "humidity": 55 }]'];
% Insert the data into the table
[result, errorResponse] = z.insert(payload);
The result output is true when the call returns HTTP 200. Otherwise, result is false, and errorResponse describes the failure.
The MathWorks docs say that one call with several rows is more efficient than one call per row when the data is available up front. Reuse the same client for repeated inserts, because a new client for each call adds overhead. For a longer loop, see the MathWorks example.
Check the table
Query the table from a notebook or the SQL editor:
SELECT * FROM main.default.air_quality LIMIT 100;
The query should show the two rows that Step 5 inserted. The MathWorks example notes that the notebook typically trails the MATLAB code by a few seconds, so refresh the query if the rows don't appear right away.
Troubleshooting
Symptom | Cause | Fix |
|---|---|---|
| The client ID or secret is wrong, the token request was refused, or the service principal lacks a grant or the Workspace access entitlement. Zerobus returns | Check the client ID and secret. Confirm the Workspace access entitlement, and rerun the Step 1 grants. For the error codes, see Zerobus Ingest errors. |
| The | Run |
An | The token is valid for one hour. MathWorks documents this as a known limitation: a token can expire during a large transaction, and the call fails without a retry. | Send smaller batches. Retry the failed call in your code, and check for duplicate rows after a retry. See Delivery guarantees. |
| A field name or type doesn't match a table column. Zerobus Ingest rejects records whose fields don't match the table schema, and returns | Match the JSON field names and types to the table columns. To capture extra fields instead of rejecting them, configure a rescue column. See Zerobus Ingest errors. |
Additional resources
- Releases: the
.mltbxpackages and release notes. - Zerobus example: the MathWorks example that creates the table, runs the client, and checks the rows from a notebook.
- Write a client: the REST and SDK clients for Zerobus Ingest, including the REST contract that the MATLAB client calls.