hadoop文件上传

要能通过java上传文件到网页上localhost:9870

首先要启动start-yarn.sh start-dfs.sh

jps能显示五个功能

java项目里

配置好maven的各项,与现有版本匹配

配置好pom.xml

在<main-class>cn.kgc.story.StoryJob</main-class>里填好要执行的路径

在Story包下建三个类StoryJob,StoryMapper,StoryReducer

StoryMapper

StoryMapper:map阶段:通过正则提取所有的符号,将每个符号以键值对(符号,1)的方式输出到reduce阶段;

//Text 提供以 与自然语言无关的方式 来处理文本、日期、数字和消息的类与接口。

private Text outputKey = new Text();

//IntWriteable 这个包没查到用法

private IntWritable outputValue = new IntWritable(1);

//需要重写map方法
@Override
protected void map(LongWritable key,Text value,Context context)
        throws IOException,InterruptedException{
    //将值转换返回一个“以文本方式表示”此对象的字符串。结果应是一个简明但易于读懂的信息表达式。
    String line = value.toString();
    //replaceAll() 替换字符串所有匹配的字母数字替换为“空”
    line.replaceAll("[a-zA-Z0-9]+","");
    //toCharArray() 方法将字符串转换为字符数组
    for (char c : line.toCharArray()) {
        outputKey.set(String.valueOf(c));
        //输出
        context.write(outputKey,outputValue);
    }
    System.out.println(line);//最后输出分隔出来的符号
}

StoryReducer

StoryReducer:reduce阶段:将map阶段的键值对(符号,重复次数的数组)输入进来,遍历这个重复次数的数组元素,累加求和,然后将键值对(符号,求和的总数)输出出去 job阶段:整合reduce阶段输出的键值对输出到HDFS中

private  IntWritable outputValue = new IntWritable(0);
@Override
protected void reduce(Text key,Iterable<IntWritable> values,Context context)
        throws IOException,InterruptedException{
    int count =0;
    for (IntWritable value: values){//遍历Storymapper里IntWritable里的重复的,将他们计数
        count += value.get();
    }
    outputValue.set(count);//将对应的值设置为他们重复的次数
    context.write(key,outputValue);//将键值对写进文件
}

StoryJob

StoryJob用于执行:将map阶段和reduce阶段关联在一起,提交reduce阶段最终的输出结果到hdfs

Configuration config = new Configuration

Configuration 这个是在hadoop 里创建两个文件

hdfs-site.xml以及core-site.xml,这两个文件里面会有访问hadoop所需的参数

然后config.set(第一个fs.default.name是用于hadoop里的名字,第二个是存放hdfs:地址:端口)

//下面这是源数据路径,结果输出的路径

final Path pathSrc = new Path("/kb17/hadoop/story/Brok。。"填网页上创建的文件)

//确保下面要创建的文件不存在

final Path pathDst = new Path("kb17/hadoop/story/rst"),这个里面的rst是新创建的,那上面的Brok。。。 也是新生成的,只不过Brok文件在Story下。rst作为一个新目录,下面放继续要上传的东西.

FileSystem dfs = null;

try{

dfs = FileSystem.get(config);//获取config里的文件

if (dfs.exists(pathDst)) {//判断是否存在pathDst里的文件,有就删除
        boolean delete = dfs.delete(pathDst, true);
        if (delete) {
​
            System.out.println(pathDst.getName() + "EXIST AND DELETE");
        }
    }
} catch (IOException e) {
    e.printStackTrace();
}finally {
    if(null!=dfs){
        try {
            dfs.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
     //任务
        try {
            //创建任务
            getInstace()与new一个对象的区分
对象使用之前通过getInstance得到而不需要自己定义,用完之后不需要delete;
​
(2)new 一定要生成一个新对象,分配内存;getInstance() 则不一定要再次创建,它可以把一个已存在的引用给你使用,这在效能上优于new;
​
(3) new创建后只能当次使用,而getInstance()可以跨栈区域使用,或者远程跨区域使用。所以getInstance()通常是创建static静态实例方法的。
​
            Job job = Job.getInstance(config);
            
            setJarByClass该方法的作用是 通过传入的class 找到Storyjob的jar包
            job.setJarByClass(StoryJob.class);
            
            setNumReducetasks是用来设置reduce的数量
            job.setNumReduceTasks(1);
            //配置任务的时候要有mapper和reducer
            //1,配置Mapper
            setMapper设置映射器
            job.setMapperClass(StoryMapper.class);
            //map端输出键的数据类型Text.class
            job.setMapOutputKeyClass(Text.class);
            // map端输出值的数据类型IntWritable.class
            job.setMapOutputValueClass(IntWritable.class);
​
            //2,配置 Reducer
            job.setReducerClass(StoryReducer.class);
            job.setOutputKeyClass(Text.class);
            job.setOutputValueClass(IntWritable.class);
​
            //3,配置 Input and Output
            //FileInputFormat.setInputPaths在读文件时,默认先读单个大文件所在的路径(job,input1,input2..)
            FileInputFormat.setInputPaths(job,pathSrc);
            //FileOutputFormat.setOutputPath指定输出路径(不存在的路径)
            FileOutputFormat.setOutputPath(job,pathDst);
​
            //强制等待任务完成
            job.waitForCompletion(true);
        } catch (IOException e) {
            e.printStackTrace();
        }catch (InterruptedException e){
            e.printStackTrace();
        }catch (ClassNotFoundException e){
            e.printStackTrace();
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值