您好,登錄后才能下訂單哦!
Connectors如何讀寫csv文件,相信很多沒有經(jīng)驗(yàn)的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。
通過使用Flink DataSet Connectors 數(shù)據(jù)流連接器打開csv文件,并提供數(shù)據(jù)流輸入與輸出操作;
示例環(huán)境
java.version: 1.8.xflink.version: 1.11.1
示例數(shù)據(jù)源 (項(xiàng)目碼云下載)
Flink 系例 之 搭建開發(fā)環(huán)境與數(shù)據(jù)
示例模塊 (pom.xml)
Flink 系例 之 DataStream Connectors 與 示例模塊
數(shù)據(jù)流輸入
CsvSource.java
package com.flink.examples.file; import org.apache.flink.api.java.DataSet; import org.apache.flink.api.java.ExecutionEnvironment; import org.apache.flink.api.java.tuple.Tuple7; import java.util.HashMap; import java.util.Map; /** * @Description 從csv文件中讀取內(nèi)容輸出到DataSet中 */ public class CsvSource { public static void main(String[] args) throws Exception { ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment(); String filePath = "D:\\Workspaces\\idea_2\\flink-examples\\connectors\\src\\main\\resources\\user.csv"; DataSet<Tuple7<Integer, String, Integer, Integer, String, String, Long>> dataSet = env .readCsvFile(filePath) .fieldDelimiter(",") .types(Integer.class, String.class, Integer.class, Integer.class, String.class, String.class, Long.class); //打印 dataSet.print(); } }
數(shù)據(jù)流輸出
CsvSink.java
package com.flink.examples.file; import org.apache.flink.api.java.DataSet; import org.apache.flink.api.java.ExecutionEnvironment; import org.apache.flink.api.java.tuple.Tuple7; import org.apache.flink.core.fs.FileSystem; /** * @Description 將DataSet數(shù)據(jù)寫入到csv文件中 */ public class CsvSink { public static void main(String[] args) throws Exception { ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment(); //需先建立文件 String filePath = "D:\\Workspaces\\idea_2\\flink-examples\\connectors\\src\\main\\resources\\user.csv"; //添加數(shù)據(jù) Tuple7<Integer, String, Integer, Integer, String, String, Long> row = new Tuple7<>(15, "chen1", 40, 1, "CN", "2020-09-08 00:00:00", 1599494400000L); //轉(zhuǎn)換為dataSet DataSet<Tuple7<Integer, String, Integer, Integer, String, String, Long>> dataSet = env.fromElements(row); //將內(nèi)容寫入到File中,如果文件已存在,將會被復(fù)蓋 dataSet.writeAsCsv(filePath, FileSystem.WriteMode.OVERWRITE).setParallelism(1); env.execute("fline file sink"); } }
數(shù)據(jù)展示
看完上述內(nèi)容,你們掌握Connectors如何讀寫csv文件的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(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)容。