1 寫入數(shù)據(jù)
GeoMesa作為空間大數(shù)據(jù)處理框架圈膏,本身是不存儲(chǔ)數(shù)據(jù)的。數(shù)據(jù)存儲(chǔ)依賴于底層的分布式數(shù)據(jù)庫篙骡,如HBase稽坤,Accumulo等桥帆。GeoMesa為了同時(shí)支持多種數(shù)據(jù)庫,提供了一個(gè)抽象的接口慎皱,屏蔽不同數(shù)據(jù)庫操作方法的差異老虫,使用戶用一種統(tǒng)一的方式存儲(chǔ)和查詢數(shù)據(jù)。來看下面這GeoMesa存儲(chǔ)架構(gòu)圖:
從這張圖中可以看出茫多,無論采用何種方式寫入數(shù)據(jù)祈匙,我們都不需要直接操作數(shù)據(jù)庫,而只需按照GeoTools或GeoMesa的接口天揖,規(guī)范好要存儲(chǔ)的數(shù)據(jù)夺欲,提交給GeoMesa處理即可。GeoMesa會(huì)自動(dòng)為我們完成創(chuàng)建元數(shù)據(jù)今膊、創(chuàng)建索引些阅、存儲(chǔ)數(shù)據(jù)等一系列過程。
GeoTools 是英國利茲大學(xué)(University of Leeds)的James Macgll 從1996 年開始研發(fā)的一個(gè)操作和顯示地圖的開源Java 代碼庫斑唬,提供了一系列符合規(guī)范的操作地理空間數(shù)據(jù)的方法市埋,可以用它來實(shí)現(xiàn)一個(gè)地理信息系統(tǒng)。GeoTools庫在開發(fā)時(shí)執(zhí)行OGC規(guī)范恕刘。
GeoTools提供了許多用于表示地理空間數(shù)據(jù)的接口缤谎,可以用他們來定義需要存儲(chǔ)的數(shù)據(jù):
DataStore
DataStore是數(shù)據(jù)的核心訪問模型,存儲(chǔ)了數(shù)據(jù)集的名稱褐着、數(shù)據(jù)結(jié)構(gòu)與類型坷澡、數(shù)據(jù)訪問源等信息,類似一種數(shù)據(jù)元信息的存儲(chǔ)集合含蓉,用于定義和描述數(shù)據(jù)的基本信息频敛。
SimpleFeatureType
SimpleFeatureType即簡單要素類型,用于定義數(shù)據(jù)類型馅扣,類似SQL語句中create table時(shí)定義表字段時(shí)所指定的信息斟赚。常用一種特殊格式的字符串表示,如:
tpList:String,startTime:Date:default=true,startPoint:Point:srid=4326:default=true
創(chuàng)建SimpleFeatureType時(shí)岂嗓,需要使用GeoMesa提供的org.locationtech.geomesa.utils.interop.SimpleFeatureTypes
類汁展,而不是直接使用Geotools的DataUtilities創(chuàng)建,目的是為了支持更多的數(shù)據(jù)類型厌殉。
sft = SimpleFeatureTypes.createType(getTypeName(), attributes.toString());
SimpleFeatureType在DataStore中被稱為Schema食绿,使用dataStore.createSchema(sft)
函數(shù)在DataStore中創(chuàng)建Schema。
SimpleFeature
SimpleFeature即簡單要素公罕,用于定義具體的數(shù)據(jù)器紧。可以使用Geotools提供的SimpleFeatureBuilder
類創(chuàng)建楼眷,只需要按照先前定義的SimpleFeatureType铲汪,依次傳入相應(yīng)類型的數(shù)據(jù)熊尉,最后設(shè)置Feature的ID即可,如:
SimpleFeatureBuilder builder = new SimpleFeatureBuilder(getSimpleFeatureType());
builder.set("tpList", ptListString);
builder.set("startTime", new Date(Long.valueOf(startTime)*1000));
builder.set("startPoint", "POINT (" + startPointY + " " + startPointX + ")");
SimpleFeature feature = builder.buildFeature(id);
其中掌腰,空間數(shù)據(jù)可以用WKT(well-known-text)格式的字符串來表示狰住。
寫入
創(chuàng)建好SimpleFeature后,就可以開始向數(shù)據(jù)庫中寫入數(shù)據(jù)了齿梁。
使用Geotools提供的FeatureWriter執(zhí)行寫入操作催植,具體代碼如下:
FeatureWriter<SimpleFeatureType, SimpleFeature> writer = datastore.getFeatureWriterAppend(sft.getTypeName(), Transaction.AUTO_COMMIT)
for (SimpleFeature feature : features) {
SimpleFeature toWrite = writer.next();
// copy attributes
toWrite.setAttributes(feature.getAttributes());
// if you want to set the feature ID, you have to cast to an implementation class
// and add the USE_PROVIDED_FID hint to the user data
((FeatureIdImpl) toWrite.getIdentifier()).setID(feature.getID());
toWrite.getUserData().put(Hints.USE_PROVIDED_FID, Boolean.TRUE);
// make sure to copy the user data, if there is any
toWrite.getUserData().putAll(feature.getUserData());
// write the feature
writer.write();
}
完整代碼:官方教程
2 查詢數(shù)據(jù)
GeoMesa同樣使用了GeoTools工具,作為查詢操作的接口勺择,查詢時(shí)的過程圖如下:
使用GeoTools進(jìn)行查詢的基本流程如下:
① 獲取要查詢的要素名稱创南,即寫入時(shí)SimpleFeatureType的Name
② 對想要查詢的字段,編寫相應(yīng)的查詢條件省核,并創(chuàng)建Filter類型的對象
③ 創(chuàng)建Query對象稿辙,將上一步中所有查詢條件加入其中
④ 執(zhí)行查詢,獲得查詢結(jié)果
其中气忠,查詢條件可以使用GeoTools提供的CQL(GeoTools’ Contextual Query Language)或ECQL語句編寫邻储,并直接轉(zhuǎn)換為Filter對象,例如:
Filter result = CQL.toFilter("ATTR1 < 10 AND ATTR2 < 2 OR ATTR3 > 10" );
Filter result = CQL.toFilter( "ATTR1 AFTER 2006-11-30T01:30:00Z/2006-12-31T01:30:00Z" );
Filter result = CQL.toFilter( "CONTAINS(ATTR1, POINT(1 2))" );
Filter result = CQL.toFilter( "BBOX(ATTR1, 10,20,30,40)" );
Filter result = CQL.toFilter( "DWITHIN(ATTR1, POINT(1 2), 10, kilometers)" );
完整CQL教程:官方文檔
執(zhí)行查詢時(shí)笔刹,使用FeatureReader完成查詢操作芥备,代碼如下:
List<SimpleFeature> queryFeatureList = new ArrayList<>();
FeatureReader<SimpleFeatureType, SimpleFeature> reader = datastore.getFeatureReader(query, Transaction.AUTO_COMMIT)
int n = 0;
while(reader.hasNext()){
SimpleFeature feature=reader.next();
queryFeatureList.add(feature);
n++;
}
System.out.println();
System.out.println("Returned"+n+"totalfeatures");
完整代碼:官方教程
幾個(gè)常用查詢條件
設(shè)置最大返回條目:
Query query = new Query(typeName, ECQL.toFilter(queryCQL));
query.setMaxFeatures(Integer.parseInt(maxView));
設(shè)置排序:
Query query = new Query(typeName, ECQL.toFilter(queryCQL));
FilterFactoryImpl ff = new FilterFactoryImpl();
query.setSortBy(new SortBy[]{new SortByImpl(ff.property("startTime"), SortOrder.ASCENDING)});
統(tǒng)計(jì)查詢-查總數(shù)
Query query = new Query(typeName);
query.getHints().put(QueryHints.STATS_STRING(), "Count()");
聚合查詢-GroupBy冬耿,查每個(gè)分組的總數(shù)
Query query = new Query(typeName);
query.getHints().put(QueryHints.STATS_STRING(), "GroupBy(\"carID\",Count())");
統(tǒng)計(jì)查詢-查最大最小值
Query query = new Query(typeName);
query.getHints().put(QueryHints.STATS_STRING(), "MinMax(\"startTime\")");
更多類型的統(tǒng)計(jì)/聚合查詢:官方文檔