首先在flink的bin目录下,打开终端,输入 start-cluster.sh
如果在http://localhost:8081上显示:Total Task Slots 0 Task Managers 0
则在终端要重新启动:
taskmanager.sh start
jobmanager.sh start
在终端提交job:/usr/local/flink-1.9.1/bin/flink run -c quickstart.WordCount ./flink_quickstart.jar --input ./a.txt --output ./b.txt(包括指定输入文件和输出文件)
该项目中的类有:
其中Tokenizer 类中的flatMap()方法是我重载的方法,里面可以替换成你想进行的算法,最终把结果数据交给out.collect。
package quickstart;
import com.sun.deploy.util.SyncAccess;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.DataSet;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.api.java.utils.ParameterTool;
import org.apache.flink.util.Collector;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class WordCount {
public WordCount() {
}
public static void main(String[] args) throws Exception {
ParameterTool params = ParameterTool.fromArgs(args);
ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
env.getConfig().setGlobalJobParameters(params);
Object text;
if (params.has("input")) {
text = env.readTextFile(params.get("input"));//此时的文件应该就是存储的每个img的路径,并以“,”隔开
} else {
System.out.println("Executing WordCount example with default input data set.");
System.out.println("Use --input to specify file input.");
text = WordCountData.getDefaultTextLineDataSet(env);
}
DataSet> counts = ((DataSet)text).flatMap(new WordCount.Tokenizer());
if (params.has("output")) {
counts.writeAsCsv(params.get("output"), "\n", " ");
env.execute("WordCount Example");
} else {
System.out.println("Printing result to stdout. Use --output to specify output path.");
counts.print();
}
}
public static final class Tokenizer implements FlatMapFunction> {
public Tokenizer() {
}
public void flatMap(String value, Collector> out) {
//获取每个图片的二进制
String[] tokens = value.split(",");
String[] var4 = tokens;
int var5 = tokens.length;
out.collect(new Tuple2("flink process begin!", ""));
//遍历每个二进制
for(int var6 = 0; var6 < var5; ++var6) {
String token = var4[var6];//得到的是图片的二进制
System.out.println(token);
//接下来调用ocr算法
//调用demo.py
Process proc;
try {
//需传入的参数
String readPath = token;//读取img的位置
//设置命令行传入参数
String[] args1 = new String[] { "python", "C:\\Users\\Administrator\\Desktop\\tmp\\testPy.py", readPath};
proc = Runtime.getRuntime().exec(args1);// 执行py文件
//用输入输出流来截取结果
BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
out.collect(new Tuple2(line, ""));
}
in.close();
proc.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
out.collect(new Tuple2("flink process finished!", ""));
}
}
}
另外一个类,是当输入文件为空时,指定输入内容
package quickstart;
import org.apache.flink.api.java.DataSet;
import org.apache.flink.api.java.ExecutionEnvironment;
public class WordCountData {
//public static final String[] WORDS = new String[]{"To be, or not to be,--that is the question:--", "Whether 'tis nobler in the mind to suffer", "The slings and arrows of outrageous fortune", "Or to take arms against a sea of troubles,", "And by opposing end them?--To die,--to sleep,--", "No more; and by a sleep to say we end", "The heartache, and the thousand natural shocks", "That flesh is heir to,--'tis a consummation", "Devoutly to be wish'd. To die,--to sleep;--", "To sleep! perchance to dream:--ay, there's the rub;", "For in that sleep of death what dreams may come,", "When we have shuffled off this mortal coil,", "Must give us pause: there's the respect", "That makes calamity of so long life;", "For who would bear the whips and scorns of time,", "The oppressor's wrong, the proud man's contumely,", "The pangs of despis'd love, the law's delay,", "The insolence of office, and the spurns", "That patient merit of the unworthy takes,", "When he himself might his quietus make", "With a bare bodkin? who would these fardels bear,", "To grunt and sweat under a weary life,", "But that the dread of something after death,--", "The undiscover'd country, from whose bourn", "No traveller returns,--puzzles the will,", "And makes us rather bear those ills we have", "Than fly to others that we know not of?", "Thus conscience does make cowards of us all;", "And thus the native hue of resolution", "Is sicklied o'er with the pale cast of thought;", "And enterprises of great pith and moment,", "With this regard, their currents turn awry,", "And lose the name of action.--Soft you now!", "The fair Ophelia!--Nymph, in thy orisons", "Be all my sins remember'd."};
public static final String WORDS = "http://192.168.142.136/001.jpg,http://192.168.142.136/002.jpg,http://192.168.142.136/003.jpg,http://192.168.142.136/004.jpg,http://192.168.142.136/005.jpg,http://192.168.142.136/demo.jpg";
public WordCountData() {
}
public static DataSet getDefaultTextLineDataSet(ExecutionEnvironment env) {
return env.fromElements(WORDS);
}
}