|
| 1 | +package com.javaedge.scala.chapter3 |
| 2 | + |
| 3 | +import com.javaedge.java.chapter3.Person |
| 4 | +import org.apache.flink.api.scala.ExecutionEnvironment |
| 5 | +import org.apache.flink.configuration.Configuration |
| 6 | + |
| 7 | +/** |
| 8 | + * @author JavaEdge |
| 9 | + * @date 2019-07-16 |
| 10 | + */ |
| 11 | +object ScalaDataSetDataSourceApp { |
| 12 | + def main(args: Array[String]): Unit = { |
| 13 | + val env = ExecutionEnvironment.getExecutionEnvironment |
| 14 | + // fromCollection(env) |
| 15 | + // textFile(env) |
| 16 | + // csvFile(env) |
| 17 | + readCompressionFiles(env) |
| 18 | + } |
| 19 | + |
| 20 | + def readCompressionFiles(env:ExecutionEnvironment): Unit ={ |
| 21 | + val filePath = "file:///Volumes/doc/data/compress" |
| 22 | + env.readTextFile(filePath).print() |
| 23 | + } |
| 24 | + |
| 25 | + def readRecursiveFiles(env: ExecutionEnvironment): Unit = { |
| 26 | + // ❌ |
| 27 | + val filePath = "file:///Volumes/doc/data/nested" |
| 28 | + env.readTextFile(filePath).print() |
| 29 | + println("===========~这是一个分割线~============") |
| 30 | + |
| 31 | + // ✔️ |
| 32 | + val parameters = new Configuration |
| 33 | + parameters.setBoolean("recursive.file.enumeration", true) |
| 34 | + env.readTextFile(filePath).withParameters(parameters).print() |
| 35 | + } |
| 36 | + |
| 37 | + def csvFile(env: ExecutionEnvironment): Unit = { |
| 38 | + import org.apache.flink.api.scala._ |
| 39 | + val filePath = "file:///Volumes/doc/data/data.csv" |
| 40 | + // env.readCsvFile[(String, Int, String)](filePath, ignoreFirstLine = true).print() |
| 41 | + // env.readCsvFile[(String, Int)](filePath, ignoreFirstLine = true,includedFields = Array(0, 1)).print() |
| 42 | + |
| 43 | + /*无法正常运行 |
| 44 | + case class MyCaseClass(name: String, age: Int) |
| 45 | + env.readCsvFile[MyCaseClass](filePath,ignoreFirstLine = true,includedFields = Array(0, 1)).print() |
| 46 | + */ |
| 47 | + env.readCsvFile[Person](filePath, ignoreFirstLine = true, pojoFields = Array("name", "age", "job")).print() |
| 48 | + } |
| 49 | + |
| 50 | + def textFile(env: ExecutionEnvironment): Unit = { |
| 51 | + val filePath = "file:///Volumes/doc/data/inputs" |
| 52 | + env.readTextFile(filePath).print() |
| 53 | + } |
| 54 | + |
| 55 | + def fromCollection(env: ExecutionEnvironment): Unit = { |
| 56 | + import org.apache.flink.api.scala._ |
| 57 | + val data = 1 to 10 |
| 58 | + env.fromCollection(data).print() |
| 59 | + } |
| 60 | +} |
0 commit comments