Quick start

Updated at:

Set up an HBase Ganos test environment with a complete code example that covers basic spatio-temporal operations, including connecting to a database, creating an index, ingesting data, and running a query.

Add the following dependencies to your project.
<properties>
    <geomesa.version>3.1.1</geomesa.version>
    <hbase.version>1.8.0</hbase.version>
</properties>
<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>
The following sample code is annotated with inline comments.
package com.aliyun.tst;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.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.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.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.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Demo {
  public static final String ZK = "localhost"; // Zookeeper address
  public static void main(String args[]){
    try{
      DataStore ds=null;
      SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd' 'HH:mm:ss");
      // Configure the connection parameters.
      Map<String, String> params= new HashMap<>();
      params.put("hbase.zookeepers",ZK);
      params.put("hbase.catalog","test_catalog");
      // Initialize the DataStore.
      ds= DataStoreFinder.getDataStore(params);
      // Create a SimpleFeatureType to define the schema.
      String sft_name="point";
      SimpleFeatureType sft=
              SimpleFeatureTypes.createType(sft_name, "name:String,dtg:Date,*geom:Point:srid=4326");
      // Specify the compression type as gz.
      sft.getUserData().put("geomesa.table.compression.enabled", "true");
      sft.getUserData().put("geomesa.table.compression.type", "gz");
      // Create the table.
      ds.createSchema(sft);
      /*
       * The GeometryFactory is used to create geometries.
       */
      GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
      SimpleFeatureBuilder builder = new SimpleFeatureBuilder(sft);
      // Construct geometry data (points).
      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));
      // Construct point 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}));
      // Ingest the features.
      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();
      // Construct the spatio-temporal query conditions.
      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";// The sort field, set to time here.
      FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2();
      SortBy[] sort = new SortBy[]{ff.sort(sortField, SortOrder.DESCENDING)};
      // Construct the Query object.
      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();
      // Print the query results.
      long sum = 0;
      while (iterator.hasNext()) {
        System.out.println(iterator.next());
        sum++;
      }
      System.out.println("Total results: " + sum);
    }
    catch (Exception e){
      e.printStackTrace();
    }
  }
}
After the code runs successfully, the output lists the three ingested point features:
ScalaSimpleFeature:e0a003f9-fbff-4c13-b03f-243b7924ba3c:point3|Sat Jan 26 12:20:14 CST 2019|POINT (120.301 35.066)
ScalaSimpleFeature:90a003f9-fbff-4c81-bc24-7b0532aefcba:point2|Sat Jan 26 12:20:14 CST 2019|POINT (120.301 35.076)
ScalaSimpleFeature:f0a003f9-fbff-4c81-8b23-fb9df9dfa029:point1|Sat Jan 26 12:20:14 CST 2019|POINT (120.301 35.086)
Total results: 3
Process finished with exit code 0