Search in sources :

Example 1 with DruidPool

use of org.smartdata.metastore.DruidPool in project SSM by Intel-bigdata.

the class MetaStoreUtils method getDBAdapter.

public static MetaStore getDBAdapter(SmartConf conf) throws MetaStoreException {
    URL pathUrl = ClassLoader.getSystemResource("");
    String path = pathUrl.getPath();
    characterTakeUpBytes = conf.getInt(SmartConfKeys.SMART_METASTORE_CHARACTER_TAKEUP_BYTES_KEY, SmartConfKeys.SMART_METASTORE_CHARACTER_TAKEUP_BYTES_DEFAULT);
    String fileName = "druid.xml";
    String expectedCpPath = path + fileName;
    LOG.info("Expected DB connection pool configuration path = " + expectedCpPath);
    File cpConfigFile = new File(expectedCpPath);
    if (cpConfigFile.exists()) {
        LOG.info("Using pool configure file: " + expectedCpPath);
        Properties p = new Properties();
        try {
            p.loadFromXML(new FileInputStream(cpConfigFile));
            String url = conf.get(SmartConfKeys.SMART_METASTORE_DB_URL_KEY);
            if (url != null) {
                p.setProperty("url", url);
            }
            String purl = p.getProperty("url");
            if (purl == null || purl.length() == 0) {
                // For testing
                purl = getDefaultSqliteDB();
                p.setProperty("url", purl);
                LOG.warn("Database URL not specified, using " + purl);
            }
            if (purl.startsWith(MetaStoreUtils.MYSQL_URL_PREFIX)) {
                String dbName = getMysqlDBName(purl);
                for (String name : DB_NAME_NOT_ALLOWED) {
                    if (dbName.equals(name)) {
                        throw new MetaStoreException(String.format("The database %s in mysql is for DB system use, " + "please appoint other database in druid.xml.", name));
                    }
                }
            }
            try {
                String pw = conf.getPasswordFromHadoop(SmartConfKeys.SMART_METASTORE_PASSWORD);
                if (pw != null && pw != "") {
                    p.setProperty("password", pw);
                }
            } catch (IOException e) {
                LOG.info("Can not get metastore password from hadoop provision credentials," + " use the one configured in druid.xml .");
            }
            for (String key : p.stringPropertyNames()) {
                if (key.equals("password")) {
                    LOG.info("\t" + key + " = **********");
                } else {
                    LOG.info("\t" + key + " = " + p.getProperty(key));
                }
            }
            return new MetaStore(new DruidPool(p));
        } catch (Exception e) {
            if (e instanceof InvalidPropertiesFormatException) {
                throw new MetaStoreException("Malformat druid.xml, please check the file.", e);
            } else {
                throw new MetaStoreException(e);
            }
        }
    } else {
        LOG.info("DB connection pool config file " + expectedCpPath + " NOT found.");
    }
    // Get Default configure from druid-template.xml
    fileName = "druid-template.xml";
    expectedCpPath = path + fileName;
    LOG.info("Expected DB connection pool configuration path = " + expectedCpPath);
    cpConfigFile = new File(expectedCpPath);
    LOG.info("Using pool configure file: " + expectedCpPath);
    Properties p = new Properties();
    try {
        p.loadFromXML(new FileInputStream(cpConfigFile));
    } catch (Exception e) {
        throw new MetaStoreException(e);
    }
    String url = conf.get(SmartConfKeys.SMART_METASTORE_DB_URL_KEY);
    if (url != null) {
        p.setProperty("url", url);
    }
    for (String key : p.stringPropertyNames()) {
        LOG.info("\t" + key + " = " + p.getProperty(key));
    }
    return new MetaStore(new DruidPool(p));
}
Also used : MetaStoreException(org.smartdata.metastore.MetaStoreException) MetaStore(org.smartdata.metastore.MetaStore) InvalidPropertiesFormatException(java.util.InvalidPropertiesFormatException) IOException(java.io.IOException) Properties(java.util.Properties) File(java.io.File) URL(java.net.URL) FileInputStream(java.io.FileInputStream) DruidPool(org.smartdata.metastore.DruidPool) InvalidPropertiesFormatException(java.util.InvalidPropertiesFormatException) SQLException(java.sql.SQLException) IOException(java.io.IOException) MetaStoreException(org.smartdata.metastore.MetaStoreException)

Aggregations

File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 URL (java.net.URL)1 SQLException (java.sql.SQLException)1 InvalidPropertiesFormatException (java.util.InvalidPropertiesFormatException)1 Properties (java.util.Properties)1 DruidPool (org.smartdata.metastore.DruidPool)1 MetaStore (org.smartdata.metastore.MetaStore)1 MetaStoreException (org.smartdata.metastore.MetaStoreException)1