下载hive 现在的版本是0.10, 打算是吧元数据存在mysql 里面的.
1.设置 hive_home 环境
2.在 /hive_home/conf 下 添加配置文件
<configuration> <property> <name>hive.metastore.local</name> <value>true</value> <description>controls whether to connect to remove metastore server or open a new metastore server in Hive Client JVM</description> </property> <!-- mysql 的东西 --> <property> <name>javax.jdo.option.ConnectionURL</name> <value>jdbc:mysql://10.158.166.3:3306/hive</value> <description>JDBC connect string for a JDBC metastore</description> </property> <property> <name>javax.jdo.option.ConnectionDriverName</name> <value>com.mysql.jdbc.Driver</value> <description>Driver class name for a JDBC metastore</description> </property> <property> <name>javax.jdo.option.ConnectionUserName</name> <value>root</value> <description>username to use against metastore database</description> </property> <property> <name>javax.jdo.option.ConnectionPassword</name> <value>root</value> <description>password to use against metastore database</description> </property> <!-- web 页面的东西 --> <property> <name>hive.hwi.listen.host</name> <value>0.0.0.0</value> <description>This is the host address the Hive Web Interface will listen on</description> </property> <property> <name>hive.hwi.listen.port</name> <value>9999</value> <description>This is the port the Hive Web Interface will listen on</description> </property> <!-- hwi war包位置 --> <property> <name>hive.hwi.war.file</name> <value>lib/hive-hwi-0.10.0.war</value> <description>This is the WAR file with the jsp content for Hive Web Interface</description> </property> <!-- HDFS 中所放的位置 --> <property> <name>hive.metastore.warehouse.dir</name> <value>/home/hive/warehouse</value> <description>location of default database for the warehouse</description> </property> <!-- 配置log 路径 --> <property> <name>hive.querylog.location</name> <value>/u01/hive/logs</value> <description>location of log</description> </property> </configuration>
3.去mysql 建数据库.hive. 字符集必须用拉丁的.否则会报错的.
4.命令行 输入 hive 进入.
5.如果是远程开发的. 还要打开远程端口.
nohup hive --service hiveserver
nohup hive --service hwi
nohup 是linux 后台继续的命令.
成功后 就可以用 http://10.221.62.68:9999/hwi/ web访问.
6.hiveserver 的默认端口是10000
使用hive的官方例子.
package org.apache.hadoop.examples.hive; import java.sql.SQLException; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import java.sql.DriverManager; public class HiveJdbcClient { private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver"; /** * @param args * @throws SQLException */ public static void main(String[] args) throws SQLException { try { Class.forName(driverName); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); System.exit(1); } Connection con = DriverManager.getConnection( "jdbc:hive://10.221.62.68:10000/hive", "", ""); Statement stmt = con.createStatement(); //drop String tableName = "testHiveDriverTable"; stmt.executeQuery("drop table " + tableName); //create ResultSet res = stmt.executeQuery("create table " + tableName + " (key int, value string)"); // show tables String sql = "show tables '" + tableName + "'"; System.out.println("Running: " + sql); res = stmt.executeQuery(sql); if (res.next()) { System.out.println(res.getString(1)); } // describe table sql = "describe " + tableName; System.out.println("Running: " + sql); res = stmt.executeQuery(sql); while (res.next()) { System.out.println(res.getString(1) + "\t" + res.getString(2)); } // load data into table // NOTE: filepath has to be local to the hive server // NOTE: /tmp/a.txt is a ctrl-A separated file with two fields per line String filepath = "/tmp/a.txt"; sql = "load data local inpath '" + filepath + "' into table " + tableName; System.out.println("Running: " + sql); res = stmt.executeQuery(sql); // select * query sql = "select * from " + tableName; System.out.println("Running: " + sql); res = stmt.executeQuery(sql); while (res.next()) { System.out.println(String.valueOf(res.getInt(1)) + "\t" + res.getString(2)); } // regular hive query sql = "select count(1) from " + tableName; System.out.println("Running: " + sql); res = stmt.executeQuery(sql); while (res.next()) { System.out.println(res.getString(1)); } } }
改下ip 就可以运行了.
如果 hiveserver 没有起来的话 是会报connection的错.