![]() |
VOOZH | about |
The CData API Server, when paired with the Presto connector (or any of over 300 supported data sources), exposes Presto data as an OData web service that can be queried from Node.js using simple HTTP requests. This article walks you through setting up the CData API Server and demonstrates the process to retrieve JSON-formatted Presto data in Node.js.
Accessing and integrating live data from Trino and Presto SQL engines has never been easier with CData. Customers rely on CData connectivity to:
Presto and Trino allow users to access a variety of underlying data sources through a single endpoint. When paired with CData connectivity, users get pure, SQL-92 access to their instances, allowing them to integrate business data with a data warehouse or easily access live data directly from their preferred tools, like Power BI and Tableau.
In many cases, CData's live connectivity surpasses the native import functionality available in tools. One customer was unable to effectively use Power BI due to the size of the datasets needed for reporting. When the company implemented the CData Power BI Connector for Presto they were able to generate reports in real-time using the DirectQuery connection mode.
If you haven't already, download and install the CData API Server. After installation, run the application, download the Presto connector from within the API Server, and configure your Presto data connection. Then, configure API Server to generate OData feeds for any tables you want to access in your single-page application (SPA).
AngularJS requires servers to have CORS (Cross-Origin Resource Sharing) enabled. To enable CORS, go to the Server tab under the Settings section in the API Server and adjust the following settings:
To work with Presto data using AngularJS, we start by creating and configuring a Presto connection. Follow the steps below to configure the API Server to connect to Presto data:
Next, create a user to access your Presto data through the API Server. You can add and configure users on the Users page. Follow the steps below to configure and create a user:
Having created a user, you are ready to create API endpoints for the Presto tables:
Having configured a connection to Presto data, created a user, and added resources to the API Server, you now have an easily accessible REST API based on the OData protocol for those resources. From the API page in API Server, you can view and copy the API Endpoints for the API:
π API EndpointsJust like with standard OData feeds, you can limit the fields returned by adding a $select parameter to the query. You can also include other standard OData URL parameters, such as $filter, $orderby, $skip, and $top. Refer to the help documentation for more details on supported OData queries.
Select the user you created, then copy and save the authtoken for future use. You can also toggle on 'Token Expiration' and set the number of days based on your use case. Click Save to save the details.
π Copy and save authtokenOData feeds provide a standardized way to access data and can be easily consumed in Node.js. Node.js scripts use the built-in http module to make requests to the CData API Server, which acts as an intermediary to expose Presto data as OData. The process involves constructing HTTP GET requests to the API Server's OData endpoints, handling the responses, and parsing the JSON data.
To get started, we'll create a new Node.js project and install the necessary packages to run it.
cd D:\ mkdir my-odata-app cd my-odata-app npm init -y
npm install http
In this section, we'll work on retrieving specific records from a Presto entity by filtering based on a unique identifier using the app_data.js script.
var http = require('http');
// Function to fetch data based on a given Id
function fetchDataById(id) {
http.get({
protocol: "http:",
hostname: "localhost",
port: 8080,
path: "/api.rsc/DEMO_DB_CRM_Account?$filter=" + encodeURIComponent("Id eq '" + id + "'"), // Updated URL with filter
auth: 'your_username:your_authtoken' // Enter your username and authtoken/password
}, function (res) {
var body = '';
res.on('data', function (chunk) {
body += chunk;
});
res.on('end', function () {
var jsonData = JSON.parse(body);
console.log(jsonData);
});
}).on('error', function (e) {
console.log("Error: ", e);
});
}
// Example usage: Fetch data for Id '11'
fetchDataById('11');
Retrieve the specific records for an unique identifier from the Presto entity, using the following command in the Terminal or Command Prompt:
node app_data.js
The app_table.js script focuses on fetching all records from a specified Presto entity.
var http = require('http');
http.get({
protocol: "http:",
hostname: "localhost",
port: 8080,
path: "/api.rsc/DEMO_DB_CRM_Account", // Updated URL to fetch data from DEMO_DB_CRM_Account
auth: 'your_username:your_password' // Enter your username and password/authtoken
}, function (res) {
var body = '';
res.on('data', function (chunk) {
body += chunk;
});
res.on('end', function () {
console.log(body);
var jsonData = JSON.parse(body);
console.log(jsonData);
});
}).on('error', function (e) {
console.log("Error: ", e);
});
Retrieve all the records from the Presto entity, using the following command:
node app_table.js
Accessing Presto data from Node.js can be greatly simplified by leveraging an OData API. CData API Server exposes Presto data as an OData service, enabling you to perform "Read" operations using standard HTTP requests. This allows you to work with your Presto data in a more structured and standardized way.
To see how API Server can simplify your data access, download a free, 30-day trial of CData API Server today! For more general information on our API Server and to see what other data sources we support, refer to our API Server page.Learn more or sign up for a free trial:
CData API Server