Updated on 2023-07-19 GMT+08:00

Database-Related SDKs

Prerequisites

  • You have configured the Java SDK environment by following the instructions provided Instructions.
  • You have initialized the DLI Client by following the instructions provided in Initializing the DLI Client and created queues by following the instructions provided in Queue-Related SDKs.

Creating a Database

DLI provides an API for creating a database. You can use the API to create a database. The sample code is as follows:

1
2
3
4
5
6
7
  private static Database createDatabase(DLIClient client) throws DLIException {
    //Call the createDatabase method of the DLIClient object to create a database.
    String dbName = "databasename";
    Database database = client.createDatabase(dbName);
    System.out.println("create database:" + database);
    return database;
  }

The default database is a built-in database. You are not allowed to create a database named default.

Deleting a Database

DLI provides an API for deleting a database. The example code is as follows:

1
2
3
4
5
6
7
8
  //Call the deleteDatabase interface of the Database object to delete a database.
  //Call the getDatabase(String databaseName) interface of the DLIClient object to obtain the Database object.
private static void deletedatabase(Database database) throws DLIException {
    String dbName = "databasename";
    database=client.getDatabase(dbName);
    database.deleteDatabase();
    System.out.println("delete db " + dbName);
}
  • A database that contains tables cannot be deleted. To delete a database that contains tables, delete the tables first.
  • A deleted database cannot be restored. Therefore, exercise caution when deleting a database.

Querying All Databases

You can use the API provided by DLI to query the list of created databases. The example code is as follows:

1
2
3
4
5
6
7
  private static void listDatabases(DLIClient client) throws DLIException {
    //Call the listAllDatabases method of the DLIClient object to query the database list.
    List<Database> databases = client.listAllDatabases();
    for (Database db : databases) {
      System.out.println("dbName:" + db.getDatabaseName() + " " + "tableCount:" + db.getTableCount());
    }
  }