我正在使用 CDH 5.2。我可以使用spark-shell来运行命令。
- 如何运行包含 spark 命令的文件(file.spark)。
- 有没有办法在没有 sbt 的情况下在 CDH 5.2 中运行/编译 scala 程序?
我正在使用 CDH 5.2。我可以使用spark-shell来运行命令。
在命令行中,您可以使用
spark-shell -i file.scala
运行编写的代码file.scala
要从 spark-shell 加载外部文件,只需执行
:load PATH_TO_FILE
这将调用您文件中的所有内容。
抱歉,我没有解决您的 SBT 问题的方法 :-)
您可以使用 sbt 或 maven 来编译 spark 程序。只需将 spark 作为依赖项添加到 maven
<repository>
<id>Spark repository</id>
<url>http://www.sparkjava.com/nexus/content/repositories/spark/</url>
</repository>
然后是依赖:
<dependency>
<groupId>spark</groupId>
<artifactId>spark</artifactId>
<version>1.2.0</version>
</dependency>
在使用 spark 命令运行文件方面:您可以简单地执行以下操作:
echo"
import org.apache.spark.sql.*
ssc = new SQLContext(sc)
ssc.sql("select * from mytable").collect
" > spark.input
现在运行命令脚本:
cat spark.input | spark-shell
只是为了给答案更多的视角
Spark-shell 是一个 scala repl
您可以键入:help以查看 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 解释文件中的行
spark-shell
version 1.6.3
在and上都经过测试spark2-shell
version 2.3.0.2.6.5.179-4
,您可以直接通过管道连接到 shell 的标准输入,例如
spark-shell <<< "1+1"
或在您的用例中,
spark-shell < file.spark
您可以在运行 shell 脚本时运行。此示例从命令行环境示例运行
./bin/spark-shell
:- 这是你的 spark-shell 在 bin 下
/home/fold1/spark_program.py
的路径 :- 这是你的 python 程序所在的路径。
所以:
./bin.spark-shell /home/fold1/spark_prohram.py