pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>sample-project</artifactId>
<groupId>org.myproject.bigdata</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>JavaFlink-112</artifactId>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<flink.version>1.12.2</flink.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<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-streaming-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.flink</groupId>
<artifactId>flink-table-planner-blink_2.11</artifactId>
<version>${flink.version}</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-table-common</artifactId>
<version>${flink.version}</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-table-api-java-bridge_2.11</artifactId>
<version>${flink.version}</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-optimizer_2.11</artifactId>
<version>${flink.version}</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-runtime-web_2.11</artifactId>
<version>${flink.version}</version>
</dependency>
<!-- Connector -->
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-connector-kafka_2.11</artifactId>
<version>${flink.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.2</version>
</dependency>
<!-- json -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
样例代码
package org.myproject.bigdata.sample;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import org.aishuang.bigdata.udf.ToJsonData;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.typeinfo.BasicTypeInfo;
import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.api.java.typeutils.RowTypeInfo;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.streaming.api.functions.source.RichSourceFunction;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.table.functions.ScalarFunction;
import org.apache.flink.types.Row;
import org.apache.flink.util.Collector;
public class FlinkTest {
private static TypeInformation[] typeInformations = {
BasicTypeInfo.STRING_TYPE_INFO,
BasicTypeInfo.STRING_TYPE_INFO,
BasicTypeInfo.STRING_TYPE_INFO,
BasicTypeInfo.STRING_TYPE_INFO,
BasicTypeInfo.STRING_TYPE_INFO,
BasicTypeInfo.STRING_TYPE_INFO,
BasicTypeInfo.STRING_TYPE_INFO,
BasicTypeInfo.STRING_TYPE_INFO,
BasicTypeInfo.STRING_TYPE_INFO,
BasicTypeInfo.STRING_TYPE_INFO
};
private static String[] columnNames = {
"n1", "n2", "n3", "n4", "n5", "n6", "n7", "n8", "n9", "n10"
};
public static void main(String[] args) throws Exception {
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env);
DataStream<Row> mapStream = env.addSource(new CustomSource())
.map((MapFunction<String, Row>) line -> {
String[] split = line.split(",");
Row row = new Row(split.length);
for (int i = 0; i < split.length; i++) {
row.setField(i, split[i]);
}
return row;
}).returns(new RowTypeInfo(typeInformations, columnNames));
tableEnv.createTemporaryFunction("tojson", ToJsonData.class);
Table table = tableEnv.fromDataStream(mapStream);
tableEnv.registerTable("tmp_table", table);
Table rsTable = tableEnv.sqlQuery("select '' as c1, '' as c2, tojson('k1,k2,k3', n1, n2, n3) as json_data from tmp_table where n1='A'");
DataStream<Row> rowDataStream = tableEnv.toAppendStream(rsTable, Row.class);
SingleOutputStreamOperator<JavaBean> process = rowDataStream.process(new ConvertJavaBean());
// rowDataStream.printToErr();
process.printToErr();
env.execute();
}
private static class ConvertJavaBean extends ProcessFunction<Row, JavaBean> {
@Override
public void processElement(Row row, Context ctx, Collector<JavaBean> out) throws Exception {
JavaBean javaBean = new JavaBean();
javaBean.setData(JSONObject.parseObject(String.valueOf(row.getField(2))));
out.collect(javaBean);
}
}
@Data
private static class JavaBean {
private JSONObject data;
}
private static class CustomSource extends RichSourceFunction<String> {
private boolean flag = true;
@Override
public void run(SourceContext<String> ctx) throws Exception {
while (flag) {
ctx.collect("A,B,C,D,E,F,G,H,J,10");
ctx.collect("X,B,C,D,E,F,G,H,J,10");
Thread.sleep(10000);
}
}
@Override
public void cancel() {
flag = false;
}
}
private class toJson extends ScalarFunction {
public String eval(String... a) {
if (a == null || a.length <= 1) {
return "";
}
JSONObject json = new JSONObject();
String[] columns = a[0].split(",");
for (int i = 0; i < columns.length; i++) {
json.put(columns[i], a[i + 1]);
}
return json.toString();
}
}
// DataStreamSource<String> source = env.fromElements(
// "A,B,C,D,E,F,G,H,J,10",
// "A,B,C,D,E,F,G,H,J,10",
// "A,B,C,D,E,F,G,H,J,10"
// );
}
标签:Flink,1.12,flink,样例,BasicTypeInfo,api,import,apache,org
From: https://www.cnblogs.com/zi-shuo/p/17660371.html