value foreach is not a member of java.util.List[String]

在java和scala混编的代码中,有时会遇到java集合和scala结合不兼容的问题,需要导入相应的依赖
 import scala.collection.JavaConverters._
然后通过scala的asScala方法,将java结合转换为scala结合具体代码如下

def main(args: Array[String]): Unit = {


    val env = ExecutionEnvironment.getExecutionEnvironment
    val path = "file:///D:\\idea\\imooc\\flink\\flink-train\\data\\input.txt"
    env.registerCachedFile(path, "scala-dc")

    import org.apache.flink.api.scala._

    val input = env.fromElements("a","b","c")
    input.map(new RichMapFunction[String, String] {

      override def open(parameters: Configuration): Unit = {
        val dcFile = getRuntimeContext.getDistributedCache.getFile("scala-dc")
        val lines = FileUtils.readLines(dcFile)
        import scala.collection.JavaConverters._
        for(ele <- lines.asScala){
          println(ele)
        }


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

 

你可能感兴趣的:(Scala,Flink)