공용 Hive
    • PDF

    공용 Hive

    • PDF

    Article Summary

    VPC 환경에서 이용 가능합니다.

    Data Forest에서 Hive 데이터베이스와 테이블을 생성하고 사용하는 방법을 설명합니다.

    참고

    UDF, Storage Handler 기능은 준비 중입니다.

    Hive 명령어 실행

    HiveServer에 처음 연결하는 경우, Hive 사용을 참조해 주십시오.
    Kerberos 인증은 계정 생성 및 관리을 참조해 주십시오.

    Dev 앱에서 다음과 같이 Hive 명령어를 실행할 수 있습니다.

    $ kinit example -kt df.example.keytab # Kerberos 인증
    $ beeline -u "jdbc:hive2://zk1.kr.df.naverncp.com:2181,zk2.kr.df.naverncp.com:2181,zk3.kr.df.naverncp.com:2181/;serviceDiscoveryMode=zooKeeper;zooKeeperNamespace=hiveserver2" -n example -p 'PASSWORD'
    

    데이터베이스 생성

    참고

    Data Forest 사용자는 Hive의 기본값 데이터베이스를 사용할 수 없습니다. 규칙에 맞게 사용자의 데이터베이스를 새로 생성해야 합니다.

    Data Forest에서 Hive 데이터베이스를 생성하기 위해서는 아래 규칙을 따라야 합니다.

    • 데이터베이스 이름 형식은 {USER}__db_*입니다.
      사용자 계정명이 example이라면 아래와 같이 DB명을 지정하는 것이 가능합니다.

      example__db_foo
      example__db_bar
      example__db_hello_foo
      example__db_hello_foo_bar
      
    • 데이터베이스 이름에는 -(하이픈)이 들어갈 수 없습니다. 사용자 이름에 -(하이픈) 이 들어있다면 _(언더바)로 치환합니다. 사용자 계정명이 example-ex이라면 아래와 같이 DB명을 지정할 수 있습니다.

      example_ex__db_foo
      

      해당 규칙을 위반하는 DB명의 예시는 다음과 같습니다.

      example_db_foo  # _(언더바) 개수가 틀린 경우
      example__DB_foo # db 를 대문자로 표기하는 경우
      example-ex__db_foo # -(하이픈)을 _(언더바)로 고치지 않은 경우
      
    • Hive 테이블 데이터는 HDFS에 저장됩니다. 데이터베이스를 생성할 때는 반드시 LOCATION 설정으로 경로를 지정해야 합니다. 이 경로는 사용자의 홈 디렉터리 밑에 warehouse 하위 경로로 제한됩니다.

      • 먼저 사용자의 홈 디렉터리 밑에 warehouse 디렉터리를 생성합니다.
        $ hadoop fs -mkdir warehouse
        $ hadoop fs -ls
        Found 4 items
        ...
        drwx------   - example hdfs          0 2021-03-29 16:18 warehouse
        
      • Beeline 명령어로 다음과 같이 데이터베이스를 생성할 수 있습니다.
        # 데이터베이스 생성
        CREATE DATABASE example__db_foo LOCATION   '/user/example/warehouse/example__db_foo.db';
        # 데이터베이스 사용
        USE example__db_foo;
        
    주의

    Beeline 명령어로 데이터베이스 조회 시 다른 사용자의 데이터베이스는 확인할 수 없습니다. 하지만 Spark SQL로는 모든 데이터베이스 목록이 반환되므로, 데이터베이스 이름에 개인 정보를 포함하지 않도록 주의해 주십시오.

    테이블 생성

    테이블 생성 시 네이밍 제약은 없습니다. LOCATION 옵션은 필수는 아니지만, 별도로 지정하고 싶은 경우 /user/example/warehouse 아래 경로로 지정해 주십시오.

    CREATE TABLE `table1` (`id` BIGINT) LOCATION '/user/example/warehouse/table1';
    

    명령어 제한

    SET, TRANSFORM 절은 사용할 수 없습니다. 전용 큐를 도입하여 SET 명령어를 사용할 수 있도록 업데이트할 예정입니다.

    Spark-Hive 접속 설정

    Spark에서 Hive의 Managed Table 데이터를 읽으려면 LLAP를 사용해야 합니다. Interactive Hive를 통해 Hive에 접근하도록 설정하는 방법을 설명합니다.

    접속 정보 확인

    설정할 수 있는 접속 정보는 다음과 같습니다.

    PropertyValue
    spark.sql.hive.hiveserver2.jdbc.url (Interactive)jdbc:hive2://zk1.kr.df.naverncp.com:2181,zk2.kr.df.naverncp.com:2181,zk3.kr.df.naverncp.com:2181/;serviceDiscoveryMode=zooKeeperHA;zooKeeperNamespace=df-interactive
    spark.sql.hive.hiveserver2.jdbc.url (Batch)jdbc:hive2://zk1.kr.df.naverncp.com:2181,zk2.kr.df.naverncp.com:2181,zk3.kr.df.naverncp.com:2181/;serviceDiscoveryMode=zooKeeper;zooKeeperNamespace=hiveserver2
    spark.datasource.hive.warehouse.metastoreUrithrift://hms1.kr.df.naverncp.com:9083,thrift://hms2.kr.df.naverncp.com:9083
    spark.datasource.hive.warehouse.load.staging.dir/user/{dataforest 계정명}/tmp
    spark.hadoop.hive.zookeeper.quorumzk1.kr.df.naverncp.com:2181,zk2.kr.df.naverncp.com:2181,zk3.kr.df.naverncp.com:2181
    spark.hadoop.hive.llap.daemon.service.hosts@df-llap
    principal{dataforest 계정명}@KR.DF.NAVERNCP.COM
    keytab{dataforest 계정 keytab 파일 경로}
    jars{hwc jar 파일 경로}
    • 표 내용을 통해 spark.sql.hive.hiveserver2.jdbc.url에 interactive, batch의 두 종류의 Connection String이 존재하는 것을 확인할 수 있습니다.
    • Interactive Hive에서 작업을 수행하려면 interactive Connection String을 사용해야 합니다. Spark 셸과 PySpark에서도 동일한 설정값을 사용하여 작업을 수행할 수 있습니다.
    • principal과 키탭은 Spark Executor를 YARN에서 실행하기 위해 필요한 설정으로 Data Forest의 Kerberos 계정을 사용합니다.
    • spark.sql.hive.hiveserver2.jdbc.url의 user, password는 Executor내에서 Interactive Hive에 접근하기 위한 인증 정보입니다. Data Forest의 계정명(Kerberos ID와 동일)과 Ranger 패스워드를 사용합니다.

    Interactive Hive에서 작업을 수행하는 예제

    spark-shell을 통해 Interactive Hive에서 작업을 수행하는 예제는 다음과 같습니다.

    spark-shell \
      --master local[2] \
      --conf spark.sql.hive.hiveserver2.jdbc.url="jdbc:hive2://zk1.kr.df.naverncp.com:2181,zk2.kr.df.naverncp.com:2181,zk3.kr.df.naverncp.com:2181/;serviceDiscoveryMode=zooKeeperHA;zooKeeperNamespace=df-interactive" \
      --conf spark.datasource.hive.warehouse.metastoreUri="thrift://hms1.kr.df.naverncp.com:9083,thrift://hms2.kr.df.naverncp.com:9083" \
      --conf spark.datasource.hive.warehouse.load.staging.dir=/user/example/tmp \
      --conf spark.hadoop.hive.zookeeper.quorum="zk1.kr.df.naverncp.com:2181,zk2.kr.df.naverncp.com:2181,zk3.kr.df.naverncp.com:2181" \
      --conf spark.hadoop.hive.llap.daemon.service.hosts=@df-llap \
      --conf spark.security.credentials.hiveserver2.enabled=false \
      --conf spark.sql.hive.hiveserver2.jdbc.url.principal="hive/_HOST@KR.DF.NAVERNCP.COM" \
      --principal example@KR.DF.NAVERNCP.COM \
      --keytab ./df.example.keytab \
      --jars /usr/hdp/current/hive_warehouse_connector/hive-warehouse-connector-assembly-1.0.0.3.1.0.0-78.jar
    

    접속이 완료되면 다음과 같이 화면이 나타납니다.

    Setting default log level to "WARN".
    To adjust logging level use sc.setLogLevel(newLevel). For SparkR, use setLogLevel(newLevel).
    Spark context Web UI available at http://ambari1.kr.df.naverncp.com:4040
    Spark context available as 'sc' (master = local[2], app id = local-1624438130183).
    Spark session available as 'spark'.
    Welcome to
          ____              __
         / __/__  ___ _____/ /__
        _\ \/ _ \/ _ `/ __/  '_/
       /___/ .__/\_,_/_/ /_/\_\   version 2.3.2.3.1.0.0-78
          /_/
    
    Using Scala version 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_112)
    Type in expressions to have them evaluated.
    Type :help for more information.
    
    scala> import com.hortonworks.hwc.HiveWarehouseSession
    import com.hortonworks.hwc.HiveWarehouseSession
    
    scala> import com.hortonworks.hwc.HiveWarehouseSession._
    import com.hortonworks.hwc.HiveWarehouseSession._
    
    scala>  val hive1 = HiveWarehouseSession.session(spark).build()
    hive1: com.hortonworks.spark.sql.hive.llap.HiveWarehouseSessionImpl = com.hortonworks.spark.sql.hive.llap.HiveWarehouseSessionImpl@3da61af2
    

    이 문서가 도움이 되었습니까?

    What's Next
    Changing your password will log you out immediately. Use the new password to log back in.
    First name must have atleast 2 characters. Numbers and special characters are not allowed.
    Last name must have atleast 1 characters. Numbers and special characters are not allowed.
    Enter a valid email
    Enter a valid password
    Your profile has been successfully updated.