云数据库 HBase 快速入门

By | 2021年4月22日

本章目的在于通过完整的代码实例帮助用户快速搭建ganos开发环境进行测试,从而快速掌握简单的Ganos时空操作,包括连接数据库、创建索引、数据入库、查询等。

依赖包:

  
  1. <dependencies>
  2. <dependency>
  3. <groupId>org.locationtech.geomesa</groupId>
  4. <artifactId>geomesa-hbase-datastore_2.11</artifactId>
  5. <version>${geomesa.version}</version>
  6. <exclusions>
  7. <exclusion>
  8. <groupId>org.apache.hbase</groupId>
  9. <artifactId>hbase-client</artifactId>
  10. </exclusion>
  11. <exclusion>
  12. <groupId>org.apache.hbase</groupId>
  13. <artifactId>hbase-server</artifactId>
  14. </exclusion>
  15. <exclusion>
  16. <groupId>org.apache.hbase</groupId>
  17. <artifactId>hbase-common</artifactId>
  18. </exclusion>
  19. <exclusion>
  20. <groupId>org.apache.hbase</groupId>
  21. <artifactId>hbase-protocol</artifactId>
  22. </exclusion>
  23. </exclusions>
  24. </dependency>
  25. <dependency>
  26. <groupId>com.aliyun.hbase</groupId>
  27. <artifactId>alihbase-client</artifactId>
  28. <version>${hbase.version}</version>
  29. <exclusions>
  30. <exclusion>
  31. <artifactId>com.google.guava</artifactId>
  32. <groupId>guava</groupId>
  33. </exclusion>
  34. </exclusions>
  35. </dependency>
  36. <dependency>
  37. <groupId>com.aliyun.hbase</groupId>
  38. <artifactId>alihbase-server</artifactId>
  39. <version>${hbase.version}</version>
  40. </dependency>
  41. <dependency>
  42. <groupId>com.aliyun.hbase</groupId>
  43. <artifactId>alihbase-common</artifactId>
  44. <version>${hbase.version}</version>
  45. </dependency>
  46. <dependency>
  47. <groupId>com.aliyun.hbase</groupId>
  48. <artifactId>alihbase-protocol</artifactId>
  49. <version>${hbase.version}</version>
  50. </dependency>
  51. <dependency>
  52. <groupId>com.alibaba</groupId>
  53. <artifactId>fastjson</artifactId>
  54. <version>1.2.49</version>
  55. </dependency>
  56. </dependencies>
  57. <build>
  58. <plugins>
  59. <plugin>
  60. <inherited>true</inherited>
  61. <groupId>org.apache.maven.plugins</groupId>
  62. <artifactId>maven-compiler-plugin</artifactId>
  63. <configuration>
  64. <source>1.8</source>
  65. <target>1.8</target>
  66. </configuration>
  67. </plugin>
  68. </plugins>
  69. </build>

示例代码以及说明

  
  1. package com.aliyun.tst;
  2. import com.vividsolutions.jts.geom.Coordinate;
  3. import com.vividsolutions.jts.geom.GeometryFactory;
  4. import com.vividsolutions.jts.geom.Point;
  5. import org.geotools.data.DataStore;
  6. import org.geotools.data.DataStoreFinder;
  7. import org.geotools.data.Query;
  8. import org.geotools.data.Transaction;
  9. import org.geotools.data.simple.SimpleFeatureCollection;
  10. import org.geotools.data.simple.SimpleFeatureIterator;
  11. import org.geotools.data.simple.SimpleFeatureWriter;
  12. import org.geotools.factory.CommonFactoryFinder;
  13. import org.geotools.factory.Hints;
  14. import org.geotools.feature.simple.SimpleFeatureBuilder;
  15. import org.geotools.filter.text.ecql.ECQL;
  16. import org.geotools.geometry.jts.JTSFactoryFinder;
  17. import org.locationtech.geomesa.utils.geotools.SimpleFeatureTypes;
  18. import org.opengis.feature.Feature;
  19. import org.opengis.feature.simple.SimpleFeature;
  20. import org.opengis.feature.simple.SimpleFeatureType;
  21. import org.opengis.filter.FilterFactory2;
  22. import org.opengis.filter.sort.SortBy;
  23. import org.opengis.filter.sort.SortOrder;
  24. import java.io.BufferedReader;
  25. import java.io.FileReader;
  26. import java.text.SimpleDateFormat;
  27. import java.util.*;
  28. public class Demo {
  29. public static final String ZK = "localhost"; //Zookeeper地址
  30. public static void main(String args[]){
  31. try{
  32. DataStore ds=null;
  33. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd' 'HH:mm:ss");
  34. //配置连接参数
  35. Map<String, String> params= new HashMap<>();
  36. params.put("hbase.zookeepers",ZK);
  37. params.put("hbase.catalog","test_catalog");
  38. //初始化DataStore
  39. ds= DataStoreFinder.getDataStore(params);
  40. //创建SimpleFeatureType定义表结构
  41. String sft_name="point";
  42. SimpleFeatureType sft=
  43. SimpleFeatureTypes.createType(sft_name, "name:String,dtg:Date,*geom:Point:srid=4326");
  44. //指定压缩方式gz
  45. sft.getUserData().put("geomesa.table.compression.enabled", "true");
  46. sft.getUserData().put("geomesa.table.compression.type", "gz");
  47. //创建数据表
  48. ds.createSchema(sft);
  49. /*
  50. * GeometryFactory 用来创建空间对象
  51. */
  52. GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
  53. SimpleFeatureBuilder builder = new SimpleFeatureBuilder(sft);
  54. //构造空间数据(点)
  55. Point point1 = geometryFactory.createPoint(new Coordinate(120.301,35.086));
  56. Point point2 = geometryFactory.createPoint(new Coordinate(120.301,35.076));
  57. Point point3 = geometryFactory.createPoint(new Coordinate(120.301,35.066));
  58. //构造点SimpleFeature
  59. List<SimpleFeature> features=new ArrayList<>();
  60. features.add(builder.buildFeature("1", new Object[]{"point1",new Date(),point1}));
  61. features.add(builder.buildFeature("2", new Object[]{"point2",new Date(),point2}));
  62. features.add(builder.buildFeature("3", new Object[]{"point3",new Date(),point3}));
  63. //要素入库
  64. SimpleFeatureWriter writer=(SimpleFeatureWriter)ds.getFeatureWriterAppend(sft_name, Transaction.AUTO_COMMIT);
  65. for(SimpleFeature feature:features){
  66. SimpleFeature toWrite=writer.next();
  67. toWrite.setAttributes(feature.getAttributes());
  68. toWrite.getUserData().putAll(feature.getUserData());
  69. writer.write();
  70. }
  71. writer.close();
  72. //构造时空查询条件
  73. long t1=format.parse("2019-01-19 11:45:00").getTime();
  74. long t2=format.parse("2019-02-21 12:15:00").getTime();
  75. String sortField="dtg";//排序字段,这里设为时间
  76. FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2();
  77. SortBy[] sort = new SortBy[]{ff.sort(sortField, SortOrder.DESCENDING)};
  78. //构造Query对象用于查询
  79. Query query = new Query(sft_name, ECQL.toFilter( "bbox(geom,120,20,130,40) AND dtg >= "+t1+" AND dtg <= "+t2));
  80. query.setSortBy(sort);
  81. SimpleFeatureCollection result=ds.getFeatureSource(sft_name).getFeatures(query);
  82. SimpleFeatureIterator iterator=result.features();
  83. //输出查询结果
  84. long sum = 0;
  85. while (iterator.hasNext()) {
  86. System.out.println(iterator.next());
  87. sum++;
  88. }
  89. System.out.println("查询总数:" + sum);
  90. }
  91. catch (Exception e){
  92. e.printStackTrace();
  93. }
  94. }
  95. }

运行成功后可以看到入库的三个点要素被打印出来,运行结果如下:result

请关注公众号获取更多资料

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注