DEV Community

Cover image for Quick tip: Using SingleStoreDB's Data API
Akmal Chaudhri for SingleStore

Posted on • Edited on

Quick tip: Using SingleStoreDB's Data API

Abstract

SingleStoreDB provides a Data API that can be used to execute SQL statements over an HTTP connection. This short article will show examples of how to use this Data API.

Create a SingleStoreDB Cloud account

A previous article showed the steps required to create a free SingleStoreDB Cloud account. We'll use Iris Demo Group as our Workspace Group Name and iris-demo as our Workspace Name. We'll make a note of our password and host name.

Test the Data API

The Data API is enabled by default, and we can test it quite quickly from the command line, as follows:

curl https://<host>/ping
Enter fullscreen mode Exit fullscreen mode

We'll replace the <host> with the value from our SingleStoreDB Cloud account.

The result should be:

pong
Enter fullscreen mode Exit fullscreen mode

Create a database

Let's now create a database:

curl -u "admin:<password>" -H "Content-Type: application/json" --data '{"sql" : "CREATE DATABASE IF NOT EXISTS iris_demo"}' https://<host>/api/v2/exec
Enter fullscreen mode Exit fullscreen mode

We'll replace the <password> and <host> with the values from our SingleStoreDB Cloud account.

The result should be:

{"lastInsertId":0,"rowsAffected":1}
Enter fullscreen mode Exit fullscreen mode

Create a table

Let's now create a table:

curl -u "admin:<password>" -H "Content-Type: application/json" --data '{"sql" : "CREATE TABLE IF NOT EXISTS iris (sepal_length FLOAT, sepal_width FLOAT, petal_length FLOAT, petal_width FLOAT, species VARCHAR(20))", "database" : "iris_demo"}' https://<host>/api/v2/exec
Enter fullscreen mode Exit fullscreen mode

The result should be:

{"lastInsertId":0,"rowsAffected":0}
Enter fullscreen mode Exit fullscreen mode

Load data into table

We'll now load some data into our database using a MySQL client.

First, we'll download a CSV file containing the Iris flower data set.

Next, we'll launch the client:

mysql --local-infile -u admin -h <host> -P 3306 --default-auth=mysql_native_password -p
Enter fullscreen mode Exit fullscreen mode

The <host> being replaced with the value obtained from SingleStoreDB Cloud.

Finally, we'll load the CSV file data into our database table, as follows:

USE iris_demo;

LOAD DATA LOCAL INFILE '/path/to/iris.csv'
INTO TABLE iris
IGNORE 1 LINES
COLUMNS TERMINATED BY ',';
Enter fullscreen mode Exit fullscreen mode

We would replace /path/to/ with the actual path to where the CSV file was located.

Perform a query

We can then perform queries, such as:

curl -u "admin:<password>" -H "Content-Type: application/json" --data '{"sql" : "SELECT * FROM iris LIMIT 5", "database" : "iris_demo"}' https://<host>/api/v2/query/rows
Enter fullscreen mode Exit fullscreen mode

The result should be similar to the following:

{
  "results": [
    {
      "rows": [
        {
          "sepal_length": 5.5,
          "sepal_width": 4.2,
          "petal_length": 1.4,
          "petal_width": 0.2,
          "species": "Iris-setosa"
        },
        {
          "sepal_length": 6.4,
          "sepal_width": 3.2,
          "petal_length": 4.5,
          "petal_width": 1.5,
          "species": "Iris-versicolor"
        },
        {
          "sepal_length": 5.6,
          "sepal_width": 2.5,
          "petal_length": 3.9,
          "petal_width": 1.1,
          "species": "Iris-versicolor"
        },
        {
          "sepal_length": 5.5,
          "sepal_width": 2.5,
          "petal_length": 4,
          "petal_width": 1.3,
          "species": "Iris-versicolor"
        },
        {
          "sepal_length": 6.4,
          "sepal_width": 2.7,
          "petal_length": 5.3,
          "petal_width": 1.9,
          "species": "Iris-virginica"
        }
      ]
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

Summary

The SingleStoreDB Data API provides another way that we can work with SingleStoreDB. This method could be useful for certain types of applications and integrations. This is another example of the flexibility that SingleStoreDB provides.

Top comments (0)