복붙노트

[HADOOP] 하이브 메타 스토어 연결을위한 Hive-Site.xml 파일의 구성 설정 방법

HADOOP

하이브 메타 스토어 연결을위한 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));
            }
        }        

    }
}

해결법

  1. ==============================

    1.hive-site.xml에 다음 행을 추가하십시오.

    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 포트입니다. 하이브는 하이브 메타 스토어를위한 우리의 mysql 데이터베이스 이름이다. hiveuser를 mysql 하이브 사용자 이름으로 변경하고 mysql 하이브 패스워드로 하이브 패스하십시오.

    mysql에서 하이브 메타 스토어에 대한 데이터베이스를 생성하지 않았다면 터미널에서 다음 단계를 수행하십시오.

    mysql -u root -p

    mysql 루트 암호를 입력하십시오.

    mysql> 데이터베이스 하이브 생성;

    mysql> 사용자 생성 'hiveuser'@ '%'IDENTIFIED BY 'hivepass';

    mysql> 'hiveuser'에 *. * 부여 '@ hivepass'에 의해 식별 된 @ localhost;

    mysql> 권한을 플러시;

    여기서 hiveuser와 hivepass는 하이브 메타 스토어에 부여한 사용자 이름과 암호입니다.

  2. ==============================

    2.여기 Hive-site.xml은 테스트 도구의 샘플입니다. 이것은 로컬 호스트에 설치된 MySql 서버로 하이브 메타 스토어를 설정하기위한 것입니다.

    여기 Hive-site.xml은 테스트 도구의 샘플입니다. 이것은 로컬 호스트에 설치된 MySql 서버로 하이브 메타 스토어를 설정하기위한 것입니다.

    <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>
    

    이 파일은 /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);
        }
    }
    
  3. from https://stackoverflow.com/questions/29485118/how-to-set-configuration-in-hive-site-xml-file-for-hive-metastore-connection by cc-by-sa and MIT license