1桑孩、因為我使用MySQL做為Hive的元數(shù)據庫,所以先安裝MySQL夯辖。
參考:http://www.cnblogs.com/hunttown/p/5452205.html
登錄命令:mysql -h主機地址 -u用戶名 -p用戶密碼
mysql –u root#初始登錄沒有密碼
修改密碼
格式:mysqladmin -u用戶名 -p舊密碼 password 新密碼
mysql>mysqladmin -uroot –password 123456
注:因為開始時root沒有密碼爱榔,所以-p舊密碼一項就可以省略了。
創(chuàng)建用于hive的用戶hadoopuser
創(chuàng)建用戶命令:CREATE USER username@"host" IDENTIFIED BY 'password';
mysql> CREATE USER hadoopuser@"192.168.254.151" IDENTIFIED BY '123456';
授權命令:GRANT privileges ON databasename.tablename TO 'username'@'host'
mysql> GRANT ALL PRIVILEGES ON *.* TO hadoopuser@"192.168.254.151" WITH GRANT OPTION;
創(chuàng)建用戶和授權可以一起做:
mysql> GRANT ALL PRIVILEGES ON *.* TO hadoopuser@"192.168.254.151" IDENTIFIED BY '123456' WITH GRANT OPTION;
創(chuàng)建數(shù)據庫hive,用于hive數(shù)據存儲
mysql> create database hive
2脐雪、解壓hive到 /home/hadoopuser/
3、切換到root用戶恢共,加入環(huán)境變量:
export HIVE_HOME=/home/hadoopuser/hive
export PATH=$PATH:$HIVE_HOME/bin
4战秋、使用root用戶,給hive/bin增加權限
chmod 777 /hive/bin/*
5讨韭、配置文件
切換到? /hive/conf
cp hive-default.xml.template? hive-site.xml
cp hive-log4j.properties.template hive-log4j.properties#Hive-2.1.0沒有此配置項
(1)配置hive-site.xml
javax.jdo.option.ConnectionURLjdbc:mysql://192.168.254.156:3306/hive?createDatabaseIfNotExist=trueJDBC connect string for a JDBC metastore
javax.jdo.option.ConnectionDriverNamecom.mysql.jdbc.DriverDriver class name for a JDBC metastore
javax.jdo.option.ConnectionUserNamehadoopusername to use against metastore database
javax.jdo.option.ConnectionPassword123456password to use against metastore database
hive.metastore.warehouse.dir/user/hive/warehouselocation of default database for the warehouse
如果使用derby元數(shù)據庫脂信,則JDBC要配置成:
javax.jdo.option.ConnectionURLjdbc:derby:/opt/hive/metastore_db;create=trueJDBC connect string for a JDBC metastore
注1:倉庫目錄如果沒有要創(chuàng)建
hdfs dfs –mkdir /user/hive
hdfs dfs –mkdir -p/user/hive/warehouse
注2:mysql的驅動jar包要上傳到hive/lib下
(2)配置hive-log4j.properties
#log4j.appender.EventCounter=org.apache.hadoop.metrics.jvm.EventCounterlog4j.appender.EventCounter=org.apache.hadoop.log.metrics.EventCounter
6、在HDFS中創(chuàng)建/tmp和/user/hive/warehouse并設置權限
hadoop fs -mkdir /tmp
hadoop fs-mkdir -p /user/hive/warehouse
hadoop fs-chmod g+w /tmp
hadoop fs-chmod g+w /user/hive/warehouse
注:hadoop 命令換成了hdfs命令透硝,上面的命令如同下面的命令
hdfs dfs -mkdir /tmp
hdfs dfs-mkdir -p /user/hive/warehouse
hdfs dfs-chmod g+w /tmp
hdfs dfs-chmod g+w /user/hive/warehouse
7狰闪、手動上傳mysql的jdbc庫到hive/lib目錄。
http://mirror.bit.edu.cn/mysql/Downloads/Connector-J/
mysql-connector-java-5.1.22-bin.jar
8濒生、初始化? 如果使用derby元數(shù)據庫埋泵,那么需要進行初始化:
[hadoopuser@Hadoop-NN-01 ~]#schematool -initSchema -dbType derby#執(zhí)行成功信息Starting metastore schema initialization to 2.0.0Initialization script hive-schema-2.0.0.derby.sql
Initialization script completed
schemaTool completed
如果運行時出現(xiàn)以下錯誤,說明上面的步驟沒有執(zhí)行罪治,請執(zhí)行:
Exceptioninthread"main"java.lang.RuntimeException: Hive metastore database is not initialized. Please use schematool (e.g. ./schematool -initSchema -dbType ...) to create the schema.Ifneeded, don't forget to include the option to auto-create the underlying databaseinyour JDBC connection string (e.g. ?createDatabaseIfNotExist=trueformysql)
如果使用schematool初始化數(shù)據庫時出現(xiàn)以下錯誤:
Initialization script hive-schema-2.1.0.derby.sql
Error:FUNCTION'NUCLEUS_ASCII' already exists. (state=X0Y68,code=30000)
org.apache.hadoop.hive.metastore.HiveMetaException: Schema initialization FAILED! Metastore state would be inconsistent !!*** schemaTool failed ***
說明數(shù)據庫文件夾中已經存在一些文件丽声,解決方法就是清空數(shù)據庫文件夾(也就是前面配置的/opt/hive/metastore_db文件夾)
9、啟動hive
hive --service metastore &#啟動metastore服務hive --service hiveserver2 &#啟動hiveserver服務hive shell#啟動hive客戶端
Hive使用
1觉义、創(chuàng)建數(shù)據庫
CREATE DATABASE myhive;
2雁社、創(chuàng)建表
CREATE TABLE doc_hive (id int, username string, sex int, age int, email string, createtime string) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t';
3、導入數(shù)據
LOAD DATA LOCAL INPATH '/home/hadoopuser/doc/t-1.txt' OVERWRITE INTO TABLE doc_hive ;
驗證:select * from myhive.doc_hive;
Hive的具體使用晒骇,在接下來的博客中會有體現(xiàn)霉撵。