• hive的访问:通过JDBC远程连接hive数据仓库


    hive的访问:终端访问  远程访问

    终端访问:安装hive,利用shell脚本访问 不可并发访问

    远程访问:通过JDBC连接数据仓库 支持并发访问

    • 启动hiveserver2服务:查看该命令可知hiveserver2,等价于hive --service hiveserver2 &
    [xiaoqiu@s150 /soft/hive/bin]$ cat hiveserver2
    #!/usr/bin/env bash
    
    # Licensed to the Apache Software Foundation (ASF) under one or more
    # contributor license agreements.  See the NOTICE file distributed with
    # this work for additional information regarding copyright ownership.
    # The ASF licenses this file to You under the Apache License, Version 2.0
    # (the "License"); you may not use this file except in compliance with
    # the License.  You may obtain a copy of the License at
    #
    #     http://www.apache.org/licenses/LICENSE-2.0
    #
    # Unless required by applicable law or agreed to in writing, software
    # distributed under the License is distributed on an "AS IS" BASIS,
    # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    # See the License for the specific language governing permissions and
    # limitations under the License.
    
    bin=`dirname "$0"`
    bin=`cd "$bin"; pwd`
    
    . "$bin"/hive --service hiveserver2 "$@"
    
    [xiaoqiu@s150 /soft/hive/bin]$ hive --service hiveserver2 &
    [1] 11638
    [xiaoqiu@s150 /soft/hive/bin]$ jps
    11652 VersionInfo
    2325 DataNode
    10710 NameNode
    2665 ResourceManager
    11387 RunJar
    2525 JournalNode
    2797 NodeManager
    11677 Jps
    
    [xiaoqiu@s150 /soft/hive/bin]$ netstat -anop |grep 10000
    (Not all processes could be identified, non-owned process info
     will not be shown, you would have to be root to see it all.)
    tcp        0      0 0.0.0.0:10000           0.0.0.0:*               LISTEN      11638/java           off (0.00/0/0)
    [xiaoqiu@s150 /soft/hive/bin]$
    

    启动hiveserver2服务,查看jps,发现出现了runjar进程  查看端口10000是否启动 

    方法一:进入到beeline命令行 通过JDBC协议 连接到hive数据仓库

    beeline> !connect jdbc:hive2://localhost:10000/incubator
    Connecting to jdbc:hive2://localhost:10000/incubator
    Enter username for jdbc:hive2://localhost:10000/incubator:
    Enter password for jdbc:hive2://localhost:10000/incubator:
    Connected to: Apache Hive (version 2.3.3)
    Driver: Hive JDBC (version 2.3.3)
    Transaction isolation: TRANSACTION_REPEATABLE_READ
    0: jdbc:hive2://localhost:10000/incubator> show tables;
    +-----------+
    | tab_name  |
    +-----------+
    | t         |
    +-----------+
    1 row selected (6.595 seconds)
    0: jdbc:hive2://localhost:10000/incubator> select * from t;
    +-------+---------+--------+
    | t.id  | t.name  | t.age  |
    +-------+---------+--------+
    +-------+---------+--------+
    No rows selected (6.435 seconds)
    0: jdbc:hive2://localhost:10000/incubator>
    

    方法二:通过api连接hive数据仓库(前提是服务端已经启动hiveserver2服务)

    package com.hive;
    
    import java.sql.*;
    
    public class AcessApp {
        public static void main(String[] args) throws ClassNotFoundException, SQLException {
            Class.forName("org.apache.hive.jdbc.HiveDriver");
            Connection con = DriverManager.getConnection("jdbc:hive2://192.168.109.150:10000/cr","xiaoqiu","0806");
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery("SELECT * from student");
            while (rs.next()){
                System.out.println(rs.getInt(1) + "," + rs.getString(2));
            }
            rs.close();
            st.close();
            con.close();
        }
    }
    
    3,yamy
    4,sunny
    欢迎关注我的公众号:小秋的博客 CSDN博客:https://blog.csdn.net/xiaoqiu_cr github:https://github.com/crr121 联系邮箱:rongchen633@gmail.com 有什么问题可以给我留言噢~
  • 相关阅读:
    牛客(46)孩子们的游戏(圆圈中最后剩下的数)
    牛客(45)扑克牌顺子
    牛客(44)翻转单词顺序列
    牛客(43)左旋转字符串
    牛客(42)和为S的两个数字
    牛客(41)和为S的连续正数序列
    牛客(40)数组中只出现一次的数字
    牛客(39)平衡二叉树
    牛客(38)二叉树的深度
    牛客(37)数字在排序数组中出现的次数
  • 原文地址:https://www.cnblogs.com/flyingcr/p/10326866.html
Copyright © 2020-2023  润新知