导入相关依赖

<dependency>
    <groupId>org.apache.hadoop</groupId>
    <artifactId>hadoop-common</artifactId>
    <version>你的Hadoop版本,如3.3.4</version>
</dependency>
<dependency>
    <groupId>org.apache.hadoop</groupId>
    <artifactId>hadoop-hdfs</artifactId>
    <version>你的Hadoop版本,如3.3.4</version>
</dependency>

java示例:

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

public class HDFSOperation {
    public static void main(String[] args) throws Exception {
        // 创建配置对象
        Configuration conf = new Configuration();
        // 设置 HDFS 的地址,根据你的实际情况修改
        conf.set("fs.defaultFS", "hdfs://localhost:9000");
        
        // 获取文件系统对象
        FileSystem fs = FileSystem.get(conf);
        
        // 1. 创建目录
        Path dirPath = new Path("/testDir");
        if (!fs.exists(dirPath)) {
            fs.mkdirs(dirPath);
            System.out.println("目录创建成功");
        }
        
        // 2. 上传本地文件到 HDFS
        Path localPath = new Path("本地文件路径,如/Users/xxx/localFile.txt");
        Path hdfsPath = new Path("/testDir/uploadedFile.txt");
        fs.copyFromLocalFile(localPath, hdfsPath);
        System.out.println("文件上传成功");
        
        // 3. 从 HDFS 下载文件到本地
        Path downloadLocalPath = new Path("本地保存路径,如/Users/xxx/downloadedFile.txt");
        fs.copyToLocalFile(hdfsPath, downloadLocalPath);
        System.out.println("文件下载成功");
        
        // 4. 删除 HDFS 上的文件
        boolean isDeleted = fs.delete(hdfsPath, false);
        if (isDeleted) {
            System.out.println("文件删除成功");
        }
        
        // 关闭文件系统
        fs.close();
    }
}

单词计数:

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

public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
    private final static IntWritable one = new IntWritable(1);
    private Text word = new Text();

    @Override
    public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        // 按空格分割每行文本为单词
        String[] words = value.toString().split(" ");
        for (String w : words) {
            word.set(w);
            // 输出 <单词, 1>
            context.write(word, one);
        }
    }
}
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
    @Override
    public 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));
    }
}
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.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class WordCount {
    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf, "word count");
        
        job.setJarByClass(WordCount.class);
        job.setMapperClass(WordCountMapper.class);
        job.setCombinerClass(WordCountReducer.class); // Combiner 可选,用于本地合并,减少网络传输
        job.setReducerClass(WordCountReducer.class);
        
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        
        // 设置输入和输出路径,根据实际情况修改
        FileInputFormat.addInputPath(job, new Path("hdfs://localhost:9000/input"));
        FileOutputFormat.setOutputPath(job, new Path("hdfs://localhost:9000/output"));
        
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}

Logo

码道开发者社区,聚焦华为云码道 CodeArts 代码智能体,沉淀 Agent、Skill、鸿蒙开发实战内容,供开发者查阅资料、交流技术、分享工程实践

更多推荐