-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dd36eae
commit b7ccc65
Showing
2 changed files
with
77 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/main/java/org/myorg/quickstart/CoreConcepts07/BatchJob.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
|
||
package org.myorg.quickstart.CoreConcepts07; | ||
|
||
import org.apache.commons.io.FileUtils; | ||
import org.apache.flink.api.common.functions.RichMapFunction; | ||
import org.apache.flink.api.common.restartstrategy.RestartStrategies; | ||
import org.apache.flink.api.common.time.Time; | ||
import org.apache.flink.api.java.DataSet; | ||
import org.apache.flink.api.java.ExecutionEnvironment; | ||
import org.apache.flink.api.java.operators.DataSource; | ||
import org.apache.flink.configuration.Configuration; | ||
import org.apache.flink.configuration.RestartStrategyOptions; | ||
import org.apache.flink.runtime.executiongraph.restart.RestartStrategy; | ||
|
||
import java.io.File; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public class BatchJob { | ||
|
||
|
||
public static void main(String[] args) throws Exception { | ||
|
||
// set up the execution environment | ||
final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment(); | ||
// env.setRestartStrategy(RestartStrategies.noRestart()); | ||
// env.setRestartStrategy(RestartStrategies.fixedDelayRestart( | ||
// 3, // 尝试重启的次数 | ||
// Time.of(10, TimeUnit.SECONDS) // 延时 | ||
// )); | ||
|
||
env.setRestartStrategy(RestartStrategies.failureRateRestart( | ||
3, // 每个时间间隔的最大故障次数 | ||
Time.of(5, TimeUnit.MINUTES), // 测量故障率的时间间隔 | ||
Time.of(5, TimeUnit.SECONDS) // 延时 | ||
)); | ||
|
||
|
||
|
||
|
||
|
||
|
||
env.registerCachedFile("/Users/wangchangye/WorkSpace/quickstart/distributedcache.txt", "distributedCache"); | ||
//1:注册一个文件,可以使用hdfs上的文件 也可以是本地文件进行测试 | ||
DataSource<String> data = env.fromElements("Linea", "Lineb", "Linec", "Lined"); | ||
|
||
DataSet<String> result = data.map(new RichMapFunction<String, String>() { | ||
private ArrayList<String> dataList = new ArrayList<String>(); | ||
|
||
@Override | ||
public void open(Configuration parameters) throws Exception { | ||
super.open(parameters); | ||
//2:使用文件 | ||
File myFile = getRuntimeContext().getDistributedCache().getFile("distributedCache"); | ||
List<String> lines = FileUtils.readLines(myFile); | ||
for (String line : lines) { | ||
this.dataList.add(line); | ||
System.err.println("分布式缓存为:" + line); | ||
} | ||
} | ||
|
||
@Override | ||
public String map(String value) throws Exception { | ||
//在这里就可以使用dataList | ||
System.err.println("使用datalist:" + dataList + "------------" +value); | ||
//业务逻辑 | ||
return dataList +":" + value; | ||
} | ||
}); | ||
|
||
result.printToErr(); | ||
} | ||
} | ||
|