Cassandra集成GeoMesa:如何插入Point类型坐标数据?
解决GeoMesa Cassandra插入GIS数据的报错问题
问题根源
你使用的Cassandra 3.0.29原生不支持Point地理类型,该类型是GeoMesa扩展提供的专用类型,必须先部署GeoMesa Cassandra插件,再通过GeoMesa的工具/API创建地理表,不能直接用原生CQL创建。另外你创建Keyspace的CQL存在语法错误(将逗号误写为点)。
分步解决方案
1. 修复Keyspace创建语句
先执行正确的Keyspace创建命令:
CREATE KEYSPACE geomesa WITH REPLICATION = {'class': 'SimpleStrategy', 'replication_factor': 3}; USE geomesa;
2. 安装GeoMesa Cassandra插件
GeoMesa需要通过插件扩展Cassandra的地理类型支持:
- 确认版本兼容:GeoMesa 3.x系列适配Cassandra 3.x(比如GeoMesa 3.4支持Cassandra 3.0+)
- 下载对应版本的GeoMesa Cassandra二进制包,解压后将
geomesa-cassandra-distributed-runtime-*.jar复制到所有Cassandra节点的lib目录 - 重启所有Cassandra节点,使插件生效
3. 创建带Point类型的表(两种方式)
方式1:用GeoMesa命令行工具创建
- 创建配置文件
conf/application.conf,填写Cassandra连接信息:
geomesa.cassandra.contact.points = "127.0.0.1:9042" geomesa.cassandra.keyspace = "geomesa" # 有认证需求则添加以下两行 # geomesa.cassandra.username = "your-username" # geomesa.cassandra.password = "your-password"
- 执行创建Schema命令:
geomesa-cassandra create-schema -c conf/application.conf -f test -s "point:Point:srid=4326"
参数说明:-f指定要素表名,-s定义字段(point:Point表示字段名为point、类型为Point;srid=4326指定WGS84坐标系)
方式2:用GeoMesa Java API创建
import org.locationtech.geomesa.cassandra.data.CassandraDataStore; import org.locationtech.geomesa.cassandra.data.CassandraDataStoreFactory; import org.locationtech.geomesa.utils.geotools.SimpleFeatureTypes; import org.opengis.feature.simple.SimpleFeatureType; import org.geotools.data.DataStoreFinder; import java.util.HashMap; import java.util.Map; public class CreateGeoTable { public static void main(String[] args) { Map<String, String> params = new HashMap<>(); params.put(CassandraDataStoreFactory.CONTACT_POINTS.key, "127.0.0.1:9042"); params.put(CassandraDataStoreFactory.KEYSPACE.key, "geomesa"); // 定义要素类型:字段point为Point类型,坐标系WGS84 SimpleFeatureType sft = SimpleFeatureTypes.createType("test", "point:Point:srid=4326"); try (CassandraDataStore dataStore = (CassandraDataStore) DataStoreFinder.getDataStore(params)) { if (dataStore == null) { throw new RuntimeException("Failed to connect to Cassandra data store"); } dataStore.createSchema(sft); System.out.println("Table created successfully"); } } }
4. 插入Point类型数据(两种方式)
方式1:用GeoMesa命令行批量导入
- 准备CSV文件
data.csv:
point POINT(116.397222 39.9075) POINT(121.473701 31.230416)
- 执行导入命令:
geomesa-cassandra ingest -c conf/application.conf -f test -s "point:Point:srid=4326" data.csv
方式2:用CQL单条插入
插件生效后,可直接在cqlsh中用WKT(Well-Known Text)格式插入:
USE geomesa; INSERT INTO test (point) VALUES ('POINT(116.397222 39.9075)');
也可以用WKB(Well-Known Binary)格式插入:
INSERT INTO test (point) VALUES (0x01010000005EC3F5285C8F5D40789406819D444340);
内容的提问来源于stack exchange,提问作者robtot
相关产品推荐
相关产品推荐

