How to run shell command in Scala from the code level – check great code snippet in 1 minute?

You are currently viewing How to run shell command in Scala from the code level – check great code snippet in 1 minute?
Share This Post, Help Others, And Earn My Heartfelt Appreciation! :)
4.9
(799)

How to run shell command in Scala

[ How to run shell command in Scala ] To execute the shell command, you must import the sys.process library and then use the available Domain Specific Language (DSL) defined with an exclamation mark (!). Please use the following code:

# Import library
import scala.sys.process._

# Run shell command to create directory in HDFS
val targetPath = "/bigdataetl/data"
s"hdfs dfs -mkdir -p ${targetPath}" !

Apache Spark

Using Apache Spark, you may find that you will also need to perform some operation on files or directories. You can also use the above library as much as possible.

# Delete file from HDFS
s"hdfs dfs -rm /bigdata_etl/temp_file.dat" !

As you can see, thanks to the fact that a method in this programming language can generally be any character, the way we use these functions is very clear and easy to use.

If you enjoyed this post please add the comment below and share this post on your Facebook, Twitter, LinkedIn or another social media webpage.
Thanks in advanced!

How useful was this post?

Click on a star to rate it!

Average rating 4.9 / 5. Vote count: 799

No votes so far! Be the first to rate this post.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments