docker-hiveでHive JDBCを試す

Dockerを使ってHive JDBCを試したときのメモ。

準備

ソースコード

build.gradle

plugins {
    id 'java'
    id 'application'
}

group 'com.example'
version '1.0-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_11

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.apache.hive:hive-jdbc:2.3.6'
}

mainClassName = 'com.example.Main'

Main.java

package com.example;

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

public class Main {

    public static void main(String[] args) throws Exception {
        Connection con = DriverManager.getConnection("jdbc:hive2://localhost:10000/default");
        Statement stmt = con.createStatement();
        try (con; stmt) {
            stmt.execute("CREATE TABLE IF NOT EXISTS table1 (id int, name string)");

            stmt.executeUpdate("INSERT INTO table1 VALUES(1, 'user1')");
            stmt.executeUpdate("INSERT INTO table1 VALUES(2, 'user2')");
            stmt.executeUpdate("INSERT INTO table1 VALUES(3, 'user3')");

            ResultSet res = stmt.executeQuery("SELECT * FROM table1");
            while (res.next()) {
                System.out.println(res.getInt(1) + ", " + res.getString(2));
            }
        }
    }
}

docker-hive

今回はHive 2.3を試すため、2.3.2-postgresql-metastoreのブランチをダウンロードする。

% git clone https://github.com/big-data-europe/docker-hive -b 2.3.2-postgresql-metastore 

https://github.com/big-data-europe/docker-hive

確認

HiveServer2を起動後、デモアプリを起動する。

% cd docker-hive
% docker-compose up -d