w39hh
w39hh

Reputation: 11

What steps are required to add support for Phoenix to ActiveJDBC?

I am trying to add some support for Apache Phoenix to ActiveJDBC. I am using the ActiveJDBC simple-example project as test, and making changes to a clone of ActiveJDBC 2.0-SNAPSHOT (latest from github).

So far in ActiveJDBC 2.0-SNAPSHOT I have:

In the simple-example project I have:

However, the database dialect is not being recognized, and is, I believe, defaulting to the DefaultDialect as I get a Phoenix error on the use of "INSERT" which is not recognized in the Phoenix grammar. Phoenix grammar

Are there additional steps I am missing when adding support for an additional dialect?

I also suspect the Phoenix jdbc driver may not support a getDbName() type method, the Phoenix driver, when asked for getPropertyInfo() returns EMPTY_INFO, see PhoenixEmbeddedDriver

If the driver does not return the DbName, is there a workaround?

It might be worth mentioning we are successfully interacting with Phoenix using standard Java jdbc classes (PreparedStatement and all that good stuff), but ActiveJDBC is much more elegant and we would like to use it.

Pieces of what we have so far:

PhoenixDialect

import java.util.Iterator;
import java.util.Map;

import org.javalite.activejdbc.MetaModel;
import static org.javalite.common.Util.join;

public class PhoenixDialect extends DefaultDialect {

    @Override
    public String insert(MetaModel metaModel, Map<String, Object> attributes) {
       StringBuilder query = new StringBuilder().append("UPSERT INTO ").append(metaModel.getTableName()).append(' ');
       if (attributes.isEmpty()) {
           appendEmptyRow(metaModel, query);
       } else {
           boolean addIdGeneratorCode = (metaModel.getIdGeneratorCode() != null
                   && attributes.get(metaModel.getIdName()) == null); // do not use containsKey
           query.append('(');
           if (addIdGeneratorCode) {
               query.append(metaModel.getIdName()).append(", ");
           }
           join(query, attributes.keySet(), ", ");
           query.append(") VALUES (");
           if (addIdGeneratorCode) {
               query.append(metaModel.getIdGeneratorCode()).append(", ");
           }
           Iterator<Object> it = attributes.values().iterator();
           appendValue(query, it.next());
           while (it.hasNext()) {
               query.append(", ");
               appendValue(query, it.next());
           }
           query.append(')');
       }
       return query.toString();
   }
}

Configuration

   public Dialect getDialect(String dbType) {

      Dialect dialect = dialects.get(dbType);
      if (dialect == null) {
         if (dbType.equalsIgnoreCase("Oracle")) {
            dialect = new OracleDialect();
         }
         else if (dbType.equalsIgnoreCase("Phoenix")) {
            dialect = new PhoenixDialect();
         }
         else if (dbType.equalsIgnoreCase("MySQL")) {
            dialect = new MySQLDialect();
         }

database.properties

development.driver=org.apache.phoenix.jdbc.PhoenixDriver
development.username=anything
development.password=anything
development.url=jdbc:phoenix:hdp-c21:2181:/hbase-unsecure

Upvotes: 1

Views: 101

Answers (1)

ipolevoy
ipolevoy

Reputation: 5518

Here is a branch that was used to integrate SQLServer with new Dialect, test suite and other related stuff:

https://github.com/javalite/activejdbc/tree/sql_server_integration

Here is a branch for h2:

https://github.com/javalite/activejdbc/commits/h2integration

Things may have changed since then, but this branch will give you good guidance. Best if you fork the project, and when done submit your work as a pull request.

Upvotes: 0

Related Questions