Skip to main content

Map vs FlatMap(Scala)


Map vs FlatMap(Scala)





val data = Seq("his is sample string",
              "one more sample string" ,
              "third line",
              "line",
              "this is one more line")
data: Seq[String] = List(his is sample string, one more sample string, third line, line, this is one more line)
val df = spark.sparkContext.parallelize(data)
df: org.apache.spark.rdd.RDD[String] = ParallelCollectionRDD[7] at parallelize at command-2343346390787948:1
df.map(x => (x , x.length)).collect()
res0: Array[(String, Int)] = Array((his is sample string,20), (one more sample string,22), (third line,10), (line,4), (this is one more line,21))
df.map(x => x.split(" ")).collect()
res1: Array[Array[String]] = Array(Array(his, is, sample, string), Array(one, more, sample, string), Array(third, line), Array(line), Array(this, is, one, more, line))
df.flatMap( x => x.split(" ")).collect()
res2: Array[String] = Array(his, is, sample, string, one, more, sample, string, third, line, line, this, is, one, more, line)

Comments

Popular posts from this blog

Install pip on Windows

If you haven't downloaded get-pip.py yet: You need to download this file from the official pip website or use curl in your command prompt. Using curl (recommended): Open Command Prompt and type: DOS curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py This will download the file to your current directory in the Command Prompt. Manual Download: Go to https://bootstrap.pypa.io/get-pip.py in your web browser, right-click on the page, select "Save as...", and save the file as get-pip.py to a location you can easily navigate to (e.g., your Downloads folder, or a dedicated Python projects folder). The file is in a different directory: You might have downloaded get-pip.py to your Downloads folder, your Desktop, or another location, but you're trying to run the command from C:\Users\gparn . Solution: Navigate to the directory where you saved get-pip.py in your Command Prompt using the cd (change directory) command. Example (if in Downloads): DOS cd C:\User...