为了更好地进行GeoMesa的测试,这里提供了一个最简化版本的Demo,同样,在之后章节里面可能会出现一些数据写入的例子,同样是以这个Demo为依据的。
在这个Demo当中,只生成了一行数据,其中包括taxiId(String)、时间数据(Date)、空间数据(Point)、description(String),简化的数据可以加快实验速度。SimpleFeatureType的名称为index-text02,vatalog的名称为“test”。
- public class IODemo01 {
-
- /**
- * 这个方法主要设定了表名"index-text02",
- * 和schema结构"taxiId:String,dtg:Date,*geom:Point:srid=4326"
- * @return SimpleFeatureType,即建表的schema表结构
- */
- public SimpleFeatureType getSimpleFeatureType() {
- SimpleFeatureType sft = SimpleFeatureTypes.createType("index-text02", "taxiId:String,dtg:Date,*geom:Point:srid=4326,description:String");
- sft.getUserData().put(SimpleFeatureTypes.DEFAULT_DATE_KEY, "dtg");
- return sft;
- }
-
- public static void main(String[] args) {
-
- Map<String, String> params = new HashMap<>();
- IODemo01 demo01 = new IODemo01();
-
- try {
- //创建datastore
- params.put("hbase.catalog", "test");
- params.put("hbase.zookeepers", "xxx.xxx.xxx.xx:2181");
-
- DataStore datastore = DataStoreFinder.getDataStore(params);
-
- // 创建schema
- SimpleFeatureType sft = demo01.getSimpleFeatureType();
- System.out.println(sft);
- System.out.println(datastore);
- datastore.createSchema(sft);
-
- //获取Features
- SimpleFeature feature = demo01.getData();
-
- //写入Features
- demo01.writeFeature(datastore, sft, feature);
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- }
-
- /**
- * 这个方法主要用来将封装好的feature存入到对应的datastore中
- * @param datastore 数据源,可以选择的有很多,此处是HBaseDataStore
- * @param sft 表结构
- * @param feature 封装好的一行数据
- */
- private void writeFeature(DataStore datastore, SimpleFeatureType sft, SimpleFeature feature) {
- try {
- System.out.println("write test data");
- FeatureWriter<SimpleFeatureType, SimpleFeature> writer =
- datastore.getFeatureWriterAppend(sft.getTypeName(), Transaction.AUTO_COMMIT);
- SimpleFeature toWrite = writer.next();
- toWrite.setAttributes(feature.getAttributes());
- ((FeatureIdImpl) toWrite.getIdentifier()).setID(feature.getID());
- toWrite.getUserData().put(Hints.USE_PROVIDED_FID, Boolean.TRUE);
- toWrite.getUserData().putAll(feature.getUserData());
- writer.write();
- // 关闭流
- writer.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
-
- }
-
- /**
- * 这个方法主要是将非结构化的数据转换为feature对象
- * @return feature对象
- */
- private SimpleFeature getData() {
- SimpleFeatureBuilder builder = new SimpleFeatureBuilder(getSimpleFeatureType());
- DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss", Locale.US);
- builder.set("taxiId", "黑A SA2342");
- builder.set("dtg", Date.from(LocalDateTime.parse("2008-02-02 13:30:49", dateFormat).toInstant(ZoneOffset.UTC)));
- builder.set("geom", "POINT (116.31412 39.89454)");
- builder.set("description", "这是一辆套牌的车");
- builder.featureUserData(Hints.USE_PROVIDED_FID, Boolean.TRUE);
- return builder.buildFeature("黑A SA2342");
- }
- }