windows本地开发flink的maven文件以及简单示例代码

pom包:



    4.0.0

    com.test.flink
    testflink
    1.0
    
    
        1.8
        1.8
        1.8
    

    
        
            org.scala-lang
            scala-library
            2.11.8
            compile
        

        
            org.slf4j
            slf4j-log4j12
            1.7.7
            compile
        

        
            log4j
            log4j
            1.2.17
            compile
        
        
        
            org.apache.flink
            flink-shaded-hadoop2
            1.7.2
            compile
        
        
        
            org.apache.flink
            flink-hadoop-compatibility_2.11
            1.7.2
            compile
        
        
        
            org.apache.flink
            flink-scala_2.11
            1.7.2
            compile
        
        
        
            org.apache.flink
            flink-streaming-scala_2.11
            1.7.2
            compile
        
        
        
            org.apache.flink
            flink-java
            1.7.2
            compile
        
        
        
            org.apache.flink
            flink-streaming-java_2.11
            1.7.2
            compile
        
        
        
            org.apache.flink
            flink-connector-kafka-0.10_2.11
            1.7.2
            compile
        
        
        
            org.apache.flink
            flink-statebackend-rocksdb_2.11
            1.7.2
        
        
        
            org.apache.flink
            flink-hbase_2.11
            1.7.2
        
        
        
            org.apache.flink
            flink-runtime-web_2.11
            1.7.2
            compile
        
        
        
            mysql
            mysql-connector-java
            5.1.35
            compile
        
    

log4j的设置:

log4j.rootLogger=WARN, console, file
log4j.logger.akka=WARN, console
log4j.logger.org.apache.kafka=WARN
log4j.logger.org.apache.hadoop=WARN
log4j.logger.org.apache.zookeeper=WARN

log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yy/MM/dd HH:mm:ss} %p %c{1}: %m%n
# Log all infos in the given file
log4j.appender.file=org.apache.log4j.FileAppender
# 注意这里要修改成自己的日志目录
log4j.appender.file.file=/tmp/flink_log
log4j.appender.file.append=false
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p %-60c %x - %m%n

flink的开发流程

windows本地开发flink的maven文件以及简单示例代码_第1张图片

package operator

import org.apache.flink.api.common.functions.FlatMapFunction
import org.apache.flink.configuration.{ConfigConstants, Configuration}
import org.apache.flink.streaming.api.scala.{DataStream, StreamExecutionEnvironment}
import org.apache.flink.util.Collector

object SocketWordCount {
  def main(args: Array[String]): Unit = {
    //生成配置对象
    val config = new Configuration()
    //开启flink-webui
    config.setBoolean(ConfigConstants.LOCAL_START_WEBSERVER,true)
    //配置webui的日志文件,否则打印日志到控制台
    config.setString("web.log.path","/tmp/flink_log")
    //配置taskManager的日志文件,否则打印日志到控制台
    config.setString(ConfigConstants.TASK_MANAGER_LOG_PATH_KEY,"/tmp/flink_log")

    //获取local运行环境
    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(config)

    //定义socket源
    val text: DataStream[String] = env.socketTextStream("localhost",6666)

    //scala开发需要加一行隐式转换,否则在调用operator的时候会报错
    import org.apache.flink.api.scala._
    
    //定义operators,作用是解析数据,分组,并且求wordCount
//    val wordCount: DataStream[(String, Int)] = text.flatMap(_.split(" ")).map((_,1)).keyBy(_._1).sum(1)

    //使用FlatMapFunction自定义函数来完成flatMap和map的组合功能
    val wordCount: DataStream[(String, Int)] = text.flatMap(new FlatMapFunction[String, (String, Int)] {
      override def flatMap(value: String, out: Collector[(String, Int)]) = {
        val strings: Array[String] = value.split(" ")
        for (s <- strings) {
          out.collect((s, 1))
        }
      }
    }).keyBy(0).sum(1)

    //定义sink打印到控制台
    wordCount.print()

    //定义任务的名称并运行
    //注意:operator是惰性的,只有遇到execute才执行
    env.execute("SocketWordCount")
  }
}

 

你可能感兴趣的:(flink)