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
相关推荐
暮色驶过苍茫2 小时前
VSCode 配置 SSH 远程连接
ide·vscode·ssh
从零开始的ops生活2 小时前
【Day 80】Linux-NAS 和 SAN 存储
linux·运维·php
Wang's Blog3 小时前
Linux小课堂: 输入重定向与管道操作详解
linux·运维·服务器
python百炼成钢5 小时前
3.Linux 网络相关
linux·运维·网络·stm32·单片机
Jtti5 小时前
香港硬防服务器防御DDOS攻击的优点
运维·服务器·ddos
lpfasd1237 小时前
第2部分:Netty核心架构与原理解析
运维·服务器·架构
小蜜蜂爱编程7 小时前
gerrit的部署与配置关联到不同服务器上的git仓库
运维·服务器·git·gerrit
心灵宝贝8 小时前
申威(sw_64)架构下如何安装java-1.8.0-swjdk的rpm包?
linux·运维·服务器
半梦半醒*9 小时前
搭建Jenkins
linux·运维·centos·tomcat·jenkins·运维开发