• 2: Windows安装1.9.3 flink && first demo project


    windows 我试验:就能支持到flink 1.9的版本,后续的版本报错

    1: 下载  flink-1.9.3-bin-scala_2.12.tgz    Index of /dist/flink/flink-1.9.3 (apache.org)

    2: 解压后运行 bin/ start-cluster.bat  http://localhost:8081

            

     3:  下面搞第一个flink 程序

             首先建一个maven工程

    $ mvn archetype:generate \
        -DarchetypeGroupId=org.apache.flink \
        -DarchetypeArtifactId=flink-quickstart-java \
        -DarchetypeVersion=1.9.0 \
        -DgroupId=wiki-edits \
        -DartifactId=wiki-edits \
        -Dversion=0.1 \
        -Dpackage=wikiedits \
        -DinteractiveMode=false
    删掉项目的
    BatchJob。java  和StreamingJob

    新建一个文件:
    WikipediaAnalysis.java
    package wikiedits;
    
    import org.apache.flink.api.common.functions.AggregateFunction;
    import org.apache.flink.api.common.functions.MapFunction;
    import org.apache.flink.api.common.serialization.SimpleStringSchema;
    import org.apache.flink.api.java.functions.KeySelector;
    import org.apache.flink.api.java.tuple.Tuple2;
    import org.apache.flink.streaming.api.datastream.DataStream;
    import org.apache.flink.streaming.api.datastream.KeyedStream;
    import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
    import org.apache.flink.streaming.api.windowing.time.Time;
    import org.apache.flink.streaming.connectors.kafka.FlinkKafkaProducer011;
    import org.apache.flink.streaming.connectors.wikiedits.WikipediaEditEvent;
    import org.apache.flink.streaming.connectors.wikiedits.WikipediaEditsSource;
    
    public class WikipediaAnalysis {
        public static void main(String[] args) throws Exception {
    
            StreamExecutionEnvironment see = StreamExecutionEnvironment.getExecutionEnvironment();
            // is batch job use ExecutionEnvironment
    
            DataStream<WikipediaEditEvent> edits = see.addSource(new WikipediaEditsSource());
    
            KeyedStream<WikipediaEditEvent, String> keyedEdits = edits
                    .keyBy(new KeySelector<WikipediaEditEvent, String>() {
                        @Override
                        public String getKey(WikipediaEditEvent event) {
                            return event.getUser();
                        }
                    });
    
    
            DataStream<Tuple2<String, Long>> result = keyedEdits
                    .timeWindow(Time.seconds(5))
                    .aggregate(new AggregateFunction<WikipediaEditEvent, Tuple2<String, Long>, Tuple2<String, Long>>() {
                        @Override
                        public Tuple2<String, Long> createAccumulator() {
                            return new Tuple2<>("", 0L);
                        }
    
                        @Override
                        public Tuple2<String, Long> add(WikipediaEditEvent value, Tuple2<String, Long> accumulator) {
                            accumulator.f0 = value.getUser();
                            accumulator.f1 += value.getByteDiff();
                            return accumulator;
                        }
    
                        @Override
                        public Tuple2<String, Long> getResult(Tuple2<String, Long> accumulator) {
                            return accumulator;
                        }
    
                        @Override
                        public Tuple2<String, Long> merge(Tuple2<String, Long> a, Tuple2<String, Long> b) {
                            return new Tuple2<>(a.f0, a.f1 + b.f1);
                        }
                    });
    
    
            result
                    .map(new MapFunction<Tuple2<String,Long>, String>() {
                        @Override
                        public String map(Tuple2<String, Long> tuple) {
                            return tuple.toString();
                        }
                    })
                    .addSink(new FlinkKafkaProducer011<>("localhost:9092", "iris", new SimpleStringSchema()));
    
          //  result.print();
    
            see.execute();
    
    
        }
    }

    pom.xml

    <!--
    Licensed to the Apache Software Foundation (ASF) under one
    or more contributor license agreements.  See the NOTICE file
    distributed with this work for additional information
    regarding copyright ownership.  The ASF licenses this file
    to you under the Apache License, Version 2.0 (the
    "License"); you may not use this file except in compliance
    with the License.  You may obtain a copy of the License at
    
      http://www.apache.org/licenses/LICENSE-2.0
    
    Unless required by applicable law or agreed to in writing,
    software distributed under the License is distributed on an
    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    KIND, either express or implied.  See the License for the
    specific language governing permissions and limitations
    under the License.
    -->
    <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>wiki-edits</groupId>
        <artifactId>wiki-edits</artifactId>
        <version>0.1</version>
        <packaging>jar</packaging>
    
        <name>Flink Quickstart Job</name>
        <url>http://www.myorganization.org</url>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <flink.version>1.9.0</flink.version>
            <java.version>1.8</java.version>
            <scala.binary.version>2.11</scala.binary.version>
            <maven.compiler.source>${java.version}</maven.compiler.source>
            <maven.compiler.target>${java.version}</maven.compiler.target>
        </properties>
    
        <repositories>
            <repository>
                <id>apache.snapshots</id>
                <name>Apache Development Snapshot Repository</name>
                <url>https://repository.apache.org/content/repositories/snapshots/</url>
                <releases>
                    <enabled>false</enabled>
                </releases>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </repository>
        </repositories>
    
        <dependencies>
            <dependency>
                <groupId>org.apache.flink</groupId>
                <artifactId>flink-java</artifactId>
                <version>${flink.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.flink</groupId>
                <artifactId>flink-streaming-java_2.11</artifactId>
                <version>${flink.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.flink</groupId>
                <artifactId>flink-clients_2.11</artifactId>
                <version>${flink.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.flink</groupId>
                <artifactId>flink-connector-wikiedits_2.11</artifactId>
                <version>${flink.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.flink</groupId>
                <artifactId>flink-connector-kafka-0.11_2.11</artifactId>
                <version>${flink.version}</version>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
    
                <!-- Java Compiler -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.1</version>
                    <configuration>
                        <source>${java.version}</source>
                        <target>${java.version}</target>
                    </configuration>
                </plugin>
    
                <!-- We use the maven-shade plugin to create a fat jar that contains all necessary dependencies. -->
                <!-- Change the value of <mainClass>...</mainClass> if your program entry point changes. -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                    <version>3.0.0</version>
                    <executions>
                        <!-- Run shade goal on package phase -->
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>shade</goal>
                            </goals>
                            <configuration>
                                <artifactSet>
                                    <excludes>
                                        <exclude>org.apache.flink:force-shading</exclude>
                                        <exclude>com.google.code.findbugs:jsr305</exclude>
                                        <exclude>org.slf4j:*</exclude>
                                        <exclude>log4j:*</exclude>
                                    </excludes>
                                </artifactSet>
                                <filters>
                                    <filter>
                                        <!-- Do not copy the signatures in the META-INF folder.
                                        Otherwise, this might cause SecurityExceptions when using the JAR. -->
                                        <artifact>*:*</artifact>
                                        <excludes>
                                            <exclude>META-INF/*.SF</exclude>
                                            <exclude>META-INF/*.DSA</exclude>
                                            <exclude>META-INF/*.RSA</exclude>
                                        </excludes>
                                    </filter>
                                </filters>
                                <transformers>
                                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                        <mainClass>wikiedits.WikipediaAnalysis</mainClass>
                                    </transformer>
                                </transformers>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
    
            <pluginManagement>
                <plugins>
    
                    <!-- This improves the out-of-the-box experience in Eclipse by resolving some warnings. -->
                    <plugin>
                        <groupId>org.eclipse.m2e</groupId>
                        <artifactId>lifecycle-mapping</artifactId>
                        <version>1.0.0</version>
                        <configuration>
                            <lifecycleMappingMetadata>
                                <pluginExecutions>
                                    <pluginExecution>
                                        <pluginExecutionFilter>
                                            <groupId>org.apache.maven.plugins</groupId>
                                            <artifactId>maven-shade-plugin</artifactId>
                                            <versionRange>[3.0.0,)</versionRange>
                                            <goals>
                                                <goal>shade</goal>
                                            </goals>
                                        </pluginExecutionFilter>
                                        <action>
                                            <ignore/>
                                        </action>
                                    </pluginExecution>
                                    <pluginExecution>
                                        <pluginExecutionFilter>
                                            <groupId>org.apache.maven.plugins</groupId>
                                            <artifactId>maven-compiler-plugin</artifactId>
                                            <versionRange>[3.1,)</versionRange>
                                            <goals>
                                                <goal>testCompile</goal>
                                                <goal>compile</goal>
                                            </goals>
                                        </pluginExecutionFilter>
                                        <action>
                                            <ignore/>
                                        </action>
                                    </pluginExecution>
                                </pluginExecutions>
                            </lifecycleMappingMetadata>
                        </configuration>
                    </plugin>
                </plugins>
            </pluginManagement>
        </build>
    
        <!-- This profile helps to make things run out of the box in IntelliJ -->
        <!-- Its adds Flink's core classes to the runtime class path. -->
        <!-- Otherwise they are missing in IntelliJ, because the dependency is 'provided' -->
        <profiles>
            <profile>
                <id>add-dependencies-for-IDEA</id>
    
                <activation>
                    <property>
                        <name>idea.version</name>
                    </property>
                </activation>
    
                <dependencies>
                    <dependency>
                        <groupId>org.apache.flink</groupId>
                        <artifactId>flink-java</artifactId>
                        <version>${flink.version}</version>
                        <scope>compile</scope>
                    </dependency>
                    <dependency>
                        <groupId>org.apache.flink</groupId>
                        <artifactId>flink-streaming-java_${scala.binary.version}</artifactId>
                        <version>${flink.version}</version>
                        <scope>compile</scope>
                    </dependency>
                </dependencies>
            </profile>
        </profiles>
    
    </project>

    4: 注意上面有如下代码:把最后结果输出到kafka,如果不想安装kafak, 就直接用result.print() 输出来替换下面的代码

       result
                    .map(new MapFunction<Tuple2<String,Long>, String>() {
                        @Override
                        public String map(Tuple2<String, Long> tuple) {
                            return tuple.toString();
                        }
                    })
                    .addSink(new FlinkKafkaProducer011<>("localhost:9092", "iris", new SimpleStringSchema()));
    5: 安装kafak, 如果上面第四步用result.print() 而不是输出到kafak,就跳过
    下载 kafka_2.11-0.11.0.2
    解压后:
    启动zookeeper服务,运行命令: bin\windows\zookeeper-server-start.bat config\zookeeper.properties

                                        启动kafka服务,运行命令:     bin\windows\kafka-server-start.bat config\server.properties

                            Kafka中创建一个Topic,名称为iris

                      bin\windows\kafka-topics.bat --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic iris
    然后,就可以看使用如下命令,读取(消费)topic iris中的数据
                       bin\windows\kafka-console-consumer.bat --bootstrap-server 127.0.0.1:9092 --topic iris --from-beginning
    6: 运行job

    $ mvn clean package
    $ mvn exec:java -Dexec.mainClass=wikiedits.WikipediaAnalysis

    7:  然后就可以看到处理完数据打印出来  或 是存到kafak

             

     8: 可以看job的执行状态    http://localhost:8081/#/job/running



  • 相关阅读:
    linux安装JRE和Tomcat
    微信公众号授权登录
    linux Nginx设置多级域名
    bootstrap制作收藏夹导航
    js下拉菜单
    QQ授权登录
    centos7.6下安装LNMP环境(linux+nginx+mysql5.7+PHP)
    typora快捷键(转载)
    旋转魔方(2)-添加照片
    test
  • 原文地址:https://www.cnblogs.com/liufei1983/p/15693768.html
Copyright © 2020-2023  润新知