MapReduce统计排序和HDFS的读写

实验材料及说明
在Ubuntu系统的/学号(每个人之间的学号)/salesInfo目录下,有买家的购买记录文件Sales,该文件记录了买家的id,购买商品的id以及购买日期,文件为名为Sales。Sales包含:买家ID、商品ID、购买日期三个字段,数据以“\t”进行分割,样本数据及格式如下:
买家ID 商品ID 购买日期
1000181 1000481 2021-04-04 16:54:31
2000001 1001597 2021-04-07 15:07:52
2000001 1001560 2021-04-07 15:08:27
2000042 1001368 2021-04-08 08:20:30
2000067 1002061 2021-04-08 16:45:33
2000056 1003289 2021-04-12 10:50:55
2000056 1003290 2021-04-12 11:57:35
2000056 1003292 2021-04-12 12:05:29
2000054 1002420 2021-04-14 15:24:12
2000055 1001679 2021-04-14 19:46:04
2000054 1010675 2021-04-14 15:23:53
2000054 1002429 2021-04-14 17:52:45
2000076 1002427 2021-04-14 19:35:39
2000054 1003326 2021-04-20 12:54:44
2000056 1002420 2021-04-15 11:24:49
2000064 1002422 2021-04-15 11:35:54
2000056 1003066 2021-04-15 11:43:01
2000056 1003055 2021-04-15 11:43:06
2000056 1010183 2021-04-15 11:45:24
2000056 1002422 2021-04-15 11:45:49
2000056 1003100 2021-04-15 11:45:54
2000056 1003094 2021-04-15 11:45:57
2000056 1003064 2021-04-15 11:46:04
2000056 1010178 2021-04-15 16:15:20
2000076 1003101 2021-04-15 16:37:27
2000076 1003103 2021-04-15 16:37:05
2000076 1003100 2021-04-15 16:37:18
2000076 1003066 2021-04-15 16:37:31
要求根据要求撰写实验报告,实验报告需要包括实验原理、算法设计思路、代码、代码调试说明、实验过程中碰到的问题和代码改进建议等内容。实验报告文件命名规则:HadoopLabX-学号-姓名.doc(X=1,2,3)。具体而言,实验报告需要包括以下内容:

目的

 掌握MapReduce的统计排序和HDFS的读写功能。即要求调用HDFS的Java API,将salesInfo目录下的Sales文件上传到HDFS文件系统的/Sales目录下;调用MapReduce的Java API统计HDFS文件系统/Sales目录下的销售文件中每个买家购买商品的数量;将上一步结果输出到Ubuntu系统的/data/hdfs目录下。

1.在hdfs中创建文件夹

package  shiyan;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class mkdirDemo {
	public static void main(String [] args) throws IOException, URISyntaxException {
	FileSystem fs = FileSystem.get(new URI("hdfs://localhost:9000"), new Configuration());
	fs.mkdirs(new Path("/Sales"));//创建文件夹
//	fs.create(new Path("/x/xx/xxx/xxxx.txt"));//创建文件
	fs.close();		
	System.out.println("success");
	}
}

2.从本地上传文件到hdfs

package shiyan;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class uplownd { 
	    public static void copyFromLocal(String source, String dest)throws IOException, URISyntaxException {
	        // 读取hadoop文件系统的配置
	        Configuration conf = new Configuration();
	        URI uri = new URI("hdfs://localhost:9000");
	        // FileSystem是用户操作HDFS的核心类,它获得URI对应的HDFS文件系统
	        FileSystem fileSystem = FileSystem.get(uri, conf);
	        // 源文件路径
	        Path srcPath = new Path(source);
	        // 目的路径
	        Path dstPath = new Path(dest);
	        // 查看目的路径是否存在
	        if (!(fileSystem.exists(dstPath))) {
	            // 如果路径不存在,即刻创建
	            fileSystem.mkdirs(dstPath);
	        }
	        // 得到本地文件名称
	        String filename = source.substring(source.lastIndexOf('/') + 1,source.length());
	        try {
	            // 将本地文件上传到HDFS
	            fileSystem.copyFromLocalFile(srcPath, dstPath);
	            System.out.println("File " + filename + " copied to " + dest);
	        } catch (Exception e) {
	            System.err.println("Exception caught! :" + e);
	            System.exit(1);
	        } finally {
	            fileSystem.close();
	        }
	    }

	}

3.统计文件中每个买家购买商品的数量

package  shiyan;
import java.io.IOException;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.Reducer.Context;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
public class shangpintongji  {
	 public static class doMapper extends Mapper<Object, Text, Text, IntWritable> {
	//第一个Object表示输入key的类型;第二个Text表示输入value的类型;第三个Text表示表示输出键的类型;第四个IntWritable表示输出值的类型  
	  public static final IntWritable one = new IntWritable(1);
	  public static Text word = new Text();
	  @Override
	  protected void map(Object key, Text value, Context context) throws IOException, InterruptedException {//抛出异常 
	   StringTokenizer tokenizer = new StringTokenizer(value.toString(), " ");//以空格分割
	//StringTokenizer是Java工具包中的一个类,用于将字符串进行拆分 
	   word.set(tokenizer.nextToken());
	//返回当前位置到下一个分隔符之间的字符串
	  context.write(word, one);
	//将word存到容器中,记一个数
	  }
	 }
	 public static class doReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
	  private IntWritable result = new IntWritable();
	  protected void reduce(Text key, Iterable<IntWritable> values, Context context)
	    throws IOException, InterruptedException {
	   int sum = 0;
	   for (IntWritable value : values) {
	    sum += value.get();
	   }
	   result.set(sum);
	   context.write(key, result);
	  }
	 } 
	 public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
		  Job job = Job.getInstance();
		  job.setJobName("shangpintongji");
		  job.setJarByClass(shangpintongji.class);
		  job.setMapperClass(doMapper.class);
		  job.setReducerClass(doReducer.class);
		  job.setOutputKeyClass(Text.class);
		  job.setOutputValueClass(IntWritable.class);
	      Path in=new Path("hdfs://localhost:9000/Sales/Sales");
		  Path out=new Path("hdfs://localhost:9000/shiyan/out1");
		  FileInputFormat.addInputPath(job, in);
		  FileOutputFormat.setOutputPath(job, out);
		  boolean flag = job.waitForCompletion(true);
		  System.out.println(flag);
		  System.exit(flag? 0 : 1);
		 }
	}

4.将输出到hdfs的结果下载到本地

package   shiyan;
import java.io.FileOutputStream;
import java.io.OutputStream;
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;
import org.apache.hadoop.io.IOUtils;
public class output {
	public static void main(String[] args) throws Exception {
		// 获取读取源文件和目标文件位置参数
		String local = "/1863710117/salesInfo/1863710117.txt";
		String uri = "hdfs://localhost:9000/shiyan/out1/part-r-00000";
		FSDataInputStream in = null;
		OutputStream out = null;
		Configuration conf = new Configuration();

		try {
			// 获取读入文件数据
            FileSystem fs = FileSystem.get(URI.create(uri),conf);
            in =fs.open(new Path(uri));
			// 获取目标文件信息
			out = new FileOutputStream(local);
			byte[] buffer = new byte[1024];
			int bytesRead = in.read(buffer);
			if (bytesRead >= 0) {
				out.write(buffer, 0, bytesRead);
			}
		} finally {
			IOUtils.closeStream(in);
			IOUtils.closeStream(out);
		}
		System.out.println("success!");
	}
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值