[原创]Scala学习:编写Scala脚本
Posted jason_zhangz
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[原创]Scala学习:编写Scala脚本相关的知识,希望对你有一定的参考价值。
scala支持脚本
1)在/opt/scala-script下创建一个文件hello.scala 编辑内容如下:
$ hello ,this is the first scala script
2)运行脚本
scala脚本的命令行参数保存在名为args的scala数组中。scala里,数组以0开始,可以通过在括号里指定索引值来访问数组元素。scala里数组 args 的第一个元素是:args(0),而不是像Java那样的:args[0]。现在,把以下内容写到新文件:
HelloWithArgs.scala中测试一下:
//向第一个参数打问好
println("hello :"+ args(0))
root@scala:/opt/scala-script#scala hello.scala
java.net.UnknownHostException: scala: scala: unknown error
at java.net.InetAddress.getLocalHost(InetAddress.java:1505)
at scala.tools.nsc.io.Socket$.localhost(Socket.scala:28)
at scala.tools.nsc.CompileSocket.getsock$1(CompileSocket.scala:175)
at scala.tools.nsc.CompileSocket.getOrCreateSocket(CompileSocket.scala:190)
at scala.tools.nsc.ScriptRunner.compileWithDaemon(ScriptRunner.scala:70)
at scala.tools.nsc.ScriptRunner.scala$tools$nsc$ScriptRunner$$compile$1(ScriptRunner.scala:113)
at scala.tools.nsc.ScriptRunner$$anonfun$withCompiledScript$1.apply$mcZ$sp(ScriptRunner.scala:161)
at scala.tools.nsc.ScriptRunner$$anonfun$withCompiledScript$1.apply(ScriptRunner.scala:129)
at scala.tools.nsc.ScriptRunner$$anonfun$withCompiledScript$1.apply(ScriptRunner.scala:129)
at scala.tools.nsc.util.package$.trackingThreads(package.scala:43)
at scala.tools.nsc.util.package$.waitingForThreads(package.scala:27)
at scala.tools.nsc.ScriptRunner.withCompiledScript(ScriptRunner.scala:128)
at scala.tools.nsc.ScriptRunner.runScript(ScriptRunner.scala:192)
at scala.tools.nsc.ScriptRunner.runScriptAndCatch(ScriptRunner.scala:205)
at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:67)
at scala.tools.nsc.MainGenericRunner.run$1(MainGenericRunner.scala:87)
at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:98)
at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:103)
at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
Caused by: java.net.UnknownHostException: scala: unknown error
at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:928)
at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1323)
at java.net.InetAddress.getLocalHost(InetAddress.java:1500)
... 18 more
以上是关于[原创]Scala学习:编写Scala脚本的主要内容,如果未能解决你的问题,请参考以下文章
在 Bash 脚本中执行 Apache Spark (Scala) 代码