Scala脚本化-Ammonite

Scala语言定义:

Scala combines object-oriented and functional programming in one concise, high-level language. 
Scala's static types help avoid bugs in complex applications, and its JVM and JavaScript runtimes let you build high-performance systems with easy access to huge ecosystems of libraries.

        --来自Scala官网:https://www.scala-lang.org/

简单翻译一下:Scala 是一个将面向对象与函数式编程简明的结合在一起的高级语言。Scala的静态类型有效的避免了在复杂应用中的Bugs。并且他运行在Java虚拟机上,并且它拥有强大的三方库供调用,这些都可以让你轻松构建高性能的系统。

感觉翻译能力太差,还是做拿来主义吧,网上的翻译:

Scala 是一门多范式(multi-paradigm)的编程语言,设计初衷是要集成面向对象编程和函数式编程的各种特性。
Scala 运行在Java虚拟机上,并兼容现有的Java程序。
Scala 源代码被编译成Java字节码,所以它可以运行于JVM之上,并可以调用现有的Java类库。

 

通过这段时间对Scala的学习与实际项目中的使用,确实能感受到Scala的强大,简洁,函数式编程的魅力。

当然,我们也可以像Shell,Perl这些脚本语言,用Scala实现缩短传统的编写-编译-链接-运行(edit-compile-link-run)过程。

一般先定义Scala脚本如下:

Script.scala:

object Script{
 def main(args: Array[String]){
  println("Hello World")
 }
}

如下打开Scala控制台执行如下命令:

scala -cp . Script

可打印:

Hello World

 

我们今天主要不是讲Scala原生自带执行Scala脚本的方法,我们讲的是一个灵活执行Scala脚本的框架:Ammonite
之所以说它灵活,就是因为他不仅可以像Scala控制台命令里面执行脚本,还可以直接集成到代码里面执行脚本文件并返回结果给程序处理。
国内参考文档较少,还附上官网及源码:
官网地址:http://ammonite.io/
Github地址:https://github.com/lihaoyi/Ammonite

1. 下载安装方法

 1 mkdir demo
 2 cd demo/
 3 wget https://github.com/lihaoyi/Ammonite/releases/download/1.1.2/2.12-1.1.2
 4 mv 2.12-1.1.2 amm
 5 chmod +x amm
 6 vi amm
 7 ./amm
 8 touch test1.sc
 9 import $ivy.`mysql:mysql-connector-java:5.1.36`
10 import $ivy.`com.github.wangzaixiang::scala-sql:2.0.3`
11 import wangzx.scala_commons.sql._
12 val driverClassName="com.mysql.jdbc.Driver"
13 val url="jdbc:mysql://10.10.10.108:3358/finance?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&rewriteBatchedStatements=true"
14 val username="root"
15 val password="today-36524"
16 val conn = java.sql.DriverManager.getConnection(url, username, password)
17 val rows = conn.rows[Row]("select * from t_invoice limit 10")
18 rows.foreach(println)
19 
20 ./amm test1.sc

 

2. 实际代码应用

1)引入Ammonite包

name := "db_report"

version := "0.1"

scalaVersion := "2.12.6"

libraryDependencies ++= Seq(
"com.lihaoyi" % "ammonite_2.12.6" % "1.1.1"
)

 

2)编写Scala脚本
Test.sc

import ammonite.main.Router.{doc, main}

@main
def main(x: Int@doc("this is x"), y:Int@doc("this is y")) = {
 val result = x*y
 println(result)
 Map("key" -> result)
}

  

3)调用Scala脚本并返回结果

package com.today.dbreport

import ammonite.ops.Path
import ammonite.util.Res
import ammonite.util.Res.{Exception, Failure, Success}

object Test {
  def main(args: Array[String]): Unit = {
    val args = Seq(("x", Some("2")), ("y", Some("3")))
    //运行Scala脚本并获取数据Map
    val result: (Res[Any], Seq[(Path, Long)]) = ammonite.Main().runScript(Path("Test.sc"), args)
    val dataMap = (result._1 match {
      case Success(x) => {
        Some(x.asInstanceOf[Map[String, AnyRef]])
      }
      case Failure(msg) => println(s"generate report by script failure: ${msg}"); None
      case Exception(msg, e) => println(s"generate report by script exception: ${msg}", e); None
    }) match {
      case Some(x) => x
      case None => Map()
    }

    println(dataMap);
  }
}

  

  输出结果:

Compiling (synthetic)/ammonite/predef/interpBridge.sc
Compiling (synthetic)/ammonite/predef/DefaultPredef.sc
Compiling /home/barry/data/projects/db_report/src/main/resources/scripts/Test.sc
6
Map(key -> 6)

 

你可能感兴趣的:(Scala脚本化-Ammonite)