(十三)GeoSpark源碼解析(二)

GeoSpark源碼解析(二)

本節(jié)我們還是以查詢?yōu)槔⌒铮聪翯eoSpark如何封裝JTS中的索引的燕耿。在上節(jié)我們簡單看了SpaitalRDD昧廷,其中降到了JavaRDD<T> rawSpatialRDD被碗,在不使用索引的情況下奏属,也是我們GeoSpark提供了match方法伶丐,調(diào)用RDD的map方法完成的悼做,本節(jié)我們在看一個SpatialRDD的成員indexedRawRDD

public class SpatialRDD<T extends Geometry>
        implements Serializable
{
    /**
     * The raw spatial RDD.
     */
    public JavaRDD<T> rawSpatialRDD;
    
   /**
     * The indexed raw RDD.
     */
    public JavaRDD<SpatialIndex> indexedRawRDD;
    
    ...
 }}

indexedRawRDD需要我們調(diào)用buildIndex(final IndexType indexType, boolean buildIndexOnSpatialPartitionedRDD)來構(gòu)建,我們看下這個函數(shù)是如何構(gòu)建索引的

public void buildIndex(final IndexType indexType, boolean buildIndexOnSpatialPartitionedRDD)
            throws Exception
    {
        if (buildIndexOnSpatialPartitionedRDD == false) {
            //This index is built on top of unpartitioned SRDD
            this.indexedRawRDD = this.rawSpatialRDD.mapPartitions(new IndexBuilder(indexType));
        }
        else {
            if (this.spatialPartitionedRDD == null) {
                throw new Exception("[AbstractSpatialRDD][buildIndex] spatialPartitionedRDD is null. Please do spatial partitioning before build index.");
            }
            this.indexedRDD = this.spatialPartitionedRDD.mapPartitions(new IndexBuilder(indexType));
        }
    }

我們先看buildIndexOnSpatialPartitionedRDDfalse的情況(分區(qū)的情況我們下次介紹)哗魂,第6行代碼this.indexedRawRDD = this.rawSpatialRDD.mapPartitions(new IndexBuilder(indexType));又調(diào)用了mapPartitions方法肛走,只不過這次傳遞的是IndexBuilder對象,我們看下這個對象

public final class IndexBuilder<T extends Geometry>
        implements FlatMapFunction<Iterator<T>, SpatialIndex>
{
    IndexType indexType;
    public IndexBuilder(IndexType indexType)
    {
        this.indexType = indexType;
    }
    @Override
    public Iterator<SpatialIndex> call(Iterator<T> objectIterator)
            throws Exception
    {
        SpatialIndex spatialIndex;
        if (indexType == IndexType.RTREE) {
            spatialIndex = new STRtree();
        }
        else {
            spatialIndex = new Quadtree();
        }
        while (objectIterator.hasNext()) {
            T spatialObject = objectIterator.next();
            spatialIndex.insert(spatialObject.getEnvelopeInternal(), spatialObject);
        }
        Set<SpatialIndex> result = new HashSet();
        spatialIndex.query(new Envelope(0.0, 0.0, 0.0, 0.0));
        result.add(spatialIndex);
        return result.iterator();
    }
}

整個類僅有30行左右代碼录别,功能很簡單朽色,就是提供一個Map映射函數(shù),indexType是構(gòu)建索引類型组题,JTS提供了兩種STRTreeeQuadtree葫男,關(guān)于他們的原理大家可以去看GIS相關(guān)教材,第20行的while循環(huán)就開始將RDD中的Geometry添加到索引樹中崔列,因為Spark規(guī)定call函數(shù)是一定要返回一個迭代器的梢褐,所以GeoSpark就將spatialIndex加到Set集合,并返回其迭代器赵讯。這里補充一點盈咳,從這里我們還能得出一點,實際上一個分區(qū)只有一個索引边翼,這也與GIS實際情況相吻合鱼响。

索引構(gòu)造完成后,就可以利用索引來進行并行分析了讯私,我們還是以查詢?yōu)槔?/p>

public static <U extends Geometry, T extends Geometry> JavaRDD<T> SpatialRangeQuery(SpatialRDD<T> spatialRDD, U originalQueryGeometry, boolean considerBoundaryIntersection, boolean useIndex)
            throws Exception
    {
        U queryGeometry = originalQueryGeometry;
        if (spatialRDD.getCRStransformation()) {
            queryGeometry = CRSTransformation.Transform(spatialRDD.getSourceEpsgCode(), spatialRDD.getTargetEpgsgCode(), originalQueryGeometry);
        }

        if (useIndex == true) {
            if (spatialRDD.indexedRawRDD == null) {
                throw new Exception("[RangeQuery][SpatialRangeQuery] Index doesn't exist. Please build index on rawSpatialRDD.");
            }
            return spatialRDD.indexedRawRDD.mapPartitions(new RangeFilterUsingIndex(queryGeometry, considerBoundaryIntersection, true));
        }
        else {
            return spatialRDD.getRawSpatialRDD().filter(new RangeFilter(queryGeometry, considerBoundaryIntersection, true));
        }
    }

首先热押,GeoSpark在第9行先判斷是否使用索引,緊接著判斷是否構(gòu)建了索引斤寇,若構(gòu)建了索引桶癣,就執(zhí)行第13行return spatialRDD.indexedRawRDD.mapPartitions(new RangeFilterUsingIndex(queryGeometry, considerBoundaryIntersection, true));我們看RangeFilterUsingIndex這個類。

public class RangeFilterUsingIndex<U extends Geometry, T extends Geometry>
        extends JudgementBase
        implements FlatMapFunction<Iterator<SpatialIndex>, T>
{
    public RangeFilterUsingIndex(U queryWindow, boolean considerBoundaryIntersection, boolean leftCoveredByRight)
    {
        super(queryWindow, considerBoundaryIntersection, leftCoveredByRight);
    }
    @Override
    public Iterator<T> call(Iterator<SpatialIndex> treeIndexes)
            throws Exception
    {
        assert treeIndexes.hasNext() == true;
        SpatialIndex treeIndex = treeIndexes.next();
        List<T> results = new ArrayList<T>();
        List<T> tempResults = treeIndex.query(this.queryGeometry.getEnvelopeInternal());
        for (T tempResult : tempResults) {
            if (leftCoveredByRight) {
                if (match(tempResult, queryGeometry)) {
                    results.add(tempResult);
                }
            }
            else {
                if (match(queryGeometry, tempResult)) {
                    results.add(tempResult);
                }
            }
        }
        return results.iterator();
    }
}

注意到call這個方法娘锁,首先在第14行SpatialIndex treeIndex = treeIndexes.next();取出索引treeIndex牙寞,然后首先根據(jù)查詢窗口queryGeometry利用索引樹快速查出這個范圍內(nèi)的Geometry(因為是索引查詢,結(jié)果不是精確的)莫秆,然后從第17行開始遍歷tempResults间雀,調(diào)用match方法,相比于直接搜索镊屎,優(yōu)勢就在于我們不在搜索整個結(jié)果集惹挟,當(dāng)數(shù)據(jù)量大的時候,是有這很大優(yōu)勢的缝驳,match它在父類JudgementBase定義有

public boolean match(Geometry spatialObject, Geometry queryWindow)
    {
        if (considerBoundaryIntersection) {
            if (queryWindow.intersects(spatialObject)) { return true; }
        }
        else {
            if (queryWindow.covers(spatialObject)) { return true; }
        }
        return false;
    }

這里面连锯,我們可以看到第4行和第7行均是利用了JTS來判斷的,到這里用狱,就一目了然了运怖,實際上還是我們提供了match這個方法,利用Spark來計算夏伊。

到這里摇展,我們就將索引和非索引查詢分析完了,下節(jié)我們來看下Spark的另一個重要特性分區(qū)溺忧。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末咏连,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子砸狞,更是在濱河造成了極大的恐慌捻勉,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,755評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件刀森,死亡現(xiàn)場離奇詭異踱启,居然都是意外死亡,警方通過查閱死者的電腦和手機研底,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評論 3 395
  • 文/潘曉璐 我一進店門埠偿,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人榜晦,你說我怎么就攤上這事冠蒋。” “怎么了乾胶?”我有些...
    開封第一講書人閱讀 165,138評論 0 355
  • 文/不壞的土叔 我叫張陵抖剿,是天一觀的道長朽寞。 經(jīng)常有香客問我,道長斩郎,這世上最難降的妖魔是什么脑融? 我笑而不...
    開封第一講書人閱讀 58,791評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮缩宜,結(jié)果婚禮上肘迎,老公的妹妹穿的比我還像新娘。我一直安慰自己锻煌,他們只是感情好妓布,可當(dāng)我...
    茶點故事閱讀 67,794評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著宋梧,像睡著了一般匣沼。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上乃秀,一...
    開封第一講書人閱讀 51,631評論 1 305
  • 那天肛著,我揣著相機與錄音,去河邊找鬼跺讯。 笑死枢贿,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的刀脏。 我是一名探鬼主播局荚,決...
    沈念sama閱讀 40,362評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼愈污!你這毒婦竟也來了耀态?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,264評論 0 276
  • 序言:老撾萬榮一對情侶失蹤暂雹,失蹤者是張志新(化名)和其女友劉穎首装,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體杭跪,經(jīng)...
    沈念sama閱讀 45,724評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡仙逻,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了涧尿。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片系奉。...
    茶點故事閱讀 40,040評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖姑廉,靈堂內(nèi)的尸體忽然破棺而出缺亮,到底是詐尸還是另有隱情,我是刑警寧澤桥言,帶...
    沈念sama閱讀 35,742評論 5 346
  • 正文 年R本政府宣布萌踱,位于F島的核電站葵礼,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏并鸵。R本人自食惡果不足惜章咧,卻給世界環(huán)境...
    茶點故事閱讀 41,364評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望能真。 院中可真熱鬧,春花似錦扰柠、人聲如沸粉铐。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽蝙泼。三九已至,卻和暖如春劝枣,著一層夾襖步出監(jiān)牢的瞬間汤踏,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評論 1 270
  • 我被黑心中介騙來泰國打工舔腾, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留溪胶,地道東北人。 一個月前我還...
    沈念sama閱讀 48,247評論 3 371
  • 正文 我出身青樓稳诚,卻偏偏與公主長得像哗脖,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子扳还,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,979評論 2 355

推薦閱讀更多精彩內(nèi)容