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

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

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

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

  • 启动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

 

 

 

分类:

技术点:

相关文章: