Search code examples
javajdbcmariadbapache-nifiapache-commons-dbcp

Apache NIFI custom processor gives error "cannot find suitable driver"


I have created a nifi processor that is supposed to read something from a database and put the result in an attribute. The code is very trivial, just creating a simple JDBC connection.

private Connection getDatabaseConnection(ProcessContext context){
    if(databaseConnection == null) {
        try {
            String url = createJDBCUrl(context);
            databaseConnection = DriverManager.getConnection(url,
                   context.getProperty(DB_USER_NAME).getValue(),
                   context.getProperty(DB_PASSWORD).getValue());
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }
    }
    return databaseConnection;
}

private String createJDBCUrl(ProcessContext context) {
    String ip = context.getProperty(DB_IP).getValue();
    String port = context.getProperty(DB_PORT).getValue();
    String dbName = context.getProperty(DB_NAME).getValue();
    return "jdbc:mariadb://" + ip + ":" + port + "/" + dbName;
}

I get this error:

No suitable driver found for jdbc:mariadb://databaste-ip:3306/database-name

I have tried putting mysql-connector-java-5.1.49 file in nifi lib folder but no luck. Also I put the file in resource folder to bundle the driver with the processor but no success as well. Also I wrote the processor differently and used BasicDataSource from Apache commons-dbcp, so I could define where the driver file is located but again I got an error:

Cannot create JDBC driver of class 'com.mysql.jdbc.Driver' for connect URL 'jdbc:mariadb://database-ip:3306/database-name'

Solution

  • If you're using the jdbc:mariadb: protocol, then you should use the MariaDB Connector/J, not the MySQL Connector/J driver. The MySQL Connector/J driver only knows about the jdbc:mysql: protocol.

    Or alternatively, if you want to continue to use MySQL Connector/J, then you should use protocol jdbc:mysql:. However, given MySQL and MariaDB are - I assume - diverging, it is better to use the driver specifically written for MariaDB.