分布式缓存

object DistributeCacheApp {
  def main(args: Array[String]): Unit = {
    val env = ExecutionEnvironment.getExecutionEnvironment
    val filePath = "D:\\a.txt"
    env.registerCachedFile(filePath, "hello-cache")
    val data = env.fromElements("hadoop", "ok", "java", "flink")

    data.map(new RichMapFunction[String, String] {

      override def open(parameters: Configuration): Unit = {
        val cacheFile = getRuntimeContext.getDistributedCache.getFile("hello-cache")
        val lines = FileUtils.readLines(cacheFile)

        import scala.collection.JavaConverters._
        for (ele <- lines.asScala) {
          println(ele)
        }
      }

      override def map(in: String): String ={
         in
      }
    }).print()
  }
}

 

posted on 2021-10-30 19:40  溪水静幽  阅读(22)  评论(0)    收藏  举报