今天我將介紹如何在Java工程使用Scala代碼。對於那些想在真實場景中嘗試使用Scala的開發人員來講,會很是有意思。這和你項目中有什麼類型的東西毫無關係:無論是Spring仍是Spark仍是別的。咱們廢話少說,開始吧。html
這裏咱們使用Maven來管理咱們的Java項目,項目的結果以下所示:java
若是想及時瞭解Spark、Hadoop或者Hbase相關的文章,歡迎關注微信公共賬號:iteblog_hadoopapache
正如你所看到的,工程的結構很是簡單。它有標準的佈局和僅僅三個Java類,以下所示:微信
package com.iteblog.demo.model; /** * User: 過往記憶 * Date: 2016-12-30 * Time: 下午23:16 * bolg: https://www.iteblog.com * 本文地址:https://www.iteblog.com/archives/1947.html * 過往記憶博客,專一於hadoop、hive、spark、shark、flume的技術博客,大量的乾貨 * 過往記憶博客微信公共賬號:iteblog_hadoop */ public class Book { private String name = null; private String author = null; public Book(String name, String author) { this.name = name; this.author = author; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } @Override public String toString() { return "Book {" + "name='" + name + '\'' + ", author='" + author + '\'' + '}'; } }
下面是所謂的數據存儲maven
package com.iteblog.demo.storage; import com.iteblog.demo.model.Book; import java.util.ArrayList; /** * User: 過往記憶 * Date: 2016-12-30 * Time: 下午23:16 * bolg: https://www.iteblog.com * 本文地址:https://www.iteblog.com/archives/1947.html * 過往記憶博客,專一於hadoop、hive、spark、shark、flume的技術博客,大量的乾貨 * 過往記憶博客微信公共賬號:iteblog_hadoop */ public class BookStorage { private ArrayList<Book> books = new ArrayList<>(); public BookStorage() { books.add(new Book("White Fang", "Jack London")); books.add(new Book("The Sea-Wolf", "Jack London")); books.add(new Book("The Road", "Jack London")); books.add(new Book("The Adventures of Tom Sawyer", "Mark Twain")); books.add(new Book("Around the World in 80 Days", "Jules Verne")); books.add(new Book("Twenty Thousand Leagues Under the Sea", "Jules Verne")); books.add(new Book("The Mysterious Island", "Jules Verne")); books.add(new Book("The Four Million", "O. Henry")); books.add(new Book("The Last Leaf", "O. Henry")); } public ArrayList<Book> getBooks() { return books; } }
最後是主類:ide
package com.iteblog.demo; import com.iteblog.demo.storage.BookStorage; /** * User: 過往記憶 * Date: 2016-12-30 * Time: 下午23:16 * bolg: https://www.iteblog.com * 本文地址:https://www.iteblog.com/archives/1947.html * 過往記憶博客,專一於hadoop、hive、spark、shark、flume的技術博客,大量的乾貨 * 過往記憶博客微信公共賬號:iteblog_hadoop */ public class Runner { public static void main(String[] args) { BookStorage storage = new BookStorage(); storage.getBooks().stream().forEach(System.out::println); } }
最後別忘記咱們還有個pom.xml文件,它可能包含一些依賴,插件和構建目標;不過這些並不重要。oop
爲了可以在Java Maven工程中使用Scala,咱們須要使用一個Maven插件:scala-maven-plugin,把下面的代碼加入到pom.xml文件中佈局
<plugin> <groupId>net.alchim31.maven</groupId> <artifactId>scala-maven-plugin</artifactId> <executions> <execution> <id>scala-compile-first</id> <phase>process-resources</phase> <goals> <goal>add-source</goal> <goal>compile</goal> </goals> </execution> <execution> <id>scala-test-compile</id> <phase>process-test-resources</phase> <goals> <goal>testCompile</goal> </goals> </execution> </executions> </plugin>
由於咱們須要使用Scala代碼,因此咱們還須要加入scala-library依賴,以下:ui
<dependency> <groupId>org.scala-lang</groupId> <artifactId>scala-library</artifactId> <version>2.11.7</version> </dependency>
最後完整的額pom.xml文件內容以下:this
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.iteblog.demo</groupId> <artifactId>iteblog</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.scala-lang</groupId> <artifactId>scala-library</artifactId> <version>2.11.7</version> </dependency> </dependencies> <build> <plugins> <!-- This plugin compiles Scala files --> <plugin> <groupId>net.alchim31.maven</groupId> <artifactId>scala-maven-plugin</artifactId> <executions> <execution> <id>scala-compile-first</id> <phase>process-resources</phase> <goals> <goal>add-source</goal> <goal>compile</goal> </goals> </execution> <execution> <id>scala-test-compile</id> <phase>process-test-resources</phase> <goals> <goal>testCompile</goal> </goals> </execution> </executions> </plugin> <!-- This plugin compiles Java files --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> <executions> <execution> <phase>compile</phase> <goals> <goal>compile</goal> </goals> </execution> </executions> </plugin> <!-- This plugin adds all dependencies to JAR file during 'package' command. Pay EXTRA attention to the 'mainClass' tag. You have to set name of class with entry point to program ('main' method) --> <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>2.5.3</version> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <mainClass>ScalaRunner</mainClass> </manifest> </archive> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
更新完上面的內容以後,你須要等待Maven下載完全部的依賴。
如今咱們能夠在Java工程中使用Scala代碼了。爲此,您須要建立新的文件夾src/main/scala
;Scala Maven插件將會識別這些目錄,而且編譯其中的Scala文件:
如今咱們在工程裏面加入Scala代碼:
package com.iteblog.service import java.util import com.iteblog.demo.model.Book import scala.collection.JavaConversions._ /** * User: 過往記憶 * Date: 2016-12-30 * Time: 下午23:16 * bolg: https://www.iteblog.com * 本文地址:https://www.iteblog.com/archives/1947.html * 過往記憶博客,專一於hadoop、hive、spark、shark、flume的技術博客,大量的乾貨 * 過往記憶博客微信公共賬號:iteblog_hadoop */ object BooksProcessor { def filterByAuthor(author: String)(implicit books: util.ArrayList[Book]) = { books.filter(book => book.getAuthor == author) } }
如今咱們能夠在Scala代碼中遍歷相關的圖書了:
import com.iteblog.demo.storage.BookStorage import com.iteblog.service.BooksProcessor /** * User: 過往記憶 * Date: 2016-12-30 * Time: 下午23:16 * bolg: https://www.iteblog.com * 本文地址:https://www.iteblog.com/archives/1947 * 過往記憶博客,專一於hadoop、hive、spark、shark、flume的技術博客,大量的乾貨 * 過往記憶博客微信公共賬號:iteblog_hadoop */ object ScalaRunner extends App { implicit val books = new BookStorage().getBooks BooksProcessor.filterByAuthor("Jack London").foreach(b => println(b)) }
最後加入Scala代碼的工程看起來以下:
若是想及時瞭解Spark、Hadoop或者Hbase相關的文章,歡迎關注微信公共賬號:iteblog_hadoop
運行ScalaRunner代碼獲得下面的結果:
Book {name='White Fang', author='Jack London'} Book {name='The Sea-Wolf', author='Jack London'} Book {name='The Road', author='Jack London'}
本博客文章除特別聲明,所有都是原創!
轉載本文請加上:轉載自過往記憶(https://www.iteblog.com/)
本文連接: 【如何在Java Maven工程中編寫Scala代碼】(https://www.iteblog.com/archives/1947.html)