Flink运行wordcount——读写hdfs

java 复制代码
package com.test;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

public class ReadHDFS {
    public static void main(String[] args) {
        final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        DataStream<String> text = env.readTextFile("hdfs://10.9.13.171:54310/testdir/abc.txt"); // your source here

        DataStream<Tuple2<String, Integer>> wordCounts = text.flatMap(new FlatMapFunction<String, Tuple2<String, Integer>>() {
            @Override
            public void flatMap(String value, Collector<Tuple2<String, Integer>> out) throws Exception {
                String[] words = value.toLowerCase().split("\\s+");
                for (String word : words) {
                    if (!word.isEmpty()) {
                        out.collect(new Tuple2<>(word, 1));
                    }
                }
            }
        });

        wordCounts.print();
        wordCounts.writeAsText("hdfs://10.9.13.171:54310/testdir/wordcountoutput");

        try {
            env.execute("WordCount Job");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

pom.xml文件

java 复制代码
<?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>flink-test</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>
        <flink.version>1.14.5</flink.version>
        <hadoop.version>3.1.2</hadoop.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.flink</groupId>
            <artifactId>flink-java</artifactId>
            <version>${flink.version}</version>
        </dependency>
        <!-- 其他依赖 -->
        <dependency>
            <groupId>org.apache.flink</groupId>
            <artifactId>flink-core</artifactId>
            <version>${flink.version}</version> <!-- 用您实际使用的Flink版本号替换 ${flink.version} -->
        </dependency>
        <dependency>
            <groupId>org.apache.flink</groupId>
            <artifactId>flink-streaming-java_2.12</artifactId>
            <version>${flink.version}</version> <!-- 用您实际使用的Flink版本号替换 ${flink.version} -->
        </dependency>
        <dependency>
            <groupId>org.apache.flink</groupId>
            <artifactId>flink-clients_2.12</artifactId>
            <version>${flink.version}</version> <!-- 用您实际使用的Flink版本号替换 ${flink.version} -->
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>${hadoop.version}</version> <!-- 用您实际使用的Hadoop版本号替换 ${hadoop.version} -->
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-hdfs</artifactId>
            <version>${hadoop.version}</version> <!-- 用您实际使用的Hadoop版本号替换 ${hadoop.version} -->
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <!-- 配置将依赖包一并打入到项目的 jar 包中 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.6.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.test.ReadHDFS</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id> <!-- this is used for inheritance merges -->
                        <phase>package</phase> <!-- 指定在打包节点执行jar包合并操作 -->
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

打包后提交到yarn集群命令

java 复制代码
[root@node171 lib]# flink run -m yarn-cluster  flink-test-1.0-SNAPSHOT-jar-with-dependencies.jar 

报错

相关推荐
千里码aicood1 小时前
计算机大数据、人工智能与智能系统开发定制开发
大数据·人工智能·深度学习·决策树·机器学习·森林树
非著名架构师3 小时前
城市通风廊道的科学依据:气候大数据如何指导未来城市规划设计
大数据·风光功率预测·高精度气象数据
IIIIIILLLLLLLLLLLLL3 小时前
Hadoop集群时间同步方法
大数据·hadoop·分布式
搞科研的小刘选手3 小时前
【经管专题会议】第五届大数据经济与数字化管理国际学术会议(BDEDM 2026)
大数据·区块链·学术会议·数据化管理·经济理论
蓝耘智算4 小时前
GPU算力租赁与算力云平台选型指南:从需求匹配到成本优化的实战思路
大数据·人工智能·ai·gpu算力·蓝耘
liliangcsdn4 小时前
如何用bootstrap模拟估计pass@k
大数据·人工智能·bootstrap
DMD1684 小时前
AI赋能旅游与酒店业:技术逻辑与开发实践解析
大数据·人工智能·信息可视化·重构·旅游·产业升级
Elastic 中国社区官方博客5 小时前
Elasticsearch 中使用 NVIDIA cuVS 实现最高快 12 倍的向量索引速度:GPU 加速第 2 章
大数据·人工智能·elasticsearch·搜索引擎·ai·全文检索·数据库架构
jqpwxt5 小时前
启点智慧景区多商户分账系统,多业态景区收银管理系统
大数据·旅游
jkyy20146 小时前
线上线下融合、跨场景协同—社区健康医疗小屋的智能升级
大数据·人工智能·物联网·健康医疗