Connect a Data Source

A data source in MindsDB refers to any external database, data warehouse, application, or other source of data that you can connect to MindsDB.

Description

The getDatabase() and createDatabase() methods of Server class Object enable you to use the existing data source or connect a new one.

Syntax

Get Database

You can use the getDatabase(String name) method to get an existing database:

/**
 * Retrieves a database by its name.
 *
 * @param name         Name of the database to retrieve
 * @return Database    Database object corresponding to the specified name
 * @throws IllegalArgumentException 
 *         if the database with the specified name does not exist
 */
public Database getDatabase(String name) {
    return databases.get(name);
}

Example

package org.example;

import mindsdb.MindsDB;
import mindsdb.models.Database;
import mindsdb.models.Project;
import mindsdb.services.Server;

public class Main {
    public static void main(String[] args) {
        Server server = MindsDB.connect();
        Database mariadb = server.getDatabase("maria");
    }
}

Create Database

Or, the createDatabase() method to connect a new data source to MindsDB:

/**
 * Create a database
 * 
 * @param name            Name of the database
 * @param engine          Engine name
 * @param connectionArgs  Connection arguments
 * @return Database
 */
public Database createDatabase(String name, String engine, Map<String, String> connectionArgs);

/**
 * Create a database
 * 
 * @param name            Name of the database
 * @param engine          Handler object
 * @param connectionArgs  Connection arguments
 * @return Database
 */
public Database createDatabase(String name, Handler engine, Map<String, String> connectionArgs);

Example

package org.example;

import mindsdb.MindsDB;
import mindsdb.models.Database;
import mindsdb.services.Server;

import java.util.Map;

public class Main {
    public static void main(String[] args) {
        Server server = MindsDB.connect();
        Database mariadb = server.createDatabase("mariadb", "mariadb", Map.of(
                "host", "localhost",
                "port", "3306",
                "database", "your_database",
                "user", "your_username",
                "password", "your_password"
        ));
    }
}
Updated on