标签:bin net src second sudo job temp worker oop
本文介绍hadoop集群配置和在windows系统上运用java操作hdfs
http://mirror.bit.edu.cn/apache/hadoop/common/hadoop-3.1.1/
sudo apt-get install ssh
sudo apt-get install rsync
mkdir /usr/local/hadoop
tar -zxvf hadoop-3.1.1.tar.gz -C /usr/local/hadoop
sudo vim /etc/profile
export HADOOP_HOME=/usr/local/hadoop/hadoop-3.1.1
export PATH=.:${JAVA_HOME}/bin:$PATH:$HADOOP_HOME/bin:$HADOOP_HOME/sbin
source /etc/profile
hadoop version
服务器
主机名配置
hostname 查看主机名
sudo vim /etc/hostname 修改主机名
sudo vim /etc/hosts 添加如下配置
192.168.76.128 master
192.168.76.129 worker
shutdown -r now 重启电脑
配置集群ssh免密登陆
cd .ssh
rm id_rsa id_rsa.pub
ssh-keygen -t rsa
进入master主机 cat id_rsa.pub >> authorized_keys
进入worker主机 ssh-copy-id -i master
进入master主机 chmod 600 authorized_keys
进入master主机 scp /root/.ssh/authorized_keys worker:/root/.ssh/
cd /usr/local/hadoop/hadoop-3.1.1/
hadoop-env.sh
sudo vim etc/hadoop/hadoop-env.sh
添加如下配置
export JAVA_HOME=/usr/src/jdk-11.0.1
core-site.xml
sudo vim etc/hadoop/core-site.xml
<configuration>
<!-- 配置NameNode -->
<property>
<name>fs.defaultFS</name>
<value>hdfs://master:9000</value>
</property>
<!-- 配置数据目录,用来存放文件 -->
<property>
<name>hadoop.tmp.dir</name>
<value>/home/hadoop/hadoopdata</value>
</property>
</configuration>
hdfs-site.xml
sudo vim etc/hadoop/hdfs-site.xml
<configuration>
<!-- 配置副本数量 -->
<property>
<name>dfs.replication</name>
<value>2</value>
</property>
</configuration>
yarn-site.xml
sudo vim etc/hadoop/yarn-site.xml
添加如下配置
<configuration>
<!-- 配置管理者-->
<property>
<name>yarn.resourcemanager.hostname</name>
<value>master</value>
</property>
<property>
<name>yarn.nodemanager.aux-services</name>
<value>mapreduce-shuffle</value>
</property>
</configuration>
mapred-site.xml
cp etc/hadoop/mapred-site.xml.template etc/hadoop/mapred-site.xml
sudo vim etc/hadoop/mapred-site.xml
添加如下配置
<configuration>
<!-- 配置集群运行方式-->
<property>
<name>mapreduce.framework.name</name>
<value>yarn</value>
</property>
<property>
<name>mapred.job.tracker</name>
<value>master:9001</value>
</property>
</configuration>
start-dfs.sh
sudo vim sbin/start-dfs.sh
添加如下配置
HDFS_DATANODE_USER=root
HDFS_DATANODE_SECURE_USER=hdfs
HDFS_NAMENODE_USER=root
HDFS_SECONDARYNAMENODE_USER=root
stop-dfs.sh
sudo vim sbin/stop-dfs.sh
添加如下配置
HDFS_DATANODE_USER=root
HDFS_DATANODE_SECURE_USER=hdfs
HDFS_NAMENODE_USER=root
HDFS_SECONDARYNAMENODE_USER=root
start-yarn.sh
sudo vim sbin/start-yarn.sh
添加如下配置
YARN_RESOURCEMANAGER_USER=root
HDFS_DATANODE_SECURE_USER=yarn
YARN_NODEMANAGER_USER=root
stop-yarn.sh
添加如下配置
YARN_RESOURCEMANAGER_USER=root
HDFS_DATANODE_SECURE_USER=yarn
YARN_NODEMANAGER_USER=root
运行
进入master运行 hdfs namenode -format # 格式化
进入master运行 hadoop-daemon.sh start namenode # 开始文件系统
进入worker运行 hadoop-daemon.sh start datanode # 开始文件系统
jps # 查看启动的namenode
netstat -ant # 查看50070端口是否建立连接
访问 http://master:50070 即可看到管理界面
hdfs集群 负责文件读写,namenode管理,datanode负责存储
yarn集群 为mapreduce程序分配硬件资源,resourcemanager需要单独放在另外一台服务器,nodemanager部署在datanode上
hadoop fs -ls / 查看fdfs根目录
hadoop fs -put file1.txt / 上传文件到fdfs根目录
hadoop fs -cat /file1.txt 查看文件内容
hadoop fs -mkdir -p /tests/test 创建文件夹
运行自带的mapreduce程序
cd /usr/local/hadoop/hadoop-2.9.2/share/hadoop/mapreduce
计算指定/tests/test文件夹下面文件的个数,并且将结果放在/test/count下
hadoop jar hadoop-mapreduce-examples-2.9.2.jar wordcount /tests/test /test/count
依赖
缺一不可
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>3.1.1</version>
</dependency>
在你当前操作系统中添加host信息
C:\WINDOWS\system32\drivers\etc\hosts 修改此文件
192.168.76.128 master
192.168.76.129 worker
上传文件
Configuration conf = new Configuration();
conf.set("fs.defaultFS","hdfs://master:9000");
FileSystem fs = FileSystem.get(new URI("hdfs://master:9000"), conf, "root");
fs.copyFromLocalFile(new Path("F:/test.txt"),new Path("/"));
下载文件
下载文件需要当前操作系统也要有hadoop环境,关于windows系统装hadoop环境的步骤如下
1. http://mirrors.tuna.tsinghua.edu.cn/apache/hadoop/common 下载一个hadoop版本
2. http://download.csdn.net/detail/wuxun1997/9841472 下载windows需要的工具包
3. 解压hadoop并且配置HADOOP_HOME环境变量,并且在PATH里加上%HADOOP_HOME%\bin
4. 将工具包解压的文件放置在hadoop文件夹的bin目录中,并且将hadoop.dll放置于c:/windows/System32中即可
Configuration conf = new Configuration();
conf.set("fs.defaultFS","hdfs://master:9000");
FileSystem fs = FileSystem.get(new URI("hdfs://master:9000"), conf, "root");
fs.copyToLocalFile(new Path("/test.txt"), new Path("F:/"));
创建文件夹
Configuration conf = new Configuration();
conf.set("fs.defaultFS","hdfs://master:9000");
FileSystem fs = FileSystem.get(new URI("hdfs://master:9000"), conf, "root");
fs.mkdirs(new Path("/app/test"));
列出根目录所有的文件
Configuration conf = new Configuration();
conf.set("fs.defaultFS","hdfs://master:9000");
FileSystem fs = FileSystem.get(new URI("hdfs://master:9000"), conf, "root");
RemoteIterator<LocatedFileStatus> iterator = fs.listFiles(new Path("/"), true);
while (iterator.hasNext()){
LocatedFileStatus next = iterator.next();
System.out.println(next.getPath());
}
列出根目录所有的文件和文件夹
Configuration conf = new Configuration();
conf.set("fs.defaultFS","hdfs://master:9000");
FileSystem fs = FileSystem.get(new URI("hdfs://master:9000"), conf, "root");
FileStatus[] fileStatuses = fs.listStatus(new Path("/"));
for (int i = 0; i < fileStatuses.length; i++) {
FileStatus fileStatus = fileStatuses[i];
System.out.println(fileStatus.getPath());
}
删除文件
Configuration conf = new Configuration();
conf.set("fs.defaultFS","hdfs://master:9000");
FileSystem fs = FileSystem.get(new URI("hdfs://master:9000"), conf, "root");
fs.delete(new Path("/test.txt"), true);
hadoop集群配置和在windows系统上运用java操作hdfs
标签:bin net src second sudo job temp worker oop
原文地址:https://www.cnblogs.com/ye-hcj/p/10192857.html