Much of the underlying infrastructure you need to interact with a HEAVY.AI server is provided by the mapd-connector repository. You can clone the repository locally to take advantage of its utilities.
Open mapd-connector/examples/browser.html in a web browser to connect to a sample HEAVY.AI server instance and display information from the sample database. You can open mapd-connector/examples/browser.js in a text editor and follow along as the following sections describe how the script connects, retrieves, and displays data from the HEAVY.AI server.
Connect
The lynchpin of the application is establishing a connection to HEAVY.AI. Fortunately, the MapdCon object does the heavy lifting for you. You provide the location and port of the HEAVY.AI host and your user credentials to create a connection.
Connecting to the HEAVY.AI server is not the final goal, of course. Once you have a connection, you can send SQL queries to retrieve data.
First, create constant variables for your SQL queries.
// The total number of tweets from Columbia
const query = "SELECT count(*) AS n FROM tweets_nov_feb WHERE country='CO'"
// try changing airtime to arrdelay in the query
const query2 =
"SELECT carrier_name as key0, AVG(airtime) AS val FROM flights_donotmodify WHERE airtime IS NOT NULL GROUP BY key0 ORDER BY val DESC LIMIT 100"
Use a promise to send a series of queries, including the ones defined as constants.
.then(session =>
// now that we have a session open we can make some db calls:
Promise.all([
session.getTablesAsync(),
session.getFieldsAsync("flights_donotmodify"),
session.queryAsync(query, defaultQueryOptions),
session.queryAsync(query2, defaultQueryOptions)
])
)
Display
Promise returns a values object with an array of query results. The first item in the values array is a list of tables. Write the results to the browser console.
.then(values => {
console.log(
"All tables available at metis.mapd.com:",
values[0].map(x => x.name)
)
The second item in the array contains a list of fields in the table flights_donotmodify.
// handle result of getFieldsAsync
console.log(
"All fields for 'flights_donotmodify':",
values[1].reduce((o, x) => Object.assign(o, { [x.name]: x }), {})
)
The third query contains a count of Twitter™ tweets from Columbia. Display that in the HTML page using the DOM.
// handle result of first query
document.getElementById("result-async").innerHTML =
"There are " + values[2][0].n + " tweets from Columbia."
console.log("Query 1 results:", Number(values[2][0].n))
The final query returns the average air time for flights listed in the flights_donotmodify table, with a limit of 100 records. Display the results as a chart using the createRowChart method.