1. 程式人生 > 其它 >java jdbc查詢hive資料,並將結果存為列表顯示

java jdbc查詢hive資料,並將結果存為列表顯示

技術標籤:java大資料javahive

java jdbc查詢hive資料,並將結果存為列表顯示
1.程式碼

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HiveConnect {
    public static void main(String [] args) throws Exception{
        Class.forName("org.apache.hive.jdbc.HiveDriver"
); //root為使用者名稱,後面為密碼,""表示無密碼 Connection connection = DriverManager.getConnection("jdbc:hive2://39.106.229.57:10000/test", "root", ""); Statement statement = connection.createStatement(); String querySQL = "select * from student limit 2"
; ResultSet rs = statement.executeQuery(querySQL); List<Map<String, Object>> list = new ArrayList<>(); try { ResultSetMetaData md = rs.getMetaData(); int columnCount = md.getColumnCount(); while (rs.next()) { Map<
String, Object>
rowData = new HashMap<>(); for (int i = 1; i <= columnCount; i++) { rowData.put(md.getColumnName(i), rs.getObject(i)); } list.add(rowData); } System.out.println(list); }catch (SQLException e) { e.printStackTrace(); } finally { try { if (rs != null) rs.close(); } catch (SQLException e) { e.printStackTrace(); } } } }

2.執行結果
在這裡插入圖片描述