hadoop+hive+JDK平台搭建

我的linux 安装的是centos-6.8版本,安装的是minimal版本
没有web界面的纯净版,如果要下载什么自己就下载什么

一,前期工作
1,修改linux ip

vi /etc/sysconfig/network-scripts/ifcfg-eth0

修改内容

BOOTPROTO=staic
IPADDR=192.168.243.131
NETMASK=255.255.255.0
GATEWAY=192.168.243.2
DNS1=192.168.243.2

2,修改主机名
vi /etc/sysconfig/network

HOSTNAME=bigdata1

3,修改主机名与ip对应关系
vi /etc/hosts

 192.168.243.131 bigdata1
 192.168.243.132 bigdata2
 192.168.243.133 bigdata3

4,关闭防火强

查看防护墙专态

service iptables status

关闭

serivice iptables stop

查看防火墙开机启动状态

chkconfig iptables --list

永久关闭开机启动

chkconfig iptables off

开启启动

chkconfig iptables on

三台虚拟机

bigdata1 192.168.243.131
bigdata2 192.168.243.132
bigdata3 192.168.243.133

修改好后

reboot 重启

重启网络

service network start

查看IP地址

ifconfig

查看网络是否与外界相通
ping www.baidu.com

二,开始安装

创建用户以及添加密码

adduser hadoop
passwd hadoop

更改用户权限

visudo

复制ctrl+y
粘贴ctrl+p

安装上传下传工具

yum -y install lrzsz 
rz XXX 上传
sz XXX 下传

linux jdk1.8安装

linux jdk1.8下载链接:祝你好运!

链接: https://pan.baidu.com/s/1HdayKolc-39tx6Q5jdjuQw 密码: s6rk

切换用户

su - hadoop

上传JDK

rz

解压JDK

tar -zxvf jdk-8u171-linux-x64.tar.gz 

添加环境变量
vi /etc.profile

export JAVA_HOME=/home/hadoop/jdk1.8.0_171
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

export PATH=$PATH:$JAVA_HOME/bin

三,安装Hadoop
1.下载hadoop
https://archive.apache.org/dist/
https://archive.apache.org/dist/hadoop/core/hadoop-2.7.2/
本次下载的是: hadoop-2.7.2.tar.gz

2.解压Hadoop包

tar -zxvf hadoop-2.7.2.tar.gz 

3.配置HADOOP伪分布式(要修改etc/hadoop下的4个文件)

第一个:hadoop-env.sh
vim hadoop-env.sh

 export JAVA_HOME=/home/hadoop/jdk1.8.0_171

第二个:core-site.xml


      
    
            fs.defaultFS
            hdfs://bigdata1:9000
    
      
    
            hadoop.tmp.dir
            /home/hadoop-2.7.2/tmp
    

第三个:hdfs-site.xml


         
      
            dfs.replication
            1
    



       dfs.permissions.enabled
       false


	
            dfs.namenode.secondary.http-address
            bigdata1:50090
    
    
            dfs.replication
            1
    

    
            dfs.datanode.data.dir
            /home/hadoop/hadoop-2.7.2/tmp/dfs/data
    


  
dfs.namenode.rpc-address.bigdata1
  bigdata1:8020 




 dfs.namenode.http-address.bigdata1
 bigdata1:50070


第四个:mapred-site.xml (需要从此文件复制mapred-site.xml.template)


	          
	        
	                mapreduce.framework.name
	                yarn
	        
			
	                mapreduce.jobhistory.address
	                bigdata1:10020
	        
	        
	                mapreduce.jobhistory.webapp.address
	                bigdata1:19888
	        
	    
	        mapred.job.tracker
	        http://bigdata1:9001
	    
	
	     

第五个:yarn-site.xml


      
    
            yarn.nodemanager.aux-services
            mapreduce_shuffle
    
      
    
            yarn.resourcemanager.hostname
            bigdata1
    
	
	  
	          yarn.resourcemanager.webapp.address
	          bigdata1:8088
	  		


第六个 :slaves

bigdata1

4.将Hadoop添加到环境变量中
vim /etc/profile

export HADOOP_HOME=/home/hadoop/hadoop-2.7.2

#刷新配置
source /etc/profile

5.初始化HDFS(格式化文件系统,此步类似刚买U盘需要格式化)

 #hadoop namenode -format(过时了)
 hdfs namenode -format

6.启动文HDFS和YARN
./start-all.sh(过时了)This script is Deprecated. Instead use start-dfs.sh and start-yarn.sh
Starting namenodes on [it]

jps(jps 在Linux/unix平台上简单察看当前java进程的一些简单情况),如果有以下进程则表示测试通过

六、配置SSH免密码登录(/root/.ssh)
#SSH协议(让linux不同机器,启动起来。hadoop老大为了让小弟听话,使用SSH进行控制)
ssh-keygen -t rsa
执行完这个命令后,会生成两个文件id_rsa(私钥)、id_rsa.pub(公钥)

 将公钥拷贝成authorized_keys 文件上
 cp id_rsa.pub authorized_keys

yum install openssh-clients
ssh-copy-id bigdata1

安装Hive
安装mysql来替换默认的Derby数据库
⑦.安装配置MySQL(主节点)

通过yum install mysql-server安装mysql服务器。

chkconfig mysqld on设置开机启动,并service mysqld start启动mysql服务,
设置root的初试密码:mysqladmin -u root password 'root'。
 
mysql -uroot -p进入mysql命令行,创建以下数据库:
#为hive建库hive
create database hive DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
#activity monitor
create database amon DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
#为oozie建库oozie
create database oozie DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
#为hue建库hue
create database hue DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
create database reports DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
 
设置root授权访问以上所有的数据库:
#授权root用户在主节点拥有所有数据库的访问权限
grant all privileges on *.* to 'root'@'%' identified by 'root' with grant option;
grant all privileges on *.* to 'root'@'bigdata1' identified by 'root' with grant option;
grant all privileges on *.* to 'root'@'127.0.0.1' identified by 'root' with grant option;
grant all privileges on *.* to 'root'@'localhost' identified by 'root' with grant option;
flush privileges;

解压hive
tar -zxvf apache-hive-1.2.2-bin.tar.gz

yum install -y mysql-connector-java
cp /usr/share/java/mysql-connector-java-5.1.17.jar /usr/local/hive/lib

vi hive-env.sh
cp hive-env.sh.template hive-env.sh

	vi /usr/local/hive/bin/hive-env.sh

	export HADOOP_HOME=/home/hadoop/hadoop-2.7.2

vi hive-site.xml

	  
	system:java.io.tmpdir  
	/home/hadoop/apache-hive-1.2.2-bin/tmp  
	  
	    
	  
	javax.jdo.option.ConnectionURL  
	jdbc:mysql://bigdata1:3306/hive?createDatabaseIfNotExist=true  
	JDBC connect string for a JDBC metastore  
	  
	  
	javax.jdo.option.ConnectionDriverName  
	com.mysql.jdbc.Driver  
	Driver class name for a JDBC metastore  
	  
	  
	javax.jdo.option.ConnectionUserName  
	root  
	username to use against metastore database  
	  
	  
	javax.jdo.option.ConnectionPassword  
	root  
	password to use against metastore database  
	  
	  
	hive.metastore.warehouse.dir  
	/user/hive/warehouse  
	location of default database for the warehouse  
	  

要使用Hadoop来创建相应的文件路径,
并且要为它们设定权限:

	hdfs dfs -mkdir -p  /user/hive/warehouse
	hdfs dfs -mkdir -p /user/hive/tmp
	hdfs dfs -mkdir -p /user/hive/log
	hdfs dfs -chmod g+w /user/hive/warehouse
	hdfs dfs -chmod g+w /user/hive/tmp
	hdfs dfs -chmod g+w /user/hive/log

#init mysql database
bin/schematool -initSchema -dbType mysql

运行hive之前首先要确保meta store服务已经启动,

  nohup bin/hive --service metastore > metastore.log 2>&1 &

如果需要用到远程客户端(比如 Tableau)连接到hive数据库,还需要启动hive service

 nohup bin/hive --service hiveserver2 > hiveserver2.log 2>&1 &

use hive;
create table dep(id int,name string)row format delimited fields terminated by ‘\t’

make.txt
1001 a
1002 b
1003 c
1004 d

load data local inpath ‘/home/hadoop/make.txt’ into table dep;
select * from dep;

hive API 测试

/Ctrl+Alt+t快熟生成代码块
/ctrl+Alt+V 自动补充变量
    try {
        //1.加载驱动
        Class.forName("org.apache.hive.jdbc.HiveDriver");

        //Ctrl+Alt+v 自动生成变量
        //2.打开连接
        Connection conn =  DriverManager.getConnection("jdbc:hive2://192.168.243.131:10000/");

        //3.获得操作对象(开启会话)

        Statement statement = conn.createStatement();

        //4执行操作
       // statement.execute("use hive");

        String sql1= "select * from dep";

        ResultSet rs = statement.executeQuery(sql1);

        //5.接受返回结果
        while (rs.next()){
            System.out.println(rs.getString(1)+"\t"+rs.getString(2));
        }
    } catch (ClassNotFoundException e) {
        e.printStackTrace();     
        }

出现结果:

1001 a
1002 b
1003 c
1004 d

你可能感兴趣的:(大数据&云计算)