服务端启动
配置文件是Hive的配置文件
服务端启动
hiveserver2
客户端启动
- 进入后连接
$ beeline
beeline> !connect jdbc:hive2://node2:10000
Connecting to jdbc:hive2://node2:10000
Enter username for jdbc:hive2://node2:10000: root
Enter password for jdbc:hive2://node2:10000:
默认端口号为10000,用户名使用root,无密码
- 直接连接
beeline -u jdbc:hive2://node2:10000 -u root
JDBC
pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.avcdata</groupId>
<artifactId>boot2hive2</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.avcdata.Main</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.hive/hive-jdbc -->
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>1.2.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.6.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.49</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>central</id>
<name>Central Repository</name>
<url>http://repo.maven.apache.org/maven2</url>
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
Java代码
package com.avcdata;
import com.alibaba.fastjson.JSONObject;
import java.sql.*;
import java.util.List;
import java.util.Map;
public class Main {
public static void main(String[] args) throws SQLException {
try {
String driverName = "org.apache.hive.jdbc.HiveDriver";
Class.forName(driverName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection conn = DriverManager.getConnection("jdbc:hive2://node2:10000", "root", "");
Statement stmt = conn.createStatement();
String sql = "SELECT * FROM psn7";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
List<String> likes = JSONObject.parseArray(rs.getString(3), String.class);
Map address = JSONObject.parseObject(rs.getString(4), Map.class);
System.out.println(rs.getString(1) + "\t" + rs.getString(2));
System.out.println(likes);
System.out.println(address);
}
}
}