Search in sources :

Example 1 with BeelineSiteParseException

use of org.apache.hive.beeline.hs2connection.BeelineSiteParseException in project hive by apache.

the class BeeLine method getDefaultConnectionUrl.

private String getDefaultConnectionUrl(CommandLine cl) throws BeelineConfFileParseException {
    Properties mergedConnectionProperties = new Properties();
    JdbcConnectionParams jdbcConnectionParams = null;
    BeelineSiteParser beelineSiteParser = getUserBeelineSiteParser();
    UserHS2ConnectionFileParser userHS2ConnFileParser = getUserHS2ConnFileParser();
    Properties userConnectionProperties = new Properties();
    if (!userHS2ConnFileParser.configExists() && !beelineSiteParser.configExists()) {
        // or beeline-site.xml in the path
        return null;
    }
    if (beelineSiteParser.configExists()) {
        String urlFromCommandLineOption = cl.getOptionValue("u");
        if (urlFromCommandLineOption != null) {
            throw new BeelineSiteParseException("Not using beeline-site.xml since the user provided the url: " + urlFromCommandLineOption);
        }
        // Get the named url from user specific config file if present
        Properties userNamedConnectionURLs = beelineSiteParser.getConnectionProperties();
        if (!userNamedConnectionURLs.isEmpty()) {
            String urlName = cl.getOptionValue("c");
            String jdbcURL = HS2ConnectionFileUtils.getNamedUrl(userNamedConnectionURLs, urlName);
            if (jdbcURL != null) {
                try {
                    jdbcConnectionParams = Utils.extractURLComponents(jdbcURL, new Properties());
                } catch (JdbcUriParseException e) {
                    throw new BeelineSiteParseException("Error in parsing jdbc url: " + jdbcURL + " from beeline-site.xml", e);
                }
            }
        }
    }
    if (userHS2ConnFileParser.configExists()) {
        // get the connection properties from user specific config file
        userConnectionProperties = userHS2ConnFileParser.getConnectionProperties();
    }
    if (jdbcConnectionParams != null) {
        String userName = cl.getOptionValue("n");
        if (userName != null) {
            jdbcConnectionParams.getSessionVars().put(JdbcConnectionParams.AUTH_USER, userName);
        }
        String password = cl.getOptionValue("p");
        if (password != null) {
            jdbcConnectionParams.getSessionVars().put(JdbcConnectionParams.AUTH_PASSWD, password);
        }
        String auth = cl.getOptionValue("a");
        if (auth != null) {
            jdbcConnectionParams.getSessionVars().put(JdbcConnectionParams.AUTH_TYPE, auth);
        }
        mergedConnectionProperties = HS2ConnectionFileUtils.mergeUserConnectionPropertiesAndBeelineSite(userConnectionProperties, jdbcConnectionParams);
    } else {
        mergedConnectionProperties = userConnectionProperties;
    }
    // load the HS2 connection url properties from hive-site.xml if it is present in the classpath
    HS2ConnectionFileParser hiveSiteParser = getHiveSiteHS2ConnectionFileParser();
    Properties hiveSiteConnectionProperties = hiveSiteParser.getConnectionProperties();
    // add/override properties found from hive-site with user-specific properties
    for (String key : mergedConnectionProperties.stringPropertyNames()) {
        if (hiveSiteConnectionProperties.containsKey(key)) {
            debug("Overriding connection url property " + key + " from user connection configuration file");
        }
        hiveSiteConnectionProperties.setProperty(key, mergedConnectionProperties.getProperty(key));
    }
    // return the url based on the aggregated connection properties
    return HS2ConnectionFileUtils.getUrl(hiveSiteConnectionProperties);
}
Also used : HiveSiteHS2ConnectionFileParser(org.apache.hive.beeline.hs2connection.HiveSiteHS2ConnectionFileParser) HS2ConnectionFileParser(org.apache.hive.beeline.hs2connection.HS2ConnectionFileParser) UserHS2ConnectionFileParser(org.apache.hive.beeline.hs2connection.UserHS2ConnectionFileParser) JdbcConnectionParams(org.apache.hive.jdbc.Utils.JdbcConnectionParams) JdbcUriParseException(org.apache.hive.jdbc.JdbcUriParseException) BeelineSiteParser(org.apache.hive.beeline.hs2connection.BeelineSiteParser) Properties(java.util.Properties) UserHS2ConnectionFileParser(org.apache.hive.beeline.hs2connection.UserHS2ConnectionFileParser) BeelineSiteParseException(org.apache.hive.beeline.hs2connection.BeelineSiteParseException)

Aggregations

Properties (java.util.Properties)1 BeelineSiteParseException (org.apache.hive.beeline.hs2connection.BeelineSiteParseException)1 BeelineSiteParser (org.apache.hive.beeline.hs2connection.BeelineSiteParser)1 HS2ConnectionFileParser (org.apache.hive.beeline.hs2connection.HS2ConnectionFileParser)1 HiveSiteHS2ConnectionFileParser (org.apache.hive.beeline.hs2connection.HiveSiteHS2ConnectionFileParser)1 UserHS2ConnectionFileParser (org.apache.hive.beeline.hs2connection.UserHS2ConnectionFileParser)1 JdbcUriParseException (org.apache.hive.jdbc.JdbcUriParseException)1 JdbcConnectionParams (org.apache.hive.jdbc.Utils.JdbcConnectionParams)1