Creating JSON Tables

This topic describes how to create MapR-DB JSON tables using either programmatic APIs or dbshell.

Note: Before creating a table, you typically create a directory and MapR volume. This is not required; however, it is a good practice. For example, assuming both the directory and volume names are sample, the command would the following.
// Create directories with hadoop
hadoop fs -mkdir /sample

// Create a MapR volume using maprcli create volume
maprcli volume create -name sample -path /sample -type rw

The following Java code examples show you how to create a table in the following ways:

  • By using the default values for the table attributes,
  • By setting specific values for the table attributes.

See the Admin and TableDescriptor APIs for more information.

The following example shows how to create a table by calling an Admin object's createTable() method and passing, as an argument, the path that you want to use for the new table:

public void createJSONTable(String tablePath) throws DBException {
    try (Admin admin = MapRDB.newAdmin()) {
        if (!admin.tableExists(tablePath)) {
            admin.createTable(tablePath);
         }
    }
}

Tables created with this version of the createTable() method use the default values for their attributes.

Alternatively, the following example how to create a table by passing a TableDescriptor object as an argument to the createTable() method:

/* Create a TableDescriptor for the table to create,
 * passing in the path of the table. 
 */
TableDescriptor tableDescriptor = MapRDB.newTableDescriptor(tablePath);
   
/* Pass the TableDescriptor object and the path to the table
 * to the Admin.createTable() method. 
 */
public void createJSONTable(String tablePath, TableDescriptor tableDescriptor) throws DBException {
    try (Admin admin = MapRDB.newAdmin()) {
        if (!admin.tableExists(tablePath)) {
            admin.createTable(tableDescriptor);
        }
    }
}

This alternative allows you to set values for some of the table's attributes.

The following dbshell command shows code syntax for creating a table:

# mapr dbshell
maprdb root:> create /<tablePath>/<tableName>