このトピックは、コーディングによってテスト目的で Ganos 開発環境を迅速に構築するのに役立ちます。このトピックを読んだ後、データベースへの接続方法、インデックスの作成方法、データのインポート方法、データのクエリ方法など、Ganos 時空間データを管理する方法を学習します。
依存関係:
<dependencies>
<dependency>
<groupId>org.locationtech.geomesa</groupId>
<artifactId>geomesa-hbase-datastore_2.11</artifactId>
<version>${geomesa.version}</version>
<exclusions>
<exclusion>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-common</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-protocol</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.aliyun.hbase</groupId>
<artifactId>alihbase-client</artifactId>
<version>${hbase.version}</version>
<exclusions>
<exclusion>
<artifactId>com.google.guava</artifactId>
<groupId>guava</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.aliyun.hbase</groupId>
<artifactId>alihbase-server</artifactId>
<version>${hbase.version}</version>
</dependency>
<dependency>
<groupId>com.aliyun.hbase</groupId>
<artifactId>alihbase-common</artifactId>
<version>${hbase.version}</version>
</dependency>
<dependency>
<groupId>com.aliyun.hbase</groupId>
<artifactId>alihbase-protocol</artifactId>
<version>${hbase.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.83</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<inherited>true</inherited>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
サンプルコードと説明
package com.aliyun.tst;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Point;
import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFinder;
import org.geotools.data.Query;
import org.geotools.data.Transaction;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureWriter;
import org.geotools.factory.CommonFactoryFinder;
import org.geotools.factory.Hints;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.filter.text.ecql.ECQL;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.locationtech.geomesa.utils.geotools.SimpleFeatureTypes;
import org.opengis.feature.Feature;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.filter.FilterFactory2;
import org.opengis.filter.sort.SortBy;
import org.opengis.filter.sort.SortOrder;
import java.io.BufferedReader;
import java.io.FileReader;
import java.text.SimpleDateFormat;
import java.util.*;
public class Demo {
public static final String ZK = "localhost"; // ZooKeeperエンドポイント。
public static void main(String args[]){
try{
DataStore ds=null;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd' 'HH:mm:ss");
// 接続パラメータを構成します。
Map<String, String> params= new HashMap<>();
params.put("hbase.zookeepers",ZK);
params.put("hbase.catalog","test_catalog");
// DataStore を初期化します。
ds= DataStoreFinder.getDataStore(params);
// SimpleFeatureType を使用してテーブルスキーマを定義します。
String sft_name="point";
SimpleFeatureType sft=
SimpleFeatureTypes.createType(sft_name, "name:String,dtg:Date,*geom:Point:srid=4326");
// 圧縮アルゴリズムとして Gzip を指定します。
sft.getUserData().put("geomesa.table.compression.enabled", "true");
sft.getUserData().put("geomesa.table.compression.type", "gz");
// テーブルを作成します。
ds.createSchema(sft);
/*
* GeometryFactory オブジェクトを作成します。
*/
GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
SimpleFeatureBuilder builder = new SimpleFeatureBuilder(sft);
// 空間データ (ポイント) を構築します。
Point point1 = geometryFactory.createPoint(new Coordinate(120.301,35.086));
Point point2 = geometryFactory.createPoint(new Coordinate(120.301,35.076));
Point point3 = geometryFactory.createPoint(new Coordinate(120.301,35.066));
// ポイントの SimpleFeatures を構築します。
List<SimpleFeature> features=new ArrayList<>();
features.add(builder.buildFeature("1", new Object[]{"point1",new Date(),point1}));
features.add(builder.buildFeature("2", new Object[]{"point2",new Date(),point2}));
features.add(builder.buildFeature("3", new Object[]{"point3",new Date(),point3}));
// SimpleFeature コンテンツをインポートします。
SimpleFeatureWriter writer=(SimpleFeatureWriter)ds.getFeatureWriterAppend(sft_name, Transaction.AUTO_COMMIT);
for(SimpleFeature feature:features){
SimpleFeature toWrite=writer.next();
toWrite.setAttributes(feature.getAttributes());
toWrite.getUserData().putAll(feature.getUserData());
writer.write();
}
writer.close();
// 時空間クエリ条件を構築します。
long t1=format.parse("2019-01-19 11:45:00").getTime();
long t2=format.parse("2019-02-21 12:15:00").getTime();
String sortField="dtg"; // ソートフィールドを指定します。この例では、データは日付でソートされます。
FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2();
SortBy[] sort = new SortBy[]{ff.sort(sortField, SortOrder.DESCENDING)};
// クエリ オブジェクトを構築します。
Query query = new Query(sft_name, ECQL.toFilter( "bbox(geom,120,20,130,40) AND dtg >= "+t1+" AND dtg <= "+t2));
query.setSortBy(sort);
SimpleFeatureCollection result=ds.getFeatureSource(sft_name).getFeatures(query);
SimpleFeatureIterator iterator=result.features();
// クエリ結果を出力します。
long sum = 0;
while (iterator.hasNext()) {
System.out.println(iterator.next());
sum++;
}
System.out.println("クエリ合計数:" + sum);
}
catch (Exception e){
e.printStackTrace();
}
}
}
上記のスクリプトを正常に実行すると、3 つの「Point」SimpleFeatures が出力されます。クエリ結果は次のとおりです: