您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“flinksql怎么將數(shù)據(jù)寫入到文件中”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“flinksql怎么將數(shù)據(jù)寫入到文件中”吧!
package com.jd.dataoutput; import com.jd.data.SensorReading; import org.apache.flink.api.common.functions.MapFunction; import org.apache.flink.streaming.api.datastream.DataStreamSource; import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator; import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; import org.apache.flink.table.api.DataTypes; import org.apache.flink.table.api.Table; import org.apache.flink.table.api.bridge.java.StreamTableEnvironment; import org.apache.flink.table.descriptors.FileSystem; import org.apache.flink.table.descriptors.OldCsv; import org.apache.flink.table.descriptors.Schema; public class FlinkSqlOutputFile { public static void main(String[] args) throws Exception { StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); env.setParallelism(1); DataStreamSource<String> stream = env.readTextFile("/Users/liuhaijing/Desktop/flinktestword/aaa.txt"); // DataStreamSource<String> stream = env.socketTextStream("localhost", 8888); SingleOutputStreamOperator<SensorReading> map = stream.map(new MapFunction<String, SensorReading>() { public SensorReading map(String s) throws Exception { String[] split = s.split(","); return new SensorReading(split[0], split[1], split[2]); } }); StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env); // 使用 table api Table table = tableEnv.fromDataStream(map); // table.printSchema(); Table select = table.select("a,b"); // select.printSchema(); // 使用 sql api // tableEnv.createTemporaryView("test", map); // Table select = tableEnv.sqlQuery(" select a, b from test"); // select.printSchema(); // DataStream<SensorReading2> sensorReading2DataStream = tableEnv.toAppendStream(select, SensorReading2.class); // sensorReading2DataStream.map(new MapFunction<SensorReading2, Object>() { // @Override // public Object map(SensorReading2 value) throws Exception { // System.out.println(value.a+" "+ value.b); // return null; // } // }); // tableEnv.connect(new FileSystem().path("/Users/liuhaijing/IdeaProjects/haijing3/spark/flinksqldemo/output/out.txt")) // .withFormat(new Csv()) // .withSchema( // new Schema() // .field("a", DataTypes.STRING()) // .field("b", DataTypes.STRING())) // .inAppendMode() // .createTemporaryTable("outputTable"); // select.insertInto("outputTable"); tableEnv.connect(new FileSystem().path("/Users/liuhaijing/IdeaProjects/haijing3/spark/flinksqldemo/output/out.txt")) .withFormat(new OldCsv()) .withSchema(new Schema() .field("a", DataTypes.STRING()) ).inAppendMode() .createTemporaryTable("outputTable"); select.insertInto("outputTable"); env.execute(); } }
到此,相信大家對“flinksql怎么將數(shù)據(jù)寫入到文件中”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。