Hive 是一個基于 Hadoop 的數(shù)據(jù)倉庫架構(gòu),它通過 Hadoop 的 MapReduce 進行數(shù)據(jù)讀取和計算,大大提高了數(shù)據(jù)的處理效率。
MySQL 是一種關(guān)系型數(shù)據(jù)庫,常用于 Web 應用程序的數(shù)據(jù)管理和處理。
Hive 可以通過 JDBC 驅(qū)動程序和 MySQL 進行交互,將 Hive 中存儲的數(shù)據(jù)導入到 MySQL 中,也可以將 MySQL 的數(shù)據(jù)導入到 Hive 中。
#將 Hive 中的數(shù)據(jù)導入到 MySQL #在 Hive 中創(chuàng)建一個表,并將數(shù)據(jù)導入到該表中 CREATE TABLE student ( id INT, name STRING, age INT ); LOAD DATA LOCAL INPATH '/user/hive/warehouse/student' INTO TABLE student; #通過 JDBC 驅(qū)動程序連接 MySQL 并導入數(shù)據(jù) import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; public class HiveToMySQL { public static void main(String[] args) { String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/test"; String user = "root"; String password = "123456"; Connection conn; try { Class.forName(driver); conn = DriverManager.getConnection(url, user, password); Statement statement = conn.createStatement(); String sql = "INSERT INTO student (id, name, age) SELECT id, name, age FROM hive.student"; statement.executeUpdate(sql); conn.close(); } catch (Exception e) { e.printStackTrace(); } } } #將 MySQL 中的數(shù)據(jù)導入到 Hive #在 Hive 中創(chuàng)建一個表 CREATE TABLE student ( id INT, name STRING, age INT ); #通過 JDBC 驅(qū)動程序連接 MySQL 并將數(shù)據(jù)導入到 Hive import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class MySQLToHive { public static void main(String[] args) { String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/test"; String user = "root"; String password = "123456"; Connection conn; try { Class.forName(driver); conn = DriverManager.getConnection(url, user, password); Statement statement = conn.createStatement(); String sql = "SELECT * FROM student"; ResultSet resultSet = statement.executeQuery(sql); while (resultSet.next()) { int id = resultSet.getInt("id"); String name = resultSet.getString("name"); int age = resultSet.getInt("age"); String hiveSql = "INSERT INTO TABLE student VALUES (" + id + ", '" + name + "', " + age + ")"; statement.executeUpdate(hiveSql); } conn.close(); } catch (Exception e) { e.printStackTrace(); } } }