Search in sources :

Example 1 with BeelineSiteParser

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

the class BeeLine method printBeelineSiteUrls.

private void printBeelineSiteUrls() {
    BeelineSiteParser beelineSiteParser = getUserBeelineSiteParser();
    if (!beelineSiteParser.configExists()) {
        output("No beeline-site.xml in the path", true);
    }
    if (beelineSiteParser.configExists()) {
        // Get the named url from user specific config file if present
        try {
            Properties userNamedConnectionURLs = beelineSiteParser.getConnectionProperties();
            userNamedConnectionURLs.remove(BeelineSiteParser.DEFAULT_NAMED_JDBC_URL_PROPERTY_KEY);
            StringBuilder sb = new StringBuilder("urls: ");
            for (Entry<Object, Object> entry : userNamedConnectionURLs.entrySet()) {
                String urlFromBeelineSite = (String) entry.getValue();
                if (isZkBasedUrl(urlFromBeelineSite)) {
                    List<String> jdbcUrls = HiveConnection.getAllUrlStrings(urlFromBeelineSite);
                    for (String jdbcUrl : jdbcUrls) {
                        sb.append(jdbcUrl + ", ");
                    }
                } else {
                    sb.append(urlFromBeelineSite + ", ");
                }
            }
            output(sb.toString(), true);
        } catch (Exception e) {
            output(e.getMessage(), true);
            return;
        }
    }
}
Also used : BeelineSiteParser(org.apache.hive.beeline.hs2connection.BeelineSiteParser) Properties(java.util.Properties) TTransportException(org.apache.thrift.transport.TTransportException) BeelineConfFileParseException(org.apache.hive.beeline.hs2connection.BeelineConfFileParseException) BeelineSiteParseException(org.apache.hive.beeline.hs2connection.BeelineSiteParseException) EOFException(java.io.EOFException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ParseException(org.apache.commons.cli.ParseException) JdbcUriParseException(org.apache.hive.jdbc.JdbcUriParseException) SQLException(java.sql.SQLException) IOException(java.io.IOException)

Example 2 with BeelineSiteParser

use of org.apache.hive.beeline.hs2connection.BeelineSiteParser 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)2 BeelineSiteParseException (org.apache.hive.beeline.hs2connection.BeelineSiteParseException)2 BeelineSiteParser (org.apache.hive.beeline.hs2connection.BeelineSiteParser)2 JdbcUriParseException (org.apache.hive.jdbc.JdbcUriParseException)2 EOFException (java.io.EOFException)1 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 SQLException (java.sql.SQLException)1 ParseException (org.apache.commons.cli.ParseException)1 BeelineConfFileParseException (org.apache.hive.beeline.hs2connection.BeelineConfFileParseException)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 JdbcConnectionParams (org.apache.hive.jdbc.Utils.JdbcConnectionParams)1 TTransportException (org.apache.thrift.transport.TTransportException)1