From the Scala REPL I am running a program thus:
:load foo.scala
I want to pass foo a parameter so I try:
:load foo.scala 1.0
which gives:
usage: :load -v file
Is there any way of running a program from the REPL with parameters like this?
If it is a single source file, I would recommend using the :paste command with which you can copy the whole source file into the REPL and then do a ctrl + c to exit the paste mode.
You could then call your program from the REPL by passing in the parameters that you need!
Related
I am studying Scala and I am using Scala for win 10
I have write a script "helloarg.scala" with 1 line println("Hello, " + args(0) + "!")
When i used cmd to run file (not in scala shell), it worked.
C:\Users\Darkntnt>scala D:\Scala\helloarg.scala planet
Hello, planet!
However, It got the error when i load file from scala shell
Error:
scala> :load D:\Scala\helloarg.scala planet
usage: :load -v file
Please help me to fix this problem.
Thank you.
That's a good one.
:load doesn't respect args; there are a few different idioms for "run an app as a script".
I'll add this example to the issue about unifying them.
As a workaround, maybe val args = Array("planet") or similar.
I'm exploring to use Scala REPL bridge to shell.It is mainly achieved by import sys.process._ package and I can use "ls" ! to execute shell.
Now, I want to use "vi" ! to open a interactive vi editor, it is really crazy but very exciting. After entering the cmd in REPL, the terminal opens a init vi canvas. Unfortunately, the terminal is not reading any input from my keyboard.
Is it possible to open a vi in REPL?
from https://stackoverflow.com/a/29972867/1573825 (java solution):
import java.lang.{Process, ProcessBuilder}
System.out.println("STARTING VI");
val processBuilder = new ProcessBuilder("/usr/bin/vi")
processBuilder.redirectOutput(ProcessBuilder.Redirect.INHERIT)
processBuilder.redirectError(ProcessBuilder.Redirect.INHERIT)
processBuilder.redirectInput(ProcessBuilder.Redirect.INHERIT)
val p = processBuilder.start()
// wait for termination.
p.waitFor()
System.out.println("Exiting VI")
it doesn't even corrupt readline.
I've been trying to find some sort of a dotfile to put Scala REPL settings and custom function in.
In particular I'm interested in passing it flags like -Dscala.color (enables syntax highlighting), as well as overriding settings like result string truncation:
scala> :power
scala> vals.isettings.maxPrintString = 10000
It would be nice to have these settings apply to both the simple Scala REPL sessions as well as sbt console sessions.
Does such a central configuration place exist for Scala?
Maybe you can use a modernized Scala REPL:
https://lihaoyi.github.io/Ammonite/
Poor man's solution: Set yourself an alias
alias myScala='scala -Dscala.repl.axPrintString = 10000'
As mentioned here ~/.sbt/0.13/global.sbt is the global configuration file for sbt. You can change your global settings here, this probably not going to effect REPL but should do work with SBT Console
You mainly asked about property settings, this goes a little beyond that to consider loading a definitions file as well—and isn't much help for Windows—but I thought I'd share in case it's useful:
I've resorted to using a wrapper script saved as ~/bin/scala, to set config properties and load some utility functions:
#!/bin/sh
# The scala REPL doesn't have any config file, so this wrapper serves to set
# some property values and load an init file of utilities when run without
# arguments to enter REPL mode.
#
# If there are arguments, just assume we're running a .scala file in script
# mode, a class or jar, etc., and execute normally.
SCALA=${SCALA:-/usr/local/bin/scala}
if [ "$#" -eq 0 ] && [ -r ~/.config/scala/replinit.scala ]; then
exec "$SCALA" -i ~/.config/scala/replinit.scala -Dscala.color
else
exec "$SCALA" "$#"
fi
If you sometimes use Ammonite REPL, as another answer suggests, the utility definitions can be shared by loading them from ~/.ammonite/predef.scala:
try load.exec(ammonite.ops.home/".config"/'scala/"replinit.scala")
catch { case _: Exception => println("=== replrc not loaded! ===") }
I'm not sure about a way to load the init file for sbt console automatically, though—Seth Tisue's comment about the initialize setting is helpful for properties, but using a :load command in a value for initialCommands in console doesn't appear to work.
I am using CDH 5.2. I am able to use spark-shell to run the commands.
How can I run the file(file.spark) which contain spark commands.
Is there any way to run/compile the scala programs in CDH 5.2 without sbt?
In command line, you can use
spark-shell -i file.scala
to run code which is written in file.scala
To load an external file from spark-shell simply do
:load PATH_TO_FILE
This will call everything in your file.
I don't have a solution for your SBT question though sorry :-)
You can use either sbt or maven to compile spark programs. Simply add the spark as dependency to maven
<repository>
<id>Spark repository</id>
<url>http://www.sparkjava.com/nexus/content/repositories/spark/</url>
</repository>
And then the dependency:
<dependency>
<groupId>spark</groupId>
<artifactId>spark</artifactId>
<version>1.2.0</version>
</dependency>
In terms of running a file with spark commands: you can simply do this:
echo"
import org.apache.spark.sql.*
ssc = new SQLContext(sc)
ssc.sql("select * from mytable").collect
" > spark.input
Now run the commands script:
cat spark.input | spark-shell
Just to give more perspective to the answers
Spark-shell is a scala repl
You can type :help to see the list of operation that are possible inside the scala shell
scala> :help
All commands can be abbreviated, e.g., :he instead of :help.
:edit <id>|<line> edit history
:help [command] print this summary or command-specific help
:history [num] show the history (optional num is commands to show)
:h? <string> search the history
:imports [name name ...] show import history, identifying sources of names
:implicits [-v] show the implicits in scope
:javap <path|class> disassemble a file or class name
:line <id>|<line> place line(s) at the end of history
:load <path> interpret lines in a file
:paste [-raw] [path] enter paste mode or paste a file
:power enable power user mode
:quit exit the interpreter
:replay [options] reset the repl and replay all previous commands
:require <path> add a jar to the classpath
:reset [options] reset the repl to its initial state, forgetting all session entries
:save <path> save replayable session to a file
:sh <command line> run a shell command (result is implicitly => List[String])
:settings <options> update compiler options, if possible; see reset
:silent disable/enable automatic printing of results
:type [-v] <expr> display the type of an expression without evaluating it
:kind [-v] <expr> display the kind of expression's type
:warnings show the suppressed warnings from the most recent line which had any
:load interpret lines in a file
Tested on both spark-shell version 1.6.3 and spark2-shell version 2.3.0.2.6.5.179-4, you can directly pipe to the shell's stdin like
spark-shell <<< "1+1"
or in your use case,
spark-shell < file.spark
You can run as you run your shell script.
This example to run from command line environment
example
./bin/spark-shell :- this is the path of your spark-shell under bin
/home/fold1/spark_program.py :- This is the path where your python program is there.
So:
./bin.spark-shell /home/fold1/spark_prohram.py
I do not know whether it is a Scala or Play! question. I want to execute some external command from my Play application, get the output from the command and show a report to user based on the command output. Can anyone help?
For example, when I enter my-command from shell it shows output like below, which I want to capture and show in web:
Id Name IP
====================
1 A x.y.z.a
2 B p.q.r.s
Please, do not worry about format and parsing of the output. Functionally, I am looking something like PHP exec. I know about java Runtime.getRuntime().exec("command") but is there any Scala/Play version to serve the purpose?
The method !! of the Scala process package does what you need, it executes the statement and captures the text output. For example:
import scala.sys.process._
val cmd = "uname -a" // Your command
val output = cmd.!! // Captures the output
scala> import scala.sys.process._
scala> Process("cat temp.txt")!
This assumes there is a temp file in your home directory. ! is for actual execution of the command. See scala.sys.process for more info.
You can use the Process library: for instance
import scala.sys.process.Process
Process("ls").!!
to get the list of files in the folder as a string. The !! get the output of the command