idea连接本地虚拟机Hadoop集群运行wordcount
虚拟机搭建hadoop集群,请参考:
https://www.cnblogs.com/HusterX/p/14125543.html
环境声明:
1. Hadoop 2.7.0
2 Java 1.8.0
3. window10
4. Vmware workstation pro 16
5. centos7
window上hadoop的安装:
1. 将hadoop.tar.gz文件解压
2. 将 "hadoop安装路径"\bin 添加到PATH路径中
3. 将hadoop.dll文件放到 C:\Windows\System32 目录下,将winutils.exe文件到 "hadoop安装路径"\bin 目录下
PS: 要下载对应版本的 hadoop.dll 和 winutils.exe (如果没有,尽量用高于自己版本的文件)具体文件在github上找。
更改window系统上的hosts文件
路径:C:\Windows\System32\drivers\etc\hosts
1 # Copyright (c) 1993-2009 Microsoft Corp. 2 # 3 # This is a sample HOSTS file used by Microsoft TCP/IP for Windows. 4 # 5 # This file contains the mappings of IP addresses to host names. Each 6 # entry should be kept on an individual line. The IP address should 7 # be placed in the first column followed by the corresponding host name. 8 # The IP address and the host name should be separated by at least one 9 # space. 10 # 11 # Additionally, comments (such as these) may be inserted on individual 12 # lines or following the machine name denoted by a '#' symbol. 13 # 14 # For example: 15 # 16 # 102.54.94.97 rhino.acme.com # source server 17 # 38.25.63.10 x.acme.com # x client host 18 19 # localhost name resolution is handled within DNS itself. 20 # 127.0.0.1 localhost 21 # ::1 localhost 22 127.0.0.1 activate.navicat.com 23 # 下边三个是虚拟机中的IP地址和hostname 24 192.168.47.131 master 25 192.168.47.132 slave1 26 192.168.47.130 slave2hosts
IDEA新建maven项目
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0modelVersion> <groupId>org.examplegroupId> <artifactId>hadoopartifactId> <version>1.0-SNAPSHOTversion> <properties> <project.build.sourceEncoding>UTF-8project.build.sourceEncoding> <maven.compiler.source>1.8maven.compiler.source> <maven.compiler.target>1.8maven.compiler.target> <hadoop.version>2.7.0hadoop.version> properties> <dependencies> <dependency> <groupId>org.apache.hadoopgroupId> <artifactId>hadoop-commonartifactId> <version>${hadoop.version}version> dependency> <dependency> <groupId>org.apache.hadoopgroupId> <artifactId>hadoop-hdfsartifactId> <version>${hadoop.version}version> dependency> <dependency> <groupId>org.apache.hadoopgroupId> <artifactId>hadoop-mapreduce-client-coreartifactId> <version>${hadoop.version}version> dependency> <dependency> <groupId>org.apache.hadoopgroupId> <artifactId>hadoop-mapreduce-client-jobclientartifactId> <version>${hadoop.version}version> dependency> <dependency> <groupId>commons-cligroupId> <artifactId>commons-cliartifactId> <version>1.3.1version> dependency> <dependency> <groupId>org.apache.hadoopgroupId> <artifactId>hadoop-clientartifactId> <version>${hadoop.version}version> dependency> dependencies> project>pom.xml
将集群中的 core-site.xml 和 hdfs-site.xml 放到项目的 resource 目录下。
PS:以下俩个文件中有修改的地方,win系统与虚拟机中的文件要一致,以免报错
<?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="configuration.xsl"?> <configuration> <property> <name>hadoop.tmp.dirname> <value>file:/opt/hadoop/hdfs/tmpvalue> <discription>A base for other temporary directories.discription> property> <property> <name>fs.defaultFSname> <value>hdfs://192.168.47.131:9000value> property> <property> <name>hadoop.proxyuser.hadoop.hostsname> <value>*value> property> <property> <name>hadoop.proxyuser.hadoop.groupsname> <value>*value> property> configuration>core-site.xml
<?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="configuration.xsl"?> <configuration> <property> <name>dfs.namenode.secondary.http-addressname> <value>192.168.47.131:50090value> property> <property> <name>dfs.namenode.http-addressname> <value>192.168.47.131:50090value> property> <property> <name>dfs.replicationname> <value>2value> property> <property> <name>dfs.namenode.name.dirname> <value>file:/opt/hadoop/hdfs/namevalue> property> <property> <name>dfs.permissions.enabledname> <value>falsevalue> property> configuration>hdfs-site.xml
笔者搭建集群中的配置文件样例,供参考
<?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="configuration.xsl"?> <!-- Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. See accompanying LICENSE file. --> <!-- Put site-specific property overrides in this file. -->core-site.xml> > >hadoop.tmp.dir >file:/opt/hadoop/hdfs/tmp >A base for other temporary directories. </property>> >fs.defaultFS >hdfs://192.168.47.131:9000 </property> </configuration>
<?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="configuration.xsl"?> <configuration> <property> <name>dfs.replicationname> <value>2value> property> <property> <name>dfs.namenode.name.dirname> <value>file:/opt/hadoop/hdfs/namevalue> property> <property> <name>dfs.datanode.data.dirname> <value>file:/opt/hadoop/hdfs/datavalue> property> <property> <name>dfs.permissions.enabledname> <value>falsevalue> property> configuration>hdfs-site.xml
<?xml version="1.0"?> <configuration> <property> <name>yarn.resourcemanager.hostnamename> <value>192.168.47.131value> property> <property> <name>yarn.nodemanager.aux-servicesname> <value>mapreduce_shufflevalue> property> <property> <name>yarn.resourcemanager.addressname> <value>192.168.47.131:8032value> property> <property> <name>yarn.resourcemanager.scheduler.addressname> <value>192.168.47.131:8030value> property> <property> <name>yarn.resourcemanager.resource-tracker.addressname> <value>192.168.47.131:8031value> property> configuration>yarn-site.xml
WordCount运行
1. 测试代码
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.log4j.BasicConfigurator; import java.io.IOException; public class HdfsTest { public static void main(String[] args) { //自动快速地使用缺省Log4j环境。 BasicConfigurator.configure(); try { String filename = "hdfs://192.168.47.131:9000/words.txt"; Configuration conf = new Configuration(); FileSystem fs = null; fs = FileSystem.get(conf); if (fs.exists(new Path(filename))){ System.out.println("the file is exist"); }else{ System.out.println("the file is not exist"); } } catch (IOException e) { e.printStackTrace(); } } }HdfsTest
2. WordCount代码示例
import java.io.IOException; import java.util.Iterator; import java.util.StringTokenizer; import org.apache.hadoop.fs.Path; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapred.FileInputFormat; import org.apache.hadoop.mapred.FileOutputFormat; import org.apache.hadoop.mapred.JobClient; import org.apache.hadoop.mapred.JobConf; import org.apache.hadoop.mapred.MapReduceBase; import org.apache.hadoop.mapred.Mapper; import org.apache.hadoop.mapred.OutputCollector; import org.apache.hadoop.mapred.Reducer; import org.apache.hadoop.mapred.Reporter; import org.apache.hadoop.mapred.TextInputFormat; import org.apache.hadoop.mapred.TextOutputFormat; import org.apache.log4j.BasicConfigurator; /** * 单词统计MapReduce */ public class WordCount { /** * Mapper类 */ public static class WordCountMapper extends MapReduceBase implements MapperWordCount.java
3. 将项目导出为jar包
请参考:https://www.cnblogs.com/ffaiss/p/10908483.html
4. 运行WordCount
5. 在 hadoop集群中 运行以下命令,查看结果
查看 / 目录下的文件 hadoop fs - ls / 查看 /out 目录下的文件内容 因为笔者在wordcount程序中设定的输出目录是 /out 所以在这查看该目录,具体根据自己的实际情况而定 hadoop fs -ls /out 一般运行成功会有以下俩个文件 [root@master ~]# hadoop fs -ls /out Found 2 items -rw-r--r-- 2 root supergroup 0 2020-12-19 22:45 /out/_SUCCESS -rw-r--r-- 2 root supergroup 85 2020-12-19 22:45 /out/part-00000 查看part-00000的内容(即程序运行结果) [root@master ~]# hadoop fs -cat /out/part-00000 CAJViewer 1 a 1 free 1 function 1 is 3 it 1 main 1 paper.And 1 software.Its 1 view 1Order