Hive JDBC创建删除数据库及增删改查表

JDBC创建数据库

JDBC 程序
在JDBC程序来创建数据库如下。

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

public class HiveCreateDb {
   private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";
   
   public static void main(String[] args) throws SQLException {
      // Register driver and create driver instance
   
      Class.forName(driverName);
      // get connection
      
      Connection con = DriverManager.getConnection("jdbc:hive://localhost:10000/default", "", "");
      Statement stmt = con.createStatement();
      
      stmt.executeQuery("CREATE DATABASE userdb");
      System.out.println(Database userdb created successfully.);
      
      con.close();
   }
}

保存程序在一个名为HiveCreateDb.java文件。下面的命令用于编译和执行这个程序。

$ javac HiveCreateDb.java
$ java HiveCreateDb

输出:

 Database userdb created successfully.

JDBC删除数据库

JDBC Program
在JDBC程序来删除数据库如下。

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

public class HiveDropDb {
   private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";
   
   public static void main(String[] args) throws SQLException {
   
      // Register driver and create driver instance
      Class.forName(driverName);
      
      // get connection
      Connection con = DriverManager.getConnection("jdbc:hive://localhost:10000/default", "", "");
      Statement stmt = con.createStatement();
      stmt.executeQuery("DROP DATABASE userdb");
      
      System.out.println(Drop userdb database successful.);
      
      con.close();
   }
}

将该程序保存在一个名为HiveDropDb.java文件。下面给出的是编译和执行这个程序的命令。

$ javac HiveDropDb.java
$ java HiveDropDb

Output:

Drop userdb database successful.

JDBC 创建表程序

以下是使用JDBC程序来创建表给出的一个例子。

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

public class HiveCreateTable {
   private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";
   
   public static void main(String[] args) throws SQLException {
   
      // Register driver and create driver instance
      Class.forName(driverName);
      
      // get connection
      Connection con = DriverManager.getConnection("jdbc:hive://localhost:10000/userdb", "", "");
      
      // create statement
      Statement stmt = con.createStatement();
      
      // execute statement
      stmt.executeQuery("CREATE TABLE IF NOT EXISTS "
         +" employee ( eid int, name String, "
         +" salary String, destignation String)"
         +" COMMENT ‘Employee details’"
         +" ROW FORMAT DELIMITED"
         +" FIELDS TERMINATED BY ‘\t’"
         +" LINES TERMINATED BY ‘\n’"
         +" STORED AS TEXTFILE;");
         
      System.out.println(Table employee created.);
      con.close();
   }
}

将该程序保存在一个名为HiveCreateDb.java文件。下面的命令用于编译和执行这个程序。

 $ javac HiveCreateDb.java 
 $ java HiveCreateDb

输出

Table employee created.

JDBC 数据插入表程序

下面给出的是JDBC程序将给定的数据加载到表中。

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

public class HiveLoadData {

   private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";
   
   public static void main(String[] args) throws SQLException {
   
      // Register driver and create driver instance
      Class.forName(driverName);
      
      // get connection
      Connection con = DriverManager.getConnection("jdbc:hive://localhost:10000/userdb", "", "");
      
      // create statement
      Statement stmt = con.createStatement();
      
      // execute statement
      stmt.executeQuery("LOAD DATA LOCAL INPATH '/home/user/sample.txt'" + "OVERWRITE INTO TABLE employee;");
      System.out.println("Load Data into employee successful");
      
      con.close();
   }
}

将该程序保存在一个名为HiveLoadData.java文件。使用下面的命令来编译和执行这个程序。

$ javac HiveLoadData.java
$ java HiveLoadData

输出:

Load Data into employee successful

JDBC修改表 程序

在JDBC程序重命名表如下。

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

public class HiveAlterRenameTo {
   private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";
   
   public static void main(String[] args) throws SQLException {
   
      // Register driver and create driver instance
      Class.forName(driverName);
      
      // get connection
      Connection con = DriverManager.getConnection("jdbc:hive://localhost:10000/userdb", "", "");
      
      // create statement
      Statement stmt = con.createStatement();
      
      // execute statement
      stmt.executeQuery("ALTER TABLE employee RENAME TO emp;");
      System.out.println("Table Renamed Successfully");
      con.close();
   }
}

将该程序保存在一个名为HiveAlterRenameTo.java文件。使用下面的命令来编译和执行这个程序。

$ javac HiveAlterRenameTo.java
$ java HiveAlterRenameTo

输出

Table renamed successfully.

Change 语句

下表包含employee表的字段,它显示的字段要被更改(粗体)。
字段名 从数据类型转换 更改字段名称 转换为数据类型
int

eid int eid int
name String ename String
salary Float salary Double
designation String designation String

下面查询重命名使用上述数据的列名和列数据类型:

hive> ALTER TABLE employee CHANGE name ename String;
hive> ALTER TABLE employee CHANGE salary salary Double;

JDBC 程序
下面给出的是使用JDBC程序来更改列。

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

public class HiveAlterChangeColumn {
   private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";
   
   public static void main(String[] args) throws SQLException {
   
      // Register driver and create driver instance
      Class.forName(driverName);
      
      // get connection
      Connection con = DriverManager.getConnection("jdbc:hive://localhost:10000/userdb", "", "");
      
      // create statement
      Statement stmt = con.createStatement();
      
      // execute statement
      stmt.executeQuery("ALTER TABLE employee CHANGE name ename String;");
      stmt.executeQuery("ALTER TABLE employee CHANGE salary salary Double;");
      
      System.out.println("Change column successful.");
      con.close();
   }
}

将该程序保存在一个名为HiveAlterChangeColumn.java文件。使用下面的命令来编译和执行这个程序。

$ javac HiveAlterChangeColumn.java
$ java HiveAlterChangeColumn

输出

Change column successful.

添加列语句
下面的查询增加了一个列名dept在employee表。

hive> ALTER TABLE employee ADD COLUMNS ( 
   > dept STRING COMMENT 'Department name');

JDBC 程序
JDBC程序添加列到表如下。

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

public class HiveAlterAddColumn {
   private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";
   
   public static void main(String[] args) throws SQLException {
   
      // Register driver and create driver instance
      Class.forName(driverName);

      // get connection
      Connection con = DriverManager.getConnection("jdbc:hive://localhost:10000/userdb", "", "");

      // create statement
      Statement stmt = con.createStatement();
      
      // execute statement
      stmt.executeQuery("ALTER TABLE employee ADD COLUMNS " + " (dept STRING COMMENT 'Department name');");
      System.out.prinln("Add column successful.");
      
      con.close();
   }
}

将该程序保存在一个名为HiveAlterAddColumn.java文件。使用下面的命令来编译和执行这个程序。

$ javac HiveAlterAddColumn.java
$ java HiveAlterAddColumn

输出

Add column successful.

REPLACE语句
以下从employee表中查询删除的所有列,并使用emp替换列:

hive> ALTER TABLE employee REPLACE COLUMNS ( 
   > eid INT empid Int, 
   > ename STRING name String);

JDBC 程序
下面给出的是JDBC程序使用empid代替eid列,name代替ename列。

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

public class HiveAlterReplaceColumn {

   private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";
   
   public static void main(String[] args) throws SQLException {
   
      // Register driver and create driver instance
      Class.forName(driverName);
      
      // get connection
      Connection con = DriverManager.getConnection("jdbc:hive://localhost:10000/userdb", "", "");
      
      // create statement
      Statement stmt = con.createStatement();
      
      // execute statement
      stmt.executeQuery("ALTER TABLE employee REPLACE COLUMNS "
         +" (eid INT empid Int,"
         +" ename STRING name String);");
         
      System.out.println(" Replace column successful");
      con.close();
   }
}

将该程序保存在一个名为HiveAlterReplaceColumn.java文件。使用下面的命令来编译和执行这个程序。

$ javac HiveAlterReplaceColumn.java
$ java HiveAlterReplaceColumn

输出:

Replace column successful.

JDBC 删除表程序

下面JDBC程序删除employee表。

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

public class HiveDropTable {

   private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";
   
   public static void main(String[] args) throws SQLException {
   
      // Register driver and create driver instance
      Class.forName(driverName);

      // get connection
      Connection con = DriverManager.getConnection("jdbc:hive://localhost:10000/userdb", "", "");

      // create statement
      Statement stmt = con.createStatement();

      // execute statement
      stmt.executeQuery("DROP TABLE IF EXISTS employee;");
      System.out.println("Drop table successful.");
      
      con.close();
   }
}

将该程序保存在一个名为HiveDropTable.java文件。使用下面的命令来编译和执行这个程序。

$ javac HiveDropTable.java
$ java HiveDropTable

输出

Drop table successful

以下查询被用来验证表的列表:

hive> SHOW TABLES;
emp
ok
Time taken: 2.1 seconds
hive>

你可能感兴趣的:(数据仓库,Hive基础,数据库,hive,java)