• 【Hive】JDBC操作


     1 package com.java.hadoop.hive;
     2  
     3 import java.sql.Connection;
     4 import java.sql.DriverManager;
     5 import java.sql.PreparedStatement;
     6 import java.sql.ResultSet;
     7 import java.sql.SQLException;
     8 import java.sql.Statement;
     9  
    10 import org.apache.hadoop.hive.metastore.api.ThriftHiveMetastore.Processor.drop_database;
    11 import org.junit.Before;
    12 import org.junit.Test;
    13  
    14 public class TestHive {
    15     private Connection connection;
    16     private PreparedStatement ps;
    17     private ResultSet rs;
    18     //创建连接
    19     @Before
    20     public void getConnection() {
    21         try {
    22  
    23             Class.forName("org.apache.hive.jdbc.HiveDriver");
    24             connection = DriverManager.getConnection("jdbc:hive2://192.168.18.130:10000/", "root", "root");
    25             System.out.println(connection);
    26         } catch (ClassNotFoundException e) {
    27             e.printStackTrace();
    28         } catch (SQLException e) {
    29             e.printStackTrace();
    30         }
    31     }
    32     //关闭连接
    33     public void close() {
    34         try {
    35             if (rs != null) {
    36                 rs.close();
    37             }
    38             if (ps != null) {
    39                 ps.close();
    40             }
    41             if (connection != null) {
    42                 connection.close();
    43             }
    44         } catch (SQLException e) {
    45             e.printStackTrace();
    46         }
    47     }
    48  
    49     // 创建表
    50     @Test
    51     public void createTable() {
    52         String sql = "create table goods2(id int,name string) row format delimited fields terminated by '	' ";
    53         try {
    54             ps = connection.prepareStatement(sql);
    55             ps.execute(sql);
    56             close();
    57         } catch (SQLException e) {
    58             e.printStackTrace();
    59         }
    60     }
    61     // 删除表
    62     @Test
    63     public void dropTable() {
    64         String sql = "drop table goods";
    65         try {
    66             ps = connection.prepareStatement(sql);
    67             ps.execute();
    68             close();
    69         } catch (SQLException e) {
    70             e.printStackTrace();
    71         }
    72     }
    73     //添加数据
    74     @Test
    75     public void insert() throws SQLException{
    76         String sql = "load data inpath '/goods.txt' into table goods";
    77         //记得先在文件系统中上传goods.txt
    78         ps = connection.prepareStatement(sql);
    79         ps.execute();
    80         close();
    81     }
    82     //查询
    83     @Test
    84     public void find() throws SQLException {
    85         String sql = "select * from goods ";
    86         ps = connection.prepareStatement(sql);
    87         rs = ps.executeQuery();
    88         while (rs.next()) {
    89             System.out.println(rs.getObject(1) + "---" + rs.getObject(2));
    90         }
    91         close();
    92     }
    93  
    94      
    95 }
     1 String sql="show tables; select * from test_tb limit 10";
     2 List<String> command = new ArrayList<String>();
     3  
     4 command.add("hive");
     5 command.add("-e");
     6 command.add(sql);
     7  
     8 List<String> results = new ArrayList<String>();
     9 ProcessBuilder hiveProcessBuilder = new ProcessBuilder(command);
    10 hiveProcess = hiveProcessBuilder.start();
    11  
    12 BufferedReader br = new BufferedReader(new InputStreamReader(
    13         hiveProcess.getInputStream()));
    14 String data = null;
    15 while ((data = br.readLine()) != null) {
    16     results.add(data);
    17 }
  • 相关阅读:
    代码扫描工具
    事务注解加在Controller 和 Service上有哪些区别
    什么是嵌套事务
    配置工程日志
    利用mkvirtualenv工具搭建python2和python3环境
    python接口自动化1-发送get请求
    面试题:使用存储过程造10w条测试数据,数据不重复
    python测试开发
    接口自动化测试持续集成+postman+newman+git+jenkins+钉钉
    app专项测试
  • 原文地址:https://www.cnblogs.com/flydkPocketMagic/p/6298403.html
Copyright © 2020-2023  润新知