Getting Started with the MapR Database JSON REST API

A simple way to invoke the MapR Database JSON REST API is to use cURL commands. This section contains a sequence cURL commands that demonstrate the basic functionality of the API.

Note: The MapR Database JSON REST API is available starting in the MEP 5.0 release.
The operations shown are the following:
  • Create a MapR Database JSON table
  • Insert documents into the table
  • Retrieve documents from the table, including retrievals that contain field projections and conditions
  • Update individual documents and fields within a document

To learn about the complete API, see the reference material at Understanding the MapR Database JSON REST API.

The examples in this section assume that you installed the MapR Data Access Gateway on the host 10.10.100.42. The examples use HTTPS with the default HTTPS port of 8243. For information about installing the Data Access Gateway, see Installing the Data Access Gateway Service.
Note: The examples URL encode the slashes in the table path (%2F) to differentiate them from the slashes in the command API.

Using Basic Authentication

The commands in this section use basic authentication. To use this form of authentication, you must pass the username and password in all commands, using the -u option.

  1. Create a MapR Database JSON table in the path /apps/employees:
    curl -X PUT \
      'https://10.10.100.42:8243/api/v2/table/%2Fapps%2Femployees' \
      -u root:mapr
  2. Insert 3 documents into the table:
    curl -X POST \
      'https://10.10.100.42:8243/api/v2/table/%2Fapps%2Femployees' \
      -u root:mapr \
      -H 'Content-Type: application/json' \
      -d '[{"_id":"user001","first_name":"John","last_name":"Doe"},
     {"_id":"user002","first_name":"Jane","last_name":"Doe"},
     {"_id":"user003","first_name":"Simon","last_name":"Davis"}]'
  3. Retrieve all of the documents:
    curl -X GET \
      'https://10.10.100.42:8243/api/v2/table/%2Fapps%2Femployees%2F' \
      -u root:mapr
    The command returns the following:
    {
        "DocumentStream": [
            {
                "_id": "user001",
                "first_name": "John",
                "last_name": "Doe"
            },
            {
                "_id": "user002",
                "first_name": "Jane",
                "last_name": "Doe"
            },
            {
                "_id": "user003",
                "first_name": "Simon",
                "last_name": "Davis"
            }
        ]
    }
  4. Limit the GET request to 2 documents starting at offset 1:
    curl -X GET \
      'https://10.10.100.42:8243/api/v2/table/%2Fapps%2Femployees%2F?offset=1&limit=2' \
      -u root:mapr
    The command returns the following:
    {
        "DocumentStream": [
            {
                "_id": "user002",
                "first_name": "Jane",
                "last_name": "Doe"
            },
            {
                "_id": "user003",
                "first_name": "Simon",
                "last_name": "Davis"
            }
        ]
    }
  5. Retrieve only the first names in the documents:
    curl -X GET \
    'https://10.10.100.42:8243/api/v2/table/%2Fapps%2Femployees?fields=first_name' \
      -u root:mapr
    The command returns the following:
    {
        "DocumentStream": [
            {
                "first_name": "John"
            },
            {
                "first_name": "Jane"
            },
            {
                "first_name": "Simon"
            }
        ]
    }
  6. Retrieve all documents with a last name of 'Doe':
    curl -g -X GET \
    'https://10.10.100.42:8243/api/v2/table/%2Fapps%2Femployees?condition={"$eq":{"last_name":"Doe"}}' \
      -u root:mapr
    Note: You must pass '-g' in the cURL command due to the nested braces in the condition.
    The command returns 2 documents:
    {
        "DocumentStream": [
            {
                "_id": "user001",
                "first_name": "John",
                "last_name": "Doe"
            },
            {
                "_id": "user002",
                "first_name": "Jane",
                "last_name": "Doe"
            }
        ]
    }
  7. Retrieve the id and first name of documents with a last name of 'Doe':
    curl -g -X GET \
    'https://10.10.100.42:8243/api/v2/table/%2Fapps%2Femployees?condition={"$eq":{"last_name":"Doe"}}&fields=_id,first_name' \
      -u root:mapr
    Note: You must pass '-g' in the cURL command due to the nested braces in the condition.
    The command returns the following:
    {
        "DocumentStream": [
            {
                "_id": "user001",
                "first_name": "John"
            },
            {
                "_id": "user002",
                "first_name": "Jane"
            }
        ]
    }
  8. Run the same command, also retrieving the query plan:
    curl -g -X GET \
    'https://10.10.100.42:8243/api/v2/table/%2Fapps%2Femployees?condition={"$eq":{"last_name":"Doe"}}&fields=_id,first_name&getPlan=true' \
      -u root:mapr
    The output includes the query plan:
    {
        "DocumentStream": [
            {
                "_id": "user001",
                "first_name": "John"
            },
            {
                "_id": "user002",
                "first_name": "Jane"
            }
        ],
        "QueryPlan": [
            [
                {
                    "streamName": "DBDocumentStream",
                    "parameters": {
                        "queryConditionPath": true,
                        "projectionPath": [
                            "_id",
                            "first_name"
                        ],
                        "primaryTable": "/apps/employees"
                    }
                }
            ]
        ]
    }
  9. Update the first name in one of the documents, specifying the id in the command:
    curl -X POST \
    'https://10.10.100.42:8243/api/v2/table/%2Fapps%2Femployees/document/user001' \
      -H 'Content-Type: application/json' \
      -u root:mapr \
      -d '{"$set":{"first_name":"Jay"}}'
  10. Retrieve the updated document, specifying the id in the command:
    curl -X GET \
    'https://10.10.100.42:8243/api/v2/table/%2Fapps%2Femployees/document/user001' \
      -u root:mapr
    The document contains an updated first name:
    {
        "_id": "user001",
        "first_name": "Jay",
        "last_name": "Doe"
    }
  11. Replace the same document, but this time with a user who has only a first name:
    curl -X PUT \
    'https://10.10.100.42:8243/api/v2/table/%2Fapps%2Femployees/document/user001' \
      -H 'Content-Type: application/json' \
      -u root:mapr \
      -d '{"_id":"user001","first_name":"Jonathan"}'
  12. Retrieve the new document by passing the id in the request:
    curl -X GET \
    'https://10.10.100.42:8243/api/v2/table/%2Fapps%2Femployees/document/user001' \
      -u root:mapr

    The document contains only a first name:

    {
        "_id": "user001",
        "first_name": "Jonathan"
    }

Using Token-Based Authentication

To use token-based authentication, you first create a token, authenticating with a username and password. You then pass the generated token in all subsequent commands.

  1. Generate a token using the following command:
    curl -X POST \
      'https://10.10.100.42:8243/auth/v2/token' \
      -u root:mapr
    The command returns the following token:
    {
        "token": "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJyb290IiwiYXVkIjoid2ViIiwiZXhwIjoxNTIwMjY5MTQwLCJpYXQiOjE1MjAyNjczNDB9.NT8L2deiA6v55bfbU_opiG1XXGPP0IwfSex3jW5A1ZsoI1ar09it7-XwNtRqfL_I29IHLyfmuHcT5eSIpwq6ng"
    }
  2. Pass the token in your cURL command, as shown in the following GET:
    curl -X GET \
      https://10.10.100.42:8243/api/v2/table/%2Fapps%2Femployees \
      -H 'Authorization: Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJyb290IiwiYXVkIjoid2ViIiwiZXhwIjoxNTIwMjY5MTQwLCJpYXQiOjE1MjAyNjczNDB9.NT8L2deiA6v55bfbU_opiG1XXGPP0IwfSex3jW5A1ZsoI1ar09it7-XwNtRqfL_I29IHLyfmuHcT5eSIpwq6ng'