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
相关推荐
caijingshiye16 分钟前
九科信息企业自动化智能体:打破知行割裂,让AI真正动手干活
运维·人工智能·自动化
HIT_Weston2 小时前
26、【Ubuntu】【远程开发】内网穿透:密钥算法介绍(二)
linux·运维·ubuntu
大地的一角4 小时前
(Linux)ELF格式与库的链接原理
linux·运维·服务器
z202305084 小时前
Linux之中断子系统-内核中断注册源码分析(4)
linux·运维·服务器
极小狐6 小时前
比 Cursor 更丝滑的 AI DevOps 编程智能体 - CodeRider-Kilo 正式发布!
运维·人工智能·devops
Sunlightʊə6 小时前
2.登录页测试用例
运维·服务器·前端·功能测试·单元测试
利刃大大7 小时前
【高并发服务器:HTTP应用】十六、HttpContext上下文模块 && HttpServer服务器模块&& 服务器测试
运维·服务器·http·高并发·项目
吃饭最爱7 小时前
Elasticsearch的用法
运维·jenkins
emiya_saber8 小时前
Linux 文件系统基本管理
linux·运维·服务器
好记忆不如烂笔头abc8 小时前
Oracle19c rac两节点实例test,在节点1查看监听状态没有test1,但在节点2可以看到test2
运维·服务器