You are currently viewing How to run shell command in Scala from the code level – check great code snippet in 1 minute?
Could You Please Share This Post? I Appreciate It And Thank YOU! :) Have A Nice Day!

What Is Scala?

Scala is a high-level programming language that mixes object-oriented and functional programming. Scala’s static types help complicated applications avoid problems, and its JVM and JavaScript runtimes allow you to construct high-performance systems with simple access to a vast library ecosystem.

Scala is a multi-paradigm programming language that was created to describe common programming patterns in a short, expressive, and type-safe manner. It combines the best of object-oriented and functional programming languages.

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 (!).

Scala Run Shell Command

The following code shows how to use the ! character run shell command from Scala:

# 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.

Could You Please Share This Post? 
I appreciate It And Thank YOU! :)
Have A Nice Day!


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.

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?