Hive JDBC Client Java Example
Step 1 - Change the directory to /usr/local/hadoop/sbin
Step 2 - Start all hadoop daemons
Step 3 - Change the directory to /usr/local/hive/bin
Step 4 - Start hiveserver2 daemon
OR
Step 5 - Create a java project and add these jar files to your java project.
Step 1 - Change the directory to /usr/local/hadoop/sbin
cd /usr/local/hadoop/sbin
start-all.sh
$ cd $HIVE_HOME/bin
$ hiveserver2
$ hive --service hiveserver2 &
$HIVE_HOME/lib/*.jar $HADOOP_HOME/share/hadoop/mapreduce/*.jar $HADOOP_HOME/share/hadoop/common/*.jar
HiveCreateDB.java
import java.sql.SQLException; import java.sql.Connection; import java.sql.Statement; import java.sql.DriverManager; public class HiveCreateDB { //private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver"; private static String driverName = "org.apache.hive.jdbc.HiveDriver"; public static void main(String[] args) throws SQLException, ClassNotFoundException { // Register driver and create driver instance Class.forName(driverName); // get connection Connection con = DriverManager.getConnection( "jdbc:hive2://localhost:10000/default", "", ""); Statement stmt = con.createStatement(); stmt.execute("CREATE DATABASE newdatabase"); System.out.println("Database userdb created successwdbfully."); con.close(); } }
Comments
Post a Comment