一 Flink的基础上手
1 maven依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
| <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <encoding>UTF-8</encoding> <scala.version>2.11.8</scala.version> <scala.compat.version>2.11</scala.compat.version> <hadoop.version>2.6.0</hadoop.version> <flink.version>1.10.0</flink.version> <spark.version>2.2.0</spark.version> </properties>
<dependencies> <dependency> <groupId>org.scala-lang</groupId> <artifactId>scala-library</artifactId> <version>${scala.version}</version> </dependency> <dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-core_2.11</artifactId> <version>${spark.version}</version> </dependency>
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>2.0.0</version> </dependency>
<dependency> <groupId>org.apache.flink</groupId> <artifactId>flink-streaming-scala_2.11</artifactId> <version>${flink.version}</version> </dependency> <dependency> <groupId>org.apache.flink</groupId> <artifactId>flink-scala_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-table-planner_2.11</artifactId> <version>${flink.version}</version> </dependency>
<dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-client</artifactId> <version>${hadoop.version}</version> <exclusions> <exclusion> <artifactId>xml-apis</artifactId> <groupId>xml-apis</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.22</version> </dependency> <dependency> <groupId>org.apache.flink</groupId> <artifactId>flink-connector-kafka-0.9_2.11</artifactId> <version>${flink.version}</version> </dependency> </dependencies>
<build>
<plugins>
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.5.1</version> <configuration> <source>${maven.compiler.source}</source> <target>${maven.compiler.target}</target> </configuration> </plugin>
<plugin> <groupId>net.alchim31.maven</groupId> <artifactId>scala-maven-plugin</artifactId> <version>3.2.0</version> <executions> <execution> <goals> <goal>compile</goal> <goal>testCompile</goal> </goals> <configuration> <args> <arg>-dependencyfile</arg> <arg>${project.build.directory}/.scala_dependencies</arg> </args>
</configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.18.1</version> <configuration> <useFile>false</useFile> <disableXmlReport>true</disableXmlReport> <includes> <include>**/*Test.*</include> <include>**/*Suite.*</include> </includes> </configuration> </plugin>
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.3</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <filters> <filter> <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>com.batch.WordCount</mainClass> </transformer> </transformers> </configuration> </execution>
</executions> </plugin> </plugins> </build>
|
2 批处理的wc
1 2 3 4 5 6 7 8 9 10 11
| def main(args: Array[String]): Unit = { val env: ExecutionEnvironment = ExecutionEnvironment. val ds: DataSet[String] = env.readTextFile("path/wc.txt") val result: AggregateDataSet[(String, Int)] = ds.flatMap(_.split(" ")) .map((_, 1)) .groupBy(0) .sum(1) result.print() }
|
2 流处理的wc
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| def main(args: Array[String]): Unit = { val env = StreamExecutionEnvironment.getExecutionEnvironment val paramTool:ParameterTool = ParameterTool.fromArgs(args) val host:String = paramTool.get("host") val port:String = paramTool.getInt("port") val stream: DataStream[String] = env.socketTextStream(host,port) val result: DataStream[(String, Int)] = stream.flatMap(_.split(" ")).setParallelism(1) .map((_, 1)).setParallelism(1) .keyBy(0) .sum(1).setParallelism(1) result.print() env.execute("Runing") }
|
1 2 3
| 运行结果: 3> (flink,1) 2> (hadoop,2)
|
若是不设置并行度,默认的并行度是当前机器的核数
运行结果的2>,3>代表并行度(当前结果在哪个子任务中执行),默认是按照key做hash计算出来,这样相同的key会在同一个子任务中运行,可以避免各个子任务间拉取数据
1 2 3 4 5 6 7
| 当只在print的时候设置.setParallelism(1) 并行度为1,之前的不进行设置,则输入的数据顺序,和输出的结果数据的顺序可能不一致,因为前面几个算子的默认并行度为核数(我的4) flink---> map1 sum1 hapoop---> map2 sum1 print(并行度1) flume---> map3 sum1 kettle---> map4 sum1 这样处理数据因为网络延迟等原因,有些sum算的快,有些慢,导致进入print的数据是乱序的
|
二 flink的部署
2.1 Standalone模式