【hadoop学习】在给定的文件中统计输出每一个字符串出现的总次数

开始学习hadoop,过程十分艰难,借鉴了一些资料,代码仅供自己学习参考

需要先虚拟机dfs和yarn群起,网页打开hadoop100,然后操作

以下是代码实现

Mapper:

package mapreduce1210;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.awt.image.ImageProducer;
import java.io.IOException;

public class it863_01_Mapper extends Mapper<LongWritable, Text,Text, IntWritable> {
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        //todo mao调用时机  每一行数据调用一次map  每一个k,v调用一次map
        String[] s = value.toString().replace("、",",").split(",");
        for (String s1:s){
            context.write(new Text(s1),new IntWritable(1));
        }


    }
}

Reducer:

package mapreduce1210;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class it863_01_Reducer extends Reducer<Text, IntWritable,Text, LongWritable>{
    //todo reduce调用时机   每一个k对应的k,v调用一次reduce
    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
        long count = 0;
        for (IntWritable s_count:values){
            s_count.get();
            count++;
        }
        context.write(key,new LongWritable(count));
    }
}

Driver:

package mapreduce1210;

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.FileInputStream;
import java.io.IOException;

public class it863_01_Driver {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Job job = Job.getInstance();

        //设置要执行的类的路径
        job.setJarByClass(it863_01_Driver.class);

        //设定要执行的mapper类   和k,v
        job.setMapperClass(it863_01_Mapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        //设定要执行的reducer
        job.setReducerClass(it863_01_Reducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);


        //设置文件输入路径
        FileInputFormat.addInputPath(job,new Path("D:\\863863\\day1210\\西游记(1).txt"));

        //设置文件输出路径
        FileOutputFormat.setOutputPath(job,new Path("D:\\863863\\day1210\\out"));
        boolean success = job.waitForCompletion(true);
        if (success){
            System.exit(1);

        }else {
            System.exit(0);
        }
    }

}

输出的文件截图,东西在part-r-00000里面
输出的文件截图,东西在part-r-00000里面 用记事本打开就可以看

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要连接到Hadoop文件系统,需要使用Hadoop的Java API。以下是一个示例代码,用于列出给定路径的所有文件并写入文件或删除文件: ```java import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.List; public class HadoopFileSystemExample { private static final String HDFS_URI = "hdfs://localhost:9000"; public static void main(String[] args) throws IOException { Configuration configuration = new Configuration(); configuration.set("fs.defaultFS", HDFS_URI); FileSystem fileSystem = FileSystem.get(configuration); // 列出给定路径的所有文件 List<String> files = listFiles(fileSystem, "/path/to/files"); // 写入文件 writeToFile(fileSystem, "/path/to/output/file", "Hello World!"); // 删除文件 deleteFile(fileSystem, "/path/to/output/file"); fileSystem.close(); } private static List<String> listFiles(FileSystem fileSystem, String path) throws IOException { List<String> files = new ArrayList<>(); Path hdfsPath = new Path(path); if (fileSystem.exists(hdfsPath)) { for (org.apache.hadoop.fs.FileStatus fileStatus : fileSystem.listStatus(hdfsPath)) { if (fileStatus.isFile()) { files.add(fileStatus.getPath().toString()); } } } return files; } private static void writeToFile(FileSystem fileSystem, String path, String content) throws IOException { Path hdfsPath = new Path(path); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fileSystem.create(hdfsPath))); writer.write(content); writer.close(); } private static void deleteFile(FileSystem fileSystem, String path) throws IOException { Path hdfsPath = new Path(path); fileSystem.delete(hdfsPath, false); } } ``` 在此示例,我们首先创建了一个Hadoop Configuration对象,并设置了默认的文件系统URI。然后,我们通过调用`FileSystem.get()`方法来获取Hadoop FileSystem实例。接下来,我们使用`listFiles()`方法列出给定路径的所有文件,使用`writeToFile()`方法将字符串写入文件并使用`deleteFile()`方法删除文件。最后,我们关闭了FileSystem实例。 需要注意的是,上述代码需要引入Hadoop的依赖包,例如: ```xml <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-common</artifactId> <version>2.7.7</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-hdfs</artifactId> <version>2.7.7</version> </dependency> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值