Java - Read & Write tables from Hive with security
Github Project :Â example-java-read-and-write-from-hive-with-security
Â
Common part
Maven Dependencies
<dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-client</artifactId> <version>${hadoop.version}</version> </dependency> <dependency> <groupId>org.apache.hive</groupId> <artifactId>hive-jdbc</artifactId> <version>${hive-jdbc.version}</version> </dependency>
Â
Hive Connection Url
Connection URL are like that :Â jdbc:hive2://hiveserver:10000/;ssl=false
Default port is 10000.
Â
Init Connection
// Set JDBC Hive Driver Class.forName(JDBC_DRIVER_NAME); // Connect to Hive con = DriverManager.getConnection(connectionUrl,user,password); // Init Statement Statement stmt = con.createStatement();
Â
How to create a Hive table with Java?
String sqlStatementDrop = "DROP TABLE IF EXISTS helloworld"; String sqlStatementCreate = "CREATE TABLE helloworld (message String) STORED AS PARQUET"; Â // Execute DROP TABLE Query stmt.execute(sqlStatementDrop); // Execute CREATE Query stmt.execute(sqlStatementCreate);
Â
How to insert data into a Hive table with Java?
String sqlStatementInsert = "INSERT INTO helloworld VALUES (\"helloworld\")"; // Execute INSERT Query stmt.execute(sqlStatementInsert);
Â
How to select data from a Hive table with Java?
String sqlStatementSelect = "SELECT * from helloworld"; // Execute SELECT Query ResultSet rs = stmt.executeQuery(sqlStatementSelect); // Process results while(rs.next()) { logger.info(rs.getString(1)); }