开发者社区 > 博文 > GeoMesa-HBase原理篇——最简demo
分享
  • 打开微信扫码分享

  • 点击前往QQ分享

  • 点击前往微博分享

  • 点击复制链接

GeoMesa-HBase原理篇——最简demo

  • 京东城市JUST团队
  • 2021-01-22
  • IP归属:未知
  • 28720浏览

为了更好地进行GeoMesa的测试,这里提供了一个最简化版本的Demo,同样,在之后章节里面可能会出现一些数据写入的例子,同样是以这个Demo为依据的。

在这个Demo当中,只生成了一行数据,其中包括taxiId(String)、时间数据(Date)、空间数据(Point)、description(String),简化的数据可以加快实验速度。SimpleFeatureType的名称为index-text02,vatalog的名称为“test”。

  1. public class IODemo01 {
  2. /**
  3. * 这个方法主要设定了表名"index-text02",
  4. * 和schema结构"taxiId:String,dtg:Date,*geom:Point:srid=4326"
  5. * @return SimpleFeatureType,即建表的schema表结构
  6. */
  7. public SimpleFeatureType getSimpleFeatureType() {
  8. SimpleFeatureType sft = SimpleFeatureTypes.createType("index-text02", "taxiId:String,dtg:Date,*geom:Point:srid=4326,description:String");
  9. sft.getUserData().put(SimpleFeatureTypes.DEFAULT_DATE_KEY, "dtg");
  10. return sft;
  11. }
  12. public static void main(String[] args) {
  13. Map<String, String> params = new HashMap<>();
  14. IODemo01 demo01 = new IODemo01();
  15. try {
  16. //创建datastore
  17. params.put("hbase.catalog", "test");
  18. params.put("hbase.zookeepers", "xxx.xxx.xxx.xx:2181");
  19. DataStore datastore = DataStoreFinder.getDataStore(params);
  20. // 创建schema
  21. SimpleFeatureType sft = demo01.getSimpleFeatureType();
  22. System.out.println(sft);
  23. System.out.println(datastore);
  24. datastore.createSchema(sft);
  25. //获取Features
  26. SimpleFeature feature = demo01.getData();
  27. //写入Features
  28. demo01.writeFeature(datastore, sft, feature);
  29. } catch (Exception e) {
  30. e.printStackTrace();
  31. }
  32. }
  33. /**
  34. * 这个方法主要用来将封装好的feature存入到对应的datastore中
  35. * @param datastore 数据源,可以选择的有很多,此处是HBaseDataStore
  36. * @param sft 表结构
  37. * @param feature 封装好的一行数据
  38. */
  39. private void writeFeature(DataStore datastore, SimpleFeatureType sft, SimpleFeature feature) {
  40. try {
  41. System.out.println("write test data");
  42. FeatureWriter<SimpleFeatureType, SimpleFeature> writer =
  43. datastore.getFeatureWriterAppend(sft.getTypeName(), Transaction.AUTO_COMMIT);
  44. SimpleFeature toWrite = writer.next();
  45. toWrite.setAttributes(feature.getAttributes());
  46. ((FeatureIdImpl) toWrite.getIdentifier()).setID(feature.getID());
  47. toWrite.getUserData().put(Hints.USE_PROVIDED_FID, Boolean.TRUE);
  48. toWrite.getUserData().putAll(feature.getUserData());
  49. writer.write();
  50. // 关闭流
  51. writer.close();
  52. } catch (IOException e) {
  53. e.printStackTrace();
  54. }
  55. }
  56. /**
  57. * 这个方法主要是将非结构化的数据转换为feature对象
  58. * @return feature对象
  59. */
  60. private SimpleFeature getData() {
  61. SimpleFeatureBuilder builder = new SimpleFeatureBuilder(getSimpleFeatureType());
  62. DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss", Locale.US);
  63. builder.set("taxiId", "黑A SA2342");
  64. builder.set("dtg", Date.from(LocalDateTime.parse("2008-02-02 13:30:49", dateFormat).toInstant(ZoneOffset.UTC)));
  65. builder.set("geom", "POINT (116.31412 39.89454)");
  66. builder.set("description", "这是一辆套牌的车");
  67. builder.featureUserData(Hints.USE_PROVIDED_FID, Boolean.TRUE);
  68. return builder.buildFeature("黑A SA2342");
  69. }
  70. }
共0条评论