Geomesa 4.0.4能否通过Java API实现Accumulo数据导入(替代SSH)
Geomesa 4.0.4 Java 数据导入方案解答
1. 官方导入API是否仍可用?
是的,Geomesa 4.0.4仍保留了Java导入API,但3.x版本后对核心API做了重构,2.0.2的旧示例代码因包结构、类名变化无法直接复用。
2. Java端数据导入方案
方案一:使用官方Ingest API
可以通过org.locationtech.geomesa.tools.ingest.IngestRunner类直接调用导入逻辑,对应命令行geomesa-accumulo ingest的底层实现。核心步骤:
- 构建Accumulo数据连接参数
- 配置导入任务(指定要素类型、数据源路径等)
- 执行导入
示例代码片段:
import org.locationtech.geomesa.accumulo.data.AccumuloDataStoreFactory; import org.locationtech.geomesa.tools.ingest.IngestRunner; import org.locationtech.geomesa.tools.ingest.IngestCommand; import java.util.Map; public class GeomesaIngestExample { public static void main(String[] args) throws Exception { // 构建Accumulo连接参数 Map<String, String> params = Map.of( "instanceId", "accumulo", "zookeepers", "zookeeper", "user", "root", "password", "GisPwd", "tableName", "example" ); // 模拟命令行参数,对应原命令的参数 String[] ingestArgs = { "--force", "-c", "example", "-f", "example", "example.shp" }; // 创建Ingest命令并执行 IngestCommand command = new IngestCommand(params, ingestArgs); IngestRunner.run(command); } }
方案二:直接使用DataStore API手动导入
如果不想依赖tools模块,可直接通过DataStore的addFeatures方法批量插入数据,步骤如下:
- 创建AccumuloDataStore实例
- 读取Shapefile数据源(使用GeoTools的
ShapefileDataStore) - 获取或创建目标要素类型(SimpleFeatureType)
- 批量写入要素到Accumulo
示例代码片段:
import org.geotools.data.DataStore; import org.geotools.data.DataStoreFinder; import org.geotools.data.shapefile.ShapefileDataStoreFactory; import org.locationtech.geomesa.accumulo.data.AccumuloDataStore; import org.locationtech.geomesa.accumulo.data.AccumuloDataStoreFactory; import org.opengis.feature.simple.SimpleFeature; import org.opengis.feature.simple.SimpleFeatureType; import java.io.File; import java.util.Map; public class DirectIngestExample { public static void main(String[] args) throws Exception { // 1. 初始化Accumulo DataStore Map<String, String> accumuloParams = Map.of( "instanceId", "accumulo", "zookeepers", "zookeeper", "user", "root", "password", "GisPwd", "tableName", "example" ); AccumuloDataStore accumuloStore = (AccumuloDataStore) DataStoreFinder.getDataStore(accumuloParams); // 2. 读取Shapefile File shapefile = new File("example.shp"); Map<String, Object> shpParams = Map.of( ShapefileDataStoreFactory.URL.key, shapefile.toURI().toURL() ); DataStore shpStore = DataStoreFinder.getDataStore(shpParams); SimpleFeatureType shpSft = shpStore.getSchema("example"); // 3. 在Accumulo中创建要素类型(如果不存在) if (!accumuloStore.getSchema("example").isPresent()) { accumuloStore.createSchema(shpSft); } // 4. 批量写入要素 try (var reader = shpStore.getFeatureReader(shpStore.getQuery("example"), null); var writer = accumuloStore.getFeatureWriterAppend("example", null)) { SimpleFeature feature; while ((feature = reader.next()) != null) { SimpleFeature toWrite = writer.next(); toWrite.setAttributes(feature.getAttributes()); writer.write(); } } // 关闭资源 shpStore.dispose(); accumuloStore.dispose(); } }
3. 是否可替代SSH命令?
完全可以。上述两种方案都无需通过SSH调用容器内的命令行工具,直接在Java应用中通过API完成和原命令geomesa-accumulo ingest --force -i accumulo -z zookeeper -u root -p GisPwd -c example -f example example.shp完全一致的导入操作。
注意事项
- 确保Java应用的依赖版本与Geomesa 4.0.4匹配,需引入
geomesa-accumulo-datastore、geomesa-shapefile、geomesa-tools(方案一需要)等相关依赖 - 若应用与Accumulo集群不在同一网络,需确保网络连通性,ZooKeeper地址可正常访问
内容的提问来源于stack exchange,提问作者Luigi
相关产品推荐
相关产品推荐

