hadoop 2.8.1安裝和配置(單機)

1、java環境(略)java

2、配置SSH無密碼登錄node

$ ssh-keygen -t dsa -P '' -f ~/.ssh/id_dsa
$ cat ~/.ssh/id_dsa.pub >> ~/.ssh/authorized_keys

$ ssh-keygen -t rsa 
$ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys 
$ chmod 0600 ~/.ssh/authorized_keys

出現sign_and_send_pubkey: signing failed: agent refused operationlinux

eval 'ssh-agent -s'
ssh-add

測試無密碼登陸apache

$ ssh localhost 
Welcome to Ubuntu 16.04.3 LTS (GNU/Linux 4.10.0-32-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

9 packages can be updated.
8 updates are security updates.

Last login: Tue Aug 15 23:16:52 2017 from ::1

3、建立hadoop用戶組、用戶ubuntu

sudo addgroup hadoop
sudo adduser --ingroup hadoop hduser
sudo adduser hduser sudo

打算以普通用戶(平時經常使用的這裏mymotif)啓動,故把它加到hadoop組eclipse

sudo usermod -a -G hadoop mymotif

4、下載、解壓、改own等ssh

cd
wget http://mirrors.tuna.tsinghua.edu.cn/apache/hadoop/common/hadoop-3.1.1/hadoop-3.1.1.tar.gz
#wget http://apache.fayea.com/hadoop/common/hadoop-2.8.1/hadoop-2.8.1.tar.gz
cd /opt
sudo tar xzvf  ~/hadoop-3.1.1.tar.gz 
sudo mv hadoop-3.1.1 hadoop
sudo chown -R hduser:hadoop hadoop

數據存放目錄/home/hduser/tmpjvm

su hduser
cd
mkdir tmp
chmod g+rw -Rf tmp            #爲了使得mymotif用戶對該目錄有讀寫權限
exit

5、配置oop

etc/hadoop/core-site.xml文件測試

<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
core-site.xml
 <!-- 指定HDFS老大(namenode)的通訊地址 -->
    <property>
        <name>fs.default.name</name>
        <value>hdfs://localhost:9000</value>
    </property>
<!-- 指定hadoop運行時產生文件的存儲路徑 -->
    <property>
        <name>hadoop.tmp.dir</name>
        <value>/home/hduser/tmp</value>
    </property>
</configuration>

etc/hadoop/hdfs-site.xml

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
                     <!-- 設置hdfs副本數量 -->
      <property>
        <name>dfs.replication</name>
        <value>1</value>
      </property>
      <property>
        <name>dfs.namenode.name.dir</name>
        <value>file:/home/hduser/tmp/dfs/name</value>
      </property>
      <property>
        <name>dfs.datanode.data.dir</name>
        <value>file:/home/hduser/tmp/dfs/data</value>
      </property>
</configuration>

etc/hadoop/hadoop-env.sh 中export JAVA_HOME=${JAVA_HOME}行改爲以下內容:

# The java implementation to use.
export JAVA_HOME=/opt/lib/jvm/jdk1.8.0_141
export HADOOP_COMMON_LIB_NATIVE_DIR="/opt/hadoop/lib/native/"
export HADOOP_OPTS="$HADOOP_OPTS -Djava.library.path=/opt/hadoop/lib:/opt/hadoop/lib/native"

修改環境變量

全局變量/etc/profile加下面這段

#Hadoop variables
export HADOOP_INSTALL=/opt/hadoop
export HADOOP_HOME=/opt/hadoop
export PATH=$PATH:$HADOOP_HOME/bin
export PATH=$PATH:$HADOOP_HOME/sbin
export HADOOP_MAPRED_HOME=$HADOOP_HOME
export HADOOP_COMMON_HOME=$HADOOP_HOME
export HADOOP_HDFS_HOME=$HADOOP_HOME
export YARN_HOME=$HADOOP_HOME
###end of paste

6、Hadoop啓動

1)格式化namenode

$ hdfs namenode –format
2)啓動NameNode 和 DataNode 守護進程

$ start-dfs.sh
3)啓動ResourceManager 和 NodeManager 守護進程

$ start-yarn.sh

【注:start-all.sh 已不被建議使用】

文件存放:

$ hadoop fs -mkdir /user
$ hadoop fs -mkdir /user/hadoop
$ hadoop fs -mkdir /user/input
$ hadoop fs -ls /user/
Found 2 items
drwxr-xr-x   - mymotif supergroup          0 2017-08-18 14:50 /user/hadoop
drwxr-xr-x   - mymotif supergroup          0 2017-08-16 01:10 /user/input
$ hadoop fs -put test.c /user/hadoop/
$ hadoop fs -ls /user/hadoop
Found 1 items
-rw-r--r--   1 mymotif supergroup         66 2017-08-18 14:54 /user/hadoop/test.c
$ hadoop fs -copyFromLocal ~/hello.c /user/input/
$ hadoop fs -copyFromLocal ~/test.c /user/input/
$ hadoop fs -ls /user/input
Found 2 items
-rw-r--r--   1 mymotif supergroup         72 2017-08-19 15:46 /user/input/hello.c
-rw-r--r--   1 mymotif supergroup         66 2017-08-16 01:10 /user/input/test.c

獲取

hadoop fs -get /user/hadoop/test.c mytest.c
或
$ hadoop fs -get hdfs://localhost:9000/user/hadoop/test.c mytest.c

7、驗證

$ jps
11856 NameNode
12210 SecondaryNameNode
12821 Jps
12502 NodeManager
12378 ResourceManager
12013 DataNode

管理界面:http://localhost:8088

NameNode界面:http://localhost:50070

HDFS NameNode界面:http://localhost:8042

其它問題:

$ ldd /opt/hadoop/lib/native/libhadoop.so.1.0.0
    linux-vdso.so.1 =>  (0x00007ffe79fb9000)
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f7b3feaa000)
    libjvm.so => not found
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f7b3fc8d000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f7b3f8c3000)
    /lib64/ld-linux-x86-64.so.2 (0x0000561243d70000)

 libjvm.so 找不到,執行下面的指令便可:

sudo ln -s /opt/lib/jvm/jdk1.8.0_141/jre/lib/amd64/server/libjvm.so /lib/x86_64-linux-gnu/libjvm.so

eclipse插件下載地址:http://download.csdn.net/download/darkdragonking/9849522

https://pan.baidu.com/s/1eSpd7zk

java測試代碼:

package test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

public class ReadHDFS {

	public static String getStringByTXT(String txtFilePath, Configuration conf)
	{

		FSDataInputStream fsr = null;
		BufferedReader bufferedReader = null;
		String lineTxt = null;
		try
		{
			FileSystem fs = FileSystem.get(URI.create(txtFilePath),conf);
			fsr = fs.open(new Path(txtFilePath));
			bufferedReader = new BufferedReader(new InputStreamReader(fsr));		
			while ((lineTxt = bufferedReader.readLine()) != null)
			{
				if(lineTxt.split("\t")[0].trim().equals("序號")){
					return lineTxt;
				}
				
			}
		} catch (Exception e)
		{
			e.printStackTrace();
		} finally
		{
			if (bufferedReader != null)
			{
				try
				{
					bufferedReader.close();
				} catch (IOException e)
				{
					e.printStackTrace();
				}
			}
		}

		return lineTxt;
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Configuration conf = new Configuration();
		String txtFilePath = "hdfs://localhost:9000/user/input/myhello.txt";
		String mbline = getStringByTXT(txtFilePath, conf);
		System.out.println(mbline);
	}

}

myhello.txt內容

$ hadoop fs -cat hdfs://localhost:9000/user/input/myhello.txt
1序號	學院	班級	學號	姓名	
序號 234	學院	班級	學號	姓名	
序號	學院12 34	班級	學號	姓名

運行結果:

序號	學院12 34	班級	學號	姓名

enjoy  it!

相關文章
相關標籤/搜索