java代码操作hive是怎样的

2023-04-08 20:34:00 操作 代码 是怎样

Java代码操作hive是怎样的?

Hive是一个开源的数据仓库系统,可以使用SQL语言查询和分析存储在Hadoop上的大量数据。 它提供了一个类似于关系数据库的查询语言HiveQL,可以轻松地将结构化数据转换为Hive表,并使用HiveQL查询数据。

Java代码操作hive需要通过JDBC驱动来连接hive服务器,然后通过SQL语句来操作hive中的数据。 下面是一个简单的Java代码示例,可以帮助你了解如何操作hive:

import java.sql.*; public class HiveJdbcClient { private static String driverName = "org.apache.hive.jdbc.HiveDriver"; public static void main(String[] args) throws SQLException { // Register driver and create driver instance Class.forName(driverName); // get connection to hive server Connection con = DriverManager.getConnection("jdbc:hive2://localhost:10000/default", "hive", ""); // create statement instance Statement stmt = con.createStatement(); // execute hiveQL query String sql = "select * from table_name"; System.out.println("Running: " + sql); ResultSet res = stmt.executeQuery(sql); // print results while (res.next()) { System.out.println(res.getString(1) + "\t" + res.getString(2)); } // close connection con.close(); } }

相关文章