第一种:控制台模式
hive>show tables;
hive>create table t1(id int,name string);
hive>insert into t1(id,name) values (1,'zs');
hive>select * from t1;
hive>quit;
第二种:脚本文件方式【实际生产中用的最多的方式】
##第一种:封装到shell脚本中调用【工作中常用】
hive -e "select * from t1"
echo "hive -e \"select * from t1\"" > t1.sh
sh -x t1.sh
##第二种:把sql语句保存到文件中
echo "create table t1(id int,name string);" > t1.sql
hive -f t1.sql
##注:这种方式一般用在初始化表结构或者数据的时候使用
第三种:JDBC方式
hive --service hiveserver2
https://cwiki.apache.org/confluence/display/Hive/HiveServer2+Clients#HiveServer2Clients-JDBC
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
public class HiveDemo {
public static void main(String[] args) throws Exception{
Class.forName("org.apache.hive.jdbc.HiveDriver");
Connection connection = DriverManager.getConnection("jdbc:hive2://bigdata11:10000/default", "root", "");
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("select * from t1");
while (resultSet.next()){
System.out.println(resultSet.getInt("id") + "\t" + resultSet.getString("name"));
}
}
}
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.hive/hive-jdbc -->
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>1.2.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-client -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.7.5</version>
</dependency>
</dependencies>