准备工作
查看${HIVE_HOME}/conf/hive-site.xml
<property>
<name>hive.server2.thrift.bind.host</name>
<value>master</value>
<description>Bind host on which to run the HiveServer2 Thrift service.</description>
</property>
<property>
<name>hive.server2.thrift.port</name>
<value>10222</value>
<description>Port number of HiveServer2 Thrift interface when hive.server2.transport.mode is 'binary'.</description>
</property>
开启hiveserver2服务
hive --service metastore &
hive --service hiveserver2 > hiveserver2.log 2>&1 &
查看是否成功开启
监听端口测试
netstat -antp|grep 10222
tcp 0 0 0.0.0.0:10222 0.0.0.0:* LISTEN 1776430/java
beeline测试
[root@home bin]# ${HIVE_HOME}/beeline
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/bigdata/binfile/hive-2.3.7/lib/log4j-slf4j-impl-2.6.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/bigdata/binfile/hadoop-2.10.0/share/hadoop/common/lib/slf4j-log4j12-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory]
Beeline version 2.3.7 by Apache Hive
## 输入连接命令
beeline> !connect jdbc:hive2://master:10222/default
## 没有账号密码就回车回车就好了
Connecting to jdbc:hive2://master:10222/default
Enter username for jdbc:hive2://master:10222/default:
Enter password for jdbc:hive2://master:10222/default:
Connected to: Apache Hive (version 2.3.7)
Driver: Hive JDBC (version 2.3.7)
Transaction isolation: TRANSACTION_REPEATABLE_READ
0: jdbc:hive2://master:10222/default> !exit
Closing: 0: jdbc:hive2://master:10222/default
pom
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>2.3.7</version>
<exclusions>
<exclusion>
<groupId>org.eclipse.jetty.aggregate</groupId>
<artifactId>jetty-all</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.hive</groupId>
<artifactId>hive-shims</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.10.0</version>
</dependency>
demo
package per.spring.demo.util;
//hivedriverClassName=org.apache.hive.jdbc.HiveDriver
// hiveurl=jdbc:hive2://192.168.31.243:10222/default
// hiveusername=root
// hivepassword=
public class testHive {
public static void main(String[] args) throws SQLException {
hiveJDBC_RowCount("select * from ads.ads_bill_receivable limit 20");
}
public static void hiveJDBC_RowCount(String sql) throws SQLException {
Connection conn = DriverManager.getConnection("jdbc:hive2://xx.xx.xx.xx:10222/default", "username", "passd");
System.out.println("---- Begin executing sql: " + sql + " ----");
java.sql.PreparedStatement statement = conn.prepareStatement(sql);
ResultSet result = statement.executeQuery();
ResultSetMetaData resultMetaData = result.getMetaData();
Integer colNum = resultMetaData.getColumnCount();
for (int j = 1; j <= colNum; j++) {
System.out.println(resultMetaData.getColumnLabel(j) + "\t");
}
System.out.println();
while (result.next()) {
for (int j = 1; j <= colNum; j++) {
System.out.println(result.getString(j) + "\t");
}
System.out.println();
}
System.out.println("---- Done executing sql: " + sql + " ----");
}
}
博主公众号
求关注
s