SSH本地连接---Hadoop单词统计

前言

个人小记


一、大致内容

用本地Windows的连接Linux系统的云主机进行Hadoop的单词统计。

二、步骤

1.本地与云主机的连接

Win+R打开终端,用SSH进行与云主机连接

c 复制代码
ssh username@主机端口

2.下载Maven

清华镜像:
https://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/

3.导入依赖pom.xml

c 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>connect-hadoop</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>3.3.0</version> <!-- 根据您的需求选择合适的版本号 -->
        </dependency>

        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>3.3.0</version>
        </dependency>


    </dependencies>

</project>

4.编写代码

c 复制代码
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.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;
import java.util.StringTokenizer;

public class WordCount {

     public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> {

        private final static IntWritable one = new IntWritable(1);
        private Text word = new Text();

        public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
            StringTokenizer itr = new StringTokenizer(value.toString());
            while (itr.hasMoreTokens()) {
                word.set(itr.nextToken());
                context.write(word, one);
            }
        }
    }

    public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable> {

        private IntWritable result = new IntWritable();

        public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
            int sum = 0;
            for (IntWritable val : values) {
                sum += val.get();
            }
            result.set(sum);
            context.write(key, result);
        }
    }

    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(TokenizerMapper.class);
        job.setCombinerClass(IntSumReducer.class);
        job.setReducerClass(IntSumReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}

5.配置Maven的环境变量(bin)

6.生成jar包

c 复制代码
mvn clean package

JAR 文件将被放置在 target 目录中。

7.生成jar包上传到云主机上

c 复制代码
scp /path/to/local/example.jar user@your_cloud_host:/home/user

8.将生成的 JAR 文件上传到 Hadoop 集群

注:输入输出要在hdfs上。

9.在 Hadoop 上运行程序

运行结果

c 复制代码
sjq@SEER:~$ hadoop fs -cat /user/sjq/run-hadoop-jar/output.txt/part-r-00000
hello   2
mapreduce       1
word    1
world   1
相关推荐
lllsure16 分钟前
Linux 日志管理
linux·运维·服务器
yewq-cn36 分钟前
自动更新 Docker 镜像
运维·docker·容器
haluhalu.38 分钟前
Linux系统下进程池设计与实现详解
linux·运维·服务器
m0_5374734939 分钟前
Nginx 生产环境平滑升级实战:从 1.24.0 到 1.28.0 的零宕机操作全记录
运维·nginx
虹梦未来40 分钟前
【运维】Ubuntu2404使用新风格更新镜像源
运维·服务器
一只旭宝1 小时前
Linux专题四:静态库,动态库,进程进阶以及fork()函数初步
linux·运维
小白不想白a1 小时前
ELB--弹性负载均衡器
运维·负载均衡
乾元1 小时前
自动化补丁评估与策略回滚:网络设备固件 / 配置的风险管理
运维·开发语言·网络·人工智能·架构·自动化
杨云龙UP1 小时前
Oracle释放磁盘空间:alert.log和listener.log清理实战记录_20251225
运维·服务器·数据库·sql·oracle
iconball1 小时前
个人用云计算学习笔记 --25 OpenStack 框架
运维·笔记·学习·云计算