【问题标题】:How to set configuration in Hive-Site.xml file for hive metastore connection?如何在 Hive-Site.xml 文件中为配置单元元存储连接设置配置?
【发布时间】:2023-03-14 19:24:01
【问题描述】:

我想使用 java 代码连接 MetaStore。我不知道如何在 Hive-Site.xml 文件中设置配置设置以及我将在哪里发布 Hive-Site.xml 文件。请帮忙。

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hive.conf.HiveConf;
import org.apache.hadoop.hive.conf.HiveConf.ConfVars;

public class HiveMetastoreJDBCTest {

    public static void main(String[] args) throws Exception {

        Connection conn = null;
        try {
            HiveConf conf = new HiveConf();
            conf.addResource(new Path("file:///path/to/hive-site.xml"));
            Class.forName(conf.getVar(ConfVars.METASTORE_CONNECTION_DRIVER));
            conn = DriverManager.getConnection(
                    conf.getVar(ConfVars.METASTORECONNECTURLKEY),
                    conf.getVar(ConfVars.METASTORE_CONNECTION_USER_NAME),
                    conf.getVar(ConfVars.METASTOREPWD));

            Statement st = conn.createStatement();
            ResultSet rs = st.executeQuery(
                "select t.tbl_name, s.location from tbls t " +
                "join sds s on t.sd_id = s.sd_id");
            while (rs.next()) {
                System.out.println(rs.getString(1) + " : " + rs.getString(2));
            }
        }        

    }
}

【问题讨论】:

    标签: hadoop hive cloudera impala metastore


    【解决方案1】:

    在您的 hive-site.xml 中添加这些行:

    <property>
      <name>hive.metastore.local</name>
      <value>true</value>
    </property>
    <property>
      <name>javax.jdo.option.ConnectionURL</name>
      <value>jdbc:mysql://localhost:3306/hive</value>
    </property>
    <property>
      <name>javax.jdo.option.ConnectionUserName</name>
      <value>hiveuser</value>
    </property>
    <property>
      <name>javax.jdo.option.ConnectionPassword</name>
      <value>hivepass</value>
    </property>
    

    jdbc:mysql://localhost:3306/hive中,3306是你默认的mysql端口; hive 是我们用于 hive 元存储的 mysql 数据库名称。 将hiveuser 更改为您的mysql hive 用户名,将hivepass 更改为您的mysql hive 密码。

    如果您尚未在 mysql 中为 hive Metastore 创建数据库,请在终端中执行此步骤:

    mysql -u root -p

    输入你的 mysql root 密码。

    mysql&gt; create database hive;

    mysql&gt; create user 'hiveuser'@'%' IDENTIFIED BY 'hivepass';

    mysql&gt; GRANT all on *.* to 'hiveuser'@localhost identified by 'hivepass';

    mysql&gt; flush privileges;

    这里,hiveuserhivepass 分别是您为 hive 元存储提供的用户名和密码。

    注意:您需要在 $HIVE_HOME/lib 中有 mysql-jdbc-connector.jar$HADOOP_HOME/lib

    【讨论】:

      【解决方案2】:

      关于 Hive-site.xml 这里是来自我的测试机器的样本。这是为了在 localhost 上安装 MySql 服务器来设置 hive Metastore。

      <configuration>
      <property>
       <name>javax.jdo.option.ConnectionURL</name>
       <value>jdbc:mysql://localhost/metastore?createDatabaseIfNotExist=true</value>
       <description>metadata is stored in a MySQL server</description>
      </property>
      <property>
       <name>javax.jdo.option.ConnectionDriverName</name>
       <value>com.mysql.jdbc.Driver</value>
       <description>MySQL JDBC driver class</description>
      </property>
      <property>
       <name>javax.jdo.option.ConnectionUserName</name>
       <value>hive</value>
       <description>user name for connecting to mysql server </description>
      </property>
      <property>
       <name>javax.jdo.option.ConnectionPassword</name>
       <value>123456</value>
       <description>password for connecting to mysql server </description>
      </property>
      </configuration>
      

      这个文件需要放在&lt;system_path&gt;/apache-hive-x.xx.x-bin/conf目录中

      我不太了解如何在 java 中使用这个文件。但是通过在java代码中指定连接字符串,你可以这样做

      import java.sql.Connection;
      import java.sql.DriverManager;
      import java.sql.ResultSet;
      import java.sql.SQLException;
      import java.sql.Statement;
      
      public class WriteToHive {
          private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";
          static Connection con;
          static Statement stmt;
      
          public WriteToHive() throws SQLException, ClassNotFoundException, Exception {
              try {
                  Class.forName(driverName);
              } catch (ClassNotFoundException e){
                  e.printStackTrace();
                  throw new ClassNotFoundException("No JDBC Hive Driver found");
                  //System.exit(1);
              } catch (Exception e) {
                  e.printStackTrace();
                  throw new Exception(e);
                  //System.exit(1);
              }
      
              con = DriverManager.getConnection("jdbc:hive://localhost:10000/rajen","","");
              stmt = con.createStatement();
          }
      
          public static void main(String[] args) throws SQLException {
              try {
                  Class.forName(driverName);
              } catch (ClassNotFoundException e){
                  e.printStackTrace();
                  System.exit(1);
              } catch (Exception e) {
                  e.printStackTrace();
                  System.exit(1);
              }
              con = DriverManager.getConnection("jdbc:hive://localhost:10000/rajen","","");
              stmt = con.createStatement();
              //Connection con = DriverManager.getConnection("jdbc:hive://","","");
              String tableName = "company_mas_hive_eclipse_trial";
      
              ResultSet res = stmt.executeQuery("use rajen");
      
              String sql = "DROP TABLE IF EXISTS " + tableName;
              System.out.println("Running: " + sql);
              res = stmt.executeQuery(sql);
      
              sql = "CREATE TABLE IF NOT EXISTS rajen.company_mas_hive_eclipse_trial (" +
                    "Name string," + 
                    "dateofincorporation string," + 
                    "country string)" +
                    "ROW FORMAT DELIMITED FIELDS TERMINATED BY \",\"";
              System.out.println("Running: " + sql);
              res = stmt.executeQuery(sql);
      
              sql = "show tables '" + tableName + "'";
              System.out.println("Running: " + sql);
              res = stmt.executeQuery(sql);
      
              if (res.next()){
                  System.out.println(res.getString(1));
              }
      
              sql = "describe " + tableName;
              System.out.println("Running: " + sql);
              res = stmt.executeQuery(sql);
              System.out.println("=========================================");
              while (res.next()) {
                System.out.println(res.getString(1) + "\t" + res.getString(2));
              }
              System.out.println("=========================================");
      
              // load data into table
              // NOTE: filepath has to be local to the hive server
              // NOTE: /tmp/a.txt is a ctrl-A separated file with two fields per line
              String filepath = "/home/seo/Refrence_Doc/sampledata/companymas"; //"/rajen/companymas";
              sql = "load data local inpath '" + filepath + "' into table " + tableName;
              System.out.println("Running: " + sql);
              res = stmt.executeQuery(sql);
      
              // load data into table
              // NOTE: filepath has to be local to the hive server
              // NOTE: /tmp/a.txt is a ctrl-A separated file with two fields per line
              filepath = "/rajen/companymas";
              sql = "load data inpath '" + filepath + "' into table " + tableName;
              System.out.println("Running: " + sql);
              //res = stmt.executeQuery(sql);
      
              // select * query
              sql = "select * from " + tableName;
              System.out.println("Running: " + sql);
              res = stmt.executeQuery(sql);
              while (res.next()) {
                  System.out.println(String.valueOf(res.getString(1)) + "\t" + res.getString(2));
              }
      
              // regular hive query
              sql = "select count(*) from " + tableName;
              System.out.println("Running: " + sql);
              res = stmt.executeQuery(sql);
              while (res.next()) {
                  System.out.println(res.getString(1));
              }
          }
      
          public void createTable(String def, String dbname) throws SQLException{
              @SuppressWarnings("unused")
              ResultSet res = stmt.executeQuery("use " + dbname);
              stmt.executeQuery(def);
          }
      
          public static void loadData(String filepath, String tableName) throws SQLException{
              stmt.executeQuery("load data local inpath '" + filepath + "' into table " + tableName);
          }
      }
      

      【讨论】:

      • 感谢您的宝贵回复。但我正在尝试使用 java 代码而不是 MySql 连接 Hive-Metastore。如果你有代码请给我。
      猜你喜欢
      • 1970-01-01
      • 2016-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-09
      • 1970-01-01
      • 1970-01-01
      • 2020-07-03
      相关资源
      最近更新 更多