Hadoop Map Reduce读取文本文件

用户2201650

我正在尝试编写一个MapReduce程序,该程序可以读取输入文件并将输出写入另一个文本文件。我打算为此使用BufferedReader类。但是我真的不知道如何在MapReduce程序中使用它。

有人可以给我一个代码片段吗?

PS我是Hadoop和MapReduce编程的新手。所以,请忍受我。

先感谢您。

温梅莎·斯瑞·凡尼

以下代码可帮助您从HDFS读取文件并在控制台中显示内容

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

public class Cat{
    public static void main (String [] args) throws Exception{
        try{
            Path pt=new Path("hdfs:/path/to/file");//Location of file in HDFS
            FileSystem fs = FileSystem.get(new Configuration());
            BufferedReader br=new BufferedReader(new InputStreamReader(fs.open(pt)));
            String line;
            line=br.readLine();
            while (line != null){
                System.out.println(line);
                line=br.readLine();
            }
        }catch(Exception e){
        }
    }
}

编辑

司机

public class ReadFile {

    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        Job job = new Job(conf, "Read a File");


        FileSystem fs = FileSystem.get(conf);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        if (fs.exists(new Path(args[1])))
            fs.delete(new Path(args[1]), true);
        job.setMapperClass(Map.class);
        job.setReducerClass(Reduce.class);

        job.setInputFormatClass(TextInputFormat.class);
        job.setOutputFormatClass(TextOutputFormat.class);

        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));
        job.setJarByClass(ReadFile.class);     
        job.waitForCompletion(true);
    }

}

映射器

public class Map extends Mapper<LongWritable, Text, Text, IntWritable> {

    public void setup(Context context) throws IOException{
        Path pt=new Path("hdfs:/path/to/file");//Location of file in HDFS
        FileSystem fs = FileSystem.get(new Configuration());
        BufferedReader br=new BufferedReader(new InputStreamReader(fs.open(pt)));
        String line;
        line=br.readLine();
        while (line != null){
            System.out.println(line);
            line=br.readLine();
        }
    }
    public void map(LongWritable key, Text value, Context context)
            throws IOException, InterruptedException {
      //as your wish
        }
    }
}

上面的代码可帮助您从HDFS读取文本文件。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Hadoop Map Reduce哈希程序

来自分类Dev

Hadoop Map-Reduce编程

来自分类Dev

hadoop mapreduce映射器从文本文件中读取不正确的值

来自分类Dev

运行Hadoop Map-Reduce作业

来自分类Dev

将Hadoop输出导出到文本文件

来自分类Dev

将Hadoop输出导出到文本文件

来自分类Dev

hadoop map-reduce:如何部署非jar文件

来自分类Dev

Hadoop Map Reduce让addInputPath使用特殊的文件名

来自分类Dev

将map <string,int>保存到文本文件

来自分类Dev

何时在Hadoop Map-Reduce中使用NLineInputFormat?

来自分类Dev

如何在scala中编写hadoop map reduce程序

来自分类Dev

用于大数据分析的Hadoop Map Reduce框架

来自分类Dev

Hadoop Map-reduce编程语法错误

来自分类Dev

如何在hadoop map reduce中写avro输出?

来自分类Dev

如何为Hadoop的Map-reduce作业设置配置?

来自分类Dev

复合密钥正在更改,Hadoop Map-Reduce?

来自分类Dev

Hadoop,成功完成Map Reduce作业,但没有输出

来自分类Dev

Hadoop Map Reduce-如何将分组与排序分开?

来自分类Dev

Hadoop Map Reduce测试-自定义记录读取器

来自分类Dev

Spark中将纯文本文件转换为Hadoop序列文件

来自分类Dev

将文本文件上传到HDFS(hadoop)的最快方法

来自分类Dev

如何将Hadoop Reducer的最终输出写入文本文件?

来自分类Dev

将文本文件上传到HDFS(hadoop)的最快方法

来自分类Dev

如何将Hadoop Reducer的最终输出写入文本文件?

来自分类Dev

如何通过hadoop mapreduce作业访问Windows / unix目录中的文本文件

来自分类Dev

在Map Reduce作业Hadoop中将文件中的数据用作哈希映射

来自分类Dev

将文本文件插入Map <Integer,Map <Integer,Double >>

来自分类Dev

从文本文件读取

来自分类Dev

从文本文件读取

Related 相关文章

热门标签

归档