geotrellis使用(二十九)遷移geotrellis至1.1.1版


目錄

  1. 前言
  2. 升級過程
  3. 總結

一、前言

       由於忙着安裝OpenStack等等各種事情,有半年的時間沒有再親密的接觸geotrellis,甚至有半年的時間沒能暢快的寫代碼。近來OpenStack折騰的稍見成效,歷經九九八十一Failure后成功的在16台服務器上搭建了雲平台,於是干了一件瘋狂的事情——在OpenStack上創建建立幾台虛擬機,並用他們搭建了Hadoop集群,完事將之前的geotrellis代碼運行在集群上。一切看似很順利,但是我是個有強迫症的人,一看geotrellis已經升級到了1.1.1版,那么我也就趕緊將自己的代碼升級到此版本,於是有了本篇文章。

二、升級過程

       從1.0版升級到1.1.1版變化不是非常大,主要是以下幾個方面的變化:

2.1 廢棄spray,改用akka發布http服務

       之前geotrellis的習慣方式是使用spray來發布http服務,這樣會造成總總的版本沖突,前面我還專門有寫文章來探討版本沖突及解決方案。1.1.1版直接使用akka發布http服務,而無需spray便少了很多沖突的可能性。build.sbt文件如下:

import scala.util.Properties

val gtVersion = "1.1.1"
val scalaV = "2.11.8"
val sparkV = "2.1.0"
val hadoopV = "2.7.1"
val akkaActorVersion = "2.4.17"
val akkaHttpVersion  = "10.0.3"

name := "GeoTrellis-SJZX"
scalaVersion := Properties.propOrElse("scala.version", scalaV)
crossScalaVersions := Seq("2.11.8", "2.10.6")
organization := "com.sjzx"
licenses := Seq("Apache-2.0" -> url("http://www.apache.org/licenses/LICENSE-2.0.html"))
scalacOptions ++= Seq(
  "-deprecation",
  "-unchecked",
  "-Yinline-warnings",
  "-language:implicitConversions",
  "-language:reflectiveCalls",
  "-language:higherKinds",
  "-language:postfixOps",
  "-language:existentials",
  "-feature")
publishMavenStyle := true
publishArtifact in Test := false
pomIncludeRepository := { _ => false }

val geotrellis = Seq(
  "org.locationtech.geotrellis" %% "geotrellis-accumulo" % gtVersion,
  "org.locationtech.geotrellis" %% "geotrellis-hbase" % gtVersion,
  "org.locationtech.geotrellis" %% "geotrellis-cassandra" % gtVersion,
  "org.locationtech.geotrellis" %% "geotrellis-s3" % gtVersion,
  "org.locationtech.geotrellis" %% "geotrellis-spark" % gtVersion,
  "org.locationtech.geotrellis" %% "geotrellis-spark-etl" % gtVersion,
  "org.locationtech.geotrellis" %% "geotrellis-shapefile" % gtVersion
)

val akka = Seq(
  "com.typesafe.akka" %% "akka-actor" % akkaActorVersion,
  "com.typesafe.akka" %% "akka-http-core" % akkaHttpVersion,
  "com.typesafe.akka" %% "akka-http" % akkaHttpVersion,
  "com.typesafe.akka" %% "akka-http-spray-json" % akkaHttpVersion
)


val cluster = Seq(
  "org.apache.spark" %% "spark-core" % sparkV,
  "org.apache.hadoop" % "hadoop-client" % hadoopV
)

val library = geotrellis ++ akka ++ cluster

libraryDependencies ++= library

ivyScala := ivyScala.value map {
  _.copy(overrideScalaVersion = true)
}

test in assembly := {}

assemblyMergeStrategy in assembly := {
  case "reference.conf" => MergeStrategy.concat
  case "application.conf" => MergeStrategy.concat
  case "META-INF/MANIFEST.MF" => MergeStrategy.discard
  case "META-INF\\MANIFEST.MF" => MergeStrategy.discard
  case "META-INF/ECLIPSEF.RSA" => MergeStrategy.discard
  case "META-INF/ECLIPSEF.SF" => MergeStrategy.discard
  case _ => MergeStrategy.first
}

       發布服務語句如下:

import akka.http.scaladsl.Http
Http().bindAndHandle(routes, host, port)

       其中host為本機ip,port為服務端口,而routes則為你定義的路由規則。定義方式如下:

import akka.http.scaladsl.model.{ContentType, HttpEntity, HttpResponse, MediaTypes}
def routes = pathPrefix(IntNumber / IntNumber / IntNumber) { (zoom, x, y) =>
    parameters(
      'names,
      'mask ? ""
    ) { (names, maskz) =>

      complete {
        Future {
          val result = ...
          HttpResponse(entity = HttpEntity(ContentType(MediaTypes.`image/png`), result))
        }
      }
    }
  }

       可以看出基本與spray版本相同,只是此處引用的包均為akka的。不同的地方在於http的響應方式有變化,變為:

HttpResponse(entity = HttpEntity(ContentType(MediaTypes.`image/png`), result))

       其中result為瓦片的字節數組。

       具體可以參考官方示例https://github.com/geotrellis/geotrellis-chatta-demo

2.2 增加CollectionLayerReader查詢瓦片方式

       如果需要根據范圍或其他條件來查詢瓦片集,之前版本只能通過LayerReader的方式,現增加了CollectionLayerReader的方式。其使用方式基本與LayerReader相同,唯一不同的是返回結果不再是RDD集合,而是Seq集合。從這一點也能看出CollectionLayerReader不再使用Spark調用瓦片,而是直接調用Accumulo或其他數據庫中的瓦片數據,所以返回的不再是RDD集合。究竟兩種方式哪種更好,我並未做大量的實驗來進行測試,個人感覺CollectionLayerReader的方式可能更自由,速度也要稍微快些。以Accumulo為例,其創建方式如下:

val instance = AccumuloInstance(
  config.getString("accumulo.instance"),
  config.getString("accumulo.zookeepers"),
  config.getString("accumulo.user"),
  new PasswordToken(config.getString("accumulo.password"))
)

val collectionLayerReader = AccumuloCollectionLayerReader(instance)

三、總結

       本文並未包含過多的知識點,算是代碼狀態的一個回歸吧。雖然部署OpenStack等運維層面的工作以及單片機、嵌入式等硬件層面的工作我都很喜歡,成功后都會給我帶來深深的享受之感,其實我更喜歡寫代碼,一行行優美的如同藝術品的代碼從大腦經過指尖展示在顯示屏上,而后便能看到所有的事情全部按照自己預想的方式運行,這種快感是無法言表的。

       半年內也有很多人咨詢我關於geotrelis的相關問題,有些我耐心的回答了,有些問的問題明顯就是沒有經過大腦的,用寶玉的話說:懶怠回答,只說幾個字去看我的博客。有些人說看了我的博客之后學到很多,也有些人說沒有講清楚,所以我感覺可能是我真的表達的不太清楚,於是我后續可能再寫一系列的博客針對geotrellis的各個部分或者功能來進行詳細講解,而不是像現在這樣結合具體業務來進行分析,當然結合具體業務進行分析的方式也會繼續進行。敬請期待!

Geotrellis系列文章鏈接地址http://www.cnblogs.com/shoufengwei/p/5619419.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM