Reputation: 41
Im trying to make a scala program that counts the number of words within a txt file and print the final count (on cloudera and using Spark)
import scala.io.Codec.string2codec
import scala.io.Source
import scala.reflect.io.File
import org.apache.spark.SparkContext
import org.apache.spark.SparkContext._
import org.apache.spark.SparkConf
object SimpleWordCount {
def main(args: Array[String]) {
val conf = new SparkConf().setAppName("Simple Word Count")
val sc = new SparkContext(conf)
The program recognises the file as the correct location as when I put in a false location it recognises it as an error
scala.io.Source.fromFile("/home/cloudera/Books/book1.txt")
.getLines
.flatMap(_.split("\\W+"))
.foldLeft(Map.empty[String, Int]){
(count, word) => count + (word -> (count.getOrElse(word, 0) + 1))
I've tried different ways to print the line here but got errors i.e
System.Out.Println(count)
[error] /home/cloudera/src/main/scala/SimpleWordCount.scala:19:21: type mismatch;
[error] found : Unit
[error] required: scala.collection.immutable.Map[String,Int]
System.out.println(word,count)
type mismatch;
[error] found : Unit
[error] required: scala.collection.immutable.Map[String,Int]
[error] System.out.println(word,count)
}
Added the following line to check if the program was running
System.out.println("This is working over here !!!!!!!!!#$%%E^$%^%%$%#$^%")
}
}
When I run the code it produces the following output
cloudera@quickstart ~]$ spark-submit --master=local[*] --class=SimpleWordCount /home/cloudera/target/scala-2.10/wordcount_2.10-1.0.jar SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/usr/lib/zookeeper/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/usr/lib/flume-ng/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/usr/lib/parquet/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/usr/lib/avro/avro-tools-1.7.6-cdh5.12.0.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation. SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory] 18/12/04 08:13:09 INFO spark.SparkContext: Running Spark version 1.6.0 18/12/04 08:13:10 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable 18/12/04 08:13:10 WARN util.Utils: Your hostname, quickstart.cloudera resolves to a loopback address: 127.0.0.1; using 192.168.182.129 instead (on interface eth3) 18/12/04 08:13:10 WARN util.Utils: Set SPARK_LOCAL_IP if you need to bind to another address 18/12/04 08:13:10 INFO spark.SecurityManager: Changing view acls to: cloudera 18/12/04 08:13:10 INFO spark.SecurityManager: Changing modify acls to: cloudera 18/12/04 08:13:10 INFO spark.SecurityManager: SecurityManager: authentication disabled; ui acls disabled; users with view permissions: Set(cloudera); users with modify permissions: Set(cloudera) 18/12/04 08:13:10 INFO util.Utils: Successfully started service 'sparkDriver' on port 34679. 18/12/04 08:13:11 INFO slf4j.Slf4jLogger: Slf4jLogger started 18/12/04 08:13:11 INFO Remoting: Starting remoting 18/12/04 08:13:11 INFO Remoting: Remoting started; listening on addresses :[akka.tcp://[email protected]:47272] 18/12/04 08:13:11 INFO Remoting: Remoting now listens on addresses: [akka.tcp://[email protected]:47272] 18/12/04 08:13:11 INFO util.Utils: Successfully started service 'sparkDriverActorSystem' on port 47272. 18/12/04 08:13:11 INFO spark.SparkEnv: Registering MapOutputTracker 18/12/04 08:13:11 INFO spark.SparkEnv: Registering BlockManagerMaster 18/12/04 08:13:11 INFO storage.DiskBlockManager: Created local directory at /tmp/blockmgr-1f139fce-3b13-4c07-bed6-7b35f82ccc6a 18/12/04 08:13:11 INFO storage.MemoryStore: MemoryStore started with capacity 530.3 MB 18/12/04 08:13:11 INFO spark.SparkEnv: Registering OutputCommitCoordinator 18/12/04 08:13:11 INFO server.Server: jetty-8.y.z-SNAPSHOT 18/12/04 08:13:11 INFO server.AbstractConnector: Started [email protected]:4040 18/12/04 08:13:11 INFO util.Utils: Successfully started service 'SparkUI' on port 4040. 18/12/04 08:13:11 INFO ui.SparkUI: Started SparkUI at http://192.168.182.129:4040 18/12/04 08:13:11 INFO spark.SparkContext: Added JAR file:/home/cloudera/target/scala-2.10/wordcount_2.10-1.0.jar at spark://192.168.182.129:34679/jars/wordcount_2.10-1.0.jar with timestamp 1543939991769 18/12/04 08:13:11 INFO executor.Executor: Starting executor ID driver on host localhost 18/12/04 08:13:11 INFO util.Utils: Successfully started service 'org.apache.spark.network.netty.NettyBlockTransferService' on port 58495. 18/12/04 08:13:11 INFO netty.NettyBlockTransferService: Server created on 58495 18/12/04 08:13:11 INFO storage.BlockManagerMaster: Trying to register BlockManager 18/12/04 08:13:11 INFO storage.BlockManagerMasterEndpoint: Registering block manager localhost:58495 with 530.3 MB RAM, BlockManagerId(driver, localhost, 58495) 18/12/04 08:13:11 INFO storage.BlockManagerMaster: Registered BlockManager
The PrintLn command seems to be working here
This is working over here !!!!!!!!!#$%%E^$%^%%$%#$^%
18/12/04 08:13:12 INFO spark.SparkContext: Invoking stop() from shutdown hook
18/12/04 08:13:12 INFO handler.ContextHandler: stopped o.s.j.s.ServletContextHandler{/metrics/json,null}
18/12/04 08:13:12 INFO handler.ContextHandler: stopped o.s.j.s.ServletContextHandler{/stages/stage/kill,null}
18/12/04 08:13:12 INFO handler.ContextHandler: stopped o.s.j.s.ServletContextHandler{/api,null}
18/12/04 08:13:12 INFO handler.ContextHandler: stopped o.s.j.s.ServletContextHandler{/,null}
18/12/04 08:13:12 INFO handler.ContextHandler: stopped o.s.j.s.ServletContextHandler{/static,null}
18/12/04 08:13:12 INFO handler.ContextHandler: stopped o.s.j.s.ServletContextHandler{/executors/threadDump/json,null}
18/12/04 08:13:12 INFO handler.ContextHandler: stopped o.s.j.s.ServletContextHandler{/executors/threadDump,null}
18/12/04 08:13:12 INFO handler.ContextHandler: stopped o.s.j.s.ServletContextHandler{/executors/json,null}
18/12/04 08:13:12 INFO handler.ContextHandler: stopped o.s.j.s.ServletContextHandler{/executors,null}
18/12/04 08:13:12 INFO handler.ContextHandler: stopped o.s.j.s.ServletContextHandler{/environment/json,null}
18/12/04 08:13:12 INFO handler.ContextHandler: stopped o.s.j.s.ServletContextHandler{/environment,null}
18/12/04 08:13:12 INFO handler.ContextHandler: stopped o.s.j.s.ServletContextHandler{/storage/rdd/json,null}
18/12/04 08:13:12 INFO handler.ContextHandler: stopped o.s.j.s.ServletContextHandler{/storage/rdd,null}
18/12/04 08:13:12 INFO handler.ContextHandler: stopped o.s.j.s.ServletContextHandler{/storage/json,null}
18/12/04 08:13:12 INFO handler.ContextHandler: stopped o.s.j.s.ServletContextHandler{/storage,null}
18/12/04 08:13:12 INFO handler.ContextHandler: stopped o.s.j.s.ServletContextHandler{/stages/pool/json,null}
18/12/04 08:13:12 INFO handler.ContextHandler: stopped o.s.j.s.ServletContextHandler{/stages/pool,null}
18/12/04 08:13:12 INFO handler.ContextHandler: stopped o.s.j.s.ServletContextHandler{/stages/stage/json,null}
18/12/04 08:13:12 INFO handler.ContextHandler: stopped o.s.j.s.ServletContextHandler{/stages/stage,null}
18/12/04 08:13:12 INFO handler.ContextHandler: stopped o.s.j.s.ServletContextHandler{/stages/json,null}
18/12/04 08:13:12 INFO handler.ContextHandler: stopped o.s.j.s.ServletContextHandler{/stages,null}
18/12/04 08:13:12 INFO handler.ContextHandler: stopped o.s.j.s.ServletContextHandler{/jobs/job/json,null}
18/12/04 08:13:12 INFO handler.ContextHandler: stopped o.s.j.s.ServletContextHandler{/jobs/job,null}
18/12/04 08:13:12 INFO handler.ContextHandler: stopped o.s.j.s.ServletContextHandler{/jobs/json,null}
18/12/04 08:13:12 INFO handler.ContextHandler: stopped o.s.j.s.ServletContextHandler{/jobs,null}
18/12/04 08:13:12 INFO ui.SparkUI: Stopped Spark web UI at http://192.168.182.129:4040
18/12/04 08:13:12 INFO spark.MapOutputTrackerMasterEndpoint: MapOutputTrackerMasterEndpoint stopped!
18/12/04 08:13:12 INFO storage.MemoryStore: MemoryStore cleared
18/12/04 08:13:12 INFO storage.BlockManager: BlockManager stopped
18/12/04 08:13:12 INFO storage.BlockManagerMaster: BlockManagerMaster stopped
18/12/04 08:13:12 INFO scheduler.OutputCommitCoordinator$OutputCommitCoordinatorEndpoint: OutputCommitCoordinator stopped!
18/12/04 08:13:12 INFO spark.SparkContext: Successfully stopped SparkContext
18/12/04 08:13:12 INFO util.ShutdownHookManager: Shutdown hook called
18/12/04 08:13:12 INFO remote.RemoteActorRefProvider$RemotingTerminator: Shutting down remote daemon.
18/12/04 08:13:12 INFO util.ShutdownHookManager: Deleting directory /tmp/spark-e468a57d-10e2-472e-98f2-f3701f6a4b1a
Upvotes: 0
Views: 775
Reputation: 22840
Maybe this can help you?
As I said, the problem is that you can't return a print inside a foldLeft, but you can just add one and then return.
val lines = List(
"Hello, World!",
"Goodbye, World!",
"Hello, Hadoop!"
)
val wordCount =
lines
.flatMap(_.split("\\W+"))
.foldLeft(Map.empty[String, Int]) {
(count, word) =>
println(s"DEBUG count: $count for word: '$word'.")
count + (word -> (count.getOrElse(word, 0) + 1))
}
val formatteWordCount =
wordCount
.map(tuple => s"${tuple._1} -> ${tuple._2}")
.mkString("\n", "\n", "\n")
println(s"Final Word Count: $formatteWordCount")
Output
DEBUG count: Map() for word: 'Hello'.
DEBUG count: Map(Hello -> 1) for word: 'World'.
DEBUG count: Map(Hello -> 1, World -> 1) for word: 'Goodbye'.
DEBUG count: Map(Hello -> 1, World -> 1, Goodbye -> 1) for word: 'World'.
DEBUG count: Map(Hello -> 1, World -> 2, Goodbye -> 1) for word: 'Hello'.
DEBUG count: Map(Hello -> 2, World -> 2, Goodbye -> 1) for word: 'Hadoop'.
Final Word Count:
Hello -> 2
World -> 2
Goodbye -> 1
Hadoop -> 1
Upvotes: 1