eclipse操作hadoop-文件上传下载以及使用MapReduce完成对一个文本文件的单词计数

本文介绍了如何在Eclipse中利用Hadoop MapReduce框架完成一个单词计数的程序。首先展示了如何操作HDFS,包括查看、重命名、删除文件以及上传下载文件。接着详细说明了MapReduce程序的实现步骤,包括导入所需jar包,编写Mapper、Reducer类以及Driver类。最后,文章提到了可能遇到的权限问题及解决办法,并展示了程序运行结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.查看hdfs上的文件

public static void main(String[] args) {
		try {
			// 创建要链接的资源地址
			URI uri = new URI("hdfs://xxx.xxx.xxx.xxx:端口号");  //这个地址是hadoop目录下etc/hadoop/core-site.xml文件里的 fs.defaultFS 地址

			Configuration conf = new Configuration();
			conf.set("dfs.replication", "1"); // 设置副本数

			// 获取文件系统实例
			FileSystem fs = FileSystem.get(uri, conf, "huangbiao");

			FileStatus[] fss = fs.listStatus(new Path("/user/huangbiao"));//这是要查看的文件系统的目录,看自己的目录是什么
			System.out.println("当前目录下的文件信息如下:");
			for (FileStatus f : fss) {
				System.out.println( f.getPath().getName() );
			}
		} catch (URISyntaxException e) {
			e.printStackTrace();
		} catch(FileNotFoundException e){
			e.printStackTrace();
		}catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

输出结果:
在这里插入图片描述
2.修改hdfs上的文件名:

public static void main(String[] args) {
		try {
			// 创建要链接的资源地址
			URI uri = new URI("hdfs://xxx.xxx.xxx.xxx:端口号"); //这个地址是hadoop目录下etc/hadoop/core-site.xml文件里的 fs.defaultFS 地址

			Configuration conf = new Configuration();
			conf.set("dfs.replication", "1"); // 设置副本数

			// 获取文件系统实例
			FileSystem fs = FileSystem.get(uri, conf, "huangbiao");

			FileStatus[] fss = fs.listStatus(new Path("/user/huangbiao"));
			System.out.println("当前目录下的文件信息如下:");
			for (FileStatus f : fss) {
				System.out.println(f.getPath().getName());
			}
			
			Scanner sc = new Scanner(System.in);
			System.out.println("请输入您要修改的文件名:");
			Path f11 = new Path(sc.next());
			
			System.out.println("请输入修改后的名称:");
			Path f12 = new Path(sc.next());
			
			boolean b1 = fs.rename(f11, f12);
			System.out.println("修改结果:"+b1);
			
		} catch (URISyntaxException e) {
			e.printStackTrace();
		} catch(FileNotFoundException e){
			e.printStackTrace();
		}catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
### 使用EclipseHadoop MapReduce实现词频统计 #### 创建项目环境配置 为了在Eclipse中开发MapReduce应用程序,需先安装必要的插件和支持库。确保已下载并配置好Hadoop环境变量,在Eclipse中通过安装`hadoop-eclipse-plugin`来支持Hadoop操作。 #### 编写Mapper类 Mapper负责处理输入数据流中的每一行记录,将其转换成键值对形式。<word#doc, 1>这种格式意味着每个单词后面附加文档编号作为键的一部分[^1]。然而,对于简单的词频统计任务来说,通常只需要<word, 1>这样的简单映射关系即可满足需求。 ```java public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> { private final static IntWritable one = new IntWritable(1); private Text word = new Text(); @Override protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String line = value.toString(); StringTokenizer tokenizer = new StringTokenizer(line.toLowerCase()); while (tokenizer.hasMoreTokens()) { word.set(tokenizer.nextToken().trim()); context.write(word, one); } } } ``` #### 编写Reducer类 Reducer接收来自不同Mapper产生的中间结果,并对其进行聚合运算。为了避免同一单词被分配至不同的Reducer实例从而造成重复计数的情况发生,应采用自定义Partitioner而非默认的Hash Partitioner。 ```java public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> { @Override protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { int sum = 0; for (IntWritable val : values) { sum += val.get(); } context.write(key, new IntWritable(sum)); } } ``` #### 自定义Partitioner 为了让相同的单词总是发送到同一个Reducer上去处理,可以创建一个继承于`org.apache.hadoop.mapreduce.Partitioner`的新类,重载其中的getPartition方法以确保相同Key始终返回一致的结果。 ```java public class CustomPartitioner extends Partitioner<Text, IntWritable> { @Override public int getPartition(Text key, IntWritable value, int numPartitions) { return Math.abs((key.hashCode() * 127) % numPartitions); // 确保hash分布均匀 } } ``` #### 配置Job参数 最后一步是在Driver程序里设置job的各种属性,比如指定InputFormat、OutputFormat以及指明使用Mapper/Combiner/Reducer等组件。同时也要记得注册自定义的Partitioner以便替代原有的分区逻辑。 ```java Configuration conf = new Configuration(); Job job = Job.getInstance(conf, "Word Count"); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); job.setJarByClass(Driver.class); job.setMapperClass(WordCountMapper.class); job.setCombinerClass(WordCountReducer.class); job.setReducerClass(WordCountReducer.class); job.setNumReduceTasks(3); // 设置reducer数量 job.setPartitionerClass(CustomPartitioner.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值