前言
完成geoMesa的安装之后,本人尝试使用java完成hbase的表创建、数据插入和读取工作。在这个过程中遇到了很多问题,查了很多资料,试了很多方法,终于实现了。其实出现这些问题都是没有导入正确jar包的原因。这里需要特别强调下,geoMesa和geotool的版本是对应的,是对应的,是对应的,重要的事情说三遍。关于版本对应问题,参考官网的pom文件。
https://github.com/locationtech/geomesa/blob/2.0.x/pom.xml
代码
XML文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.fr</groupId>
<artifactId>geomesa5</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>geomesa</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<geotools.version>20.0</geotools.version>
<hbase.version>1.3.5</hbase.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-main</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-api</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-cql</artifactId>
<version>23.0</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-grid</artifactId>
<version>23.0</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-render</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-data</artifactId>
<version>${geotools.version}</version>
</dependency>
<!--// https://mvnrepository.com/artifact/org.locationtech.geomesa/geomesa-hbase-datastore-->
<dependency>
<groupId>org.locationtech.geomesa</groupId>
<artifactId>geomesa-hbase-datastore_2.11</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-protocol</artifactId>
<version>${hbase.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>${hbase.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-common</artifactId>
<version>${hbase.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>${hbase.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-annotations</artifactId>
<version>${hbase.version}</version>
<scope>runtime</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<id>osgeo</id>
<name>Open Source Geospatial Foundation Repository</name>
<url>http://download.osgeo.org/webdav/geotools/</url>
</repository>
</repositories>
</project>
创建表并写入数据
package cn.fr.geomesa5;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFinder;
import org.geotools.data.FeatureWriter;
import org.geotools.data.Transaction;
import org.geotools.factory.Hints;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.filter.identity.FeatureIdImpl;
import org.locationtech.geomesa.utils.geotools.SimpleFeatureTypes;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
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");
//设置xz3索引时间间隔为天
sft.getUserData().put("geomesa.xz3.interval", "day");
//设置时空索引时间字段为date
sft.getUserData().put("geomesa.index.dtg", "dateAttr");
//设置索引精度
sft.getUserData().put("geomesa.xz.precision", 10);
return sft;
}
public static void main(String[] args) {
Map<String, String> params = new HashMap<String,String>();
IODemo01 demo01 = new IODemo01();
try {
//创建datastore
params.put("hbase.catalog", "demo");
params.put("hbase.zookeepers", "localhost");
DataStore datastore = DataStoreFinder.getDataStore(params);
// 创建schema
SimpleFeatureType sft = demo01.getSimpleFeatureType();
System.out.println(sft);
//sft.getUserData().put("geomesa.mixed.geometries",Boolean.parseBoolean(true));
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");
}
}
读取数据
package cn.fr.geomesa5;
import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFinder;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.opengis.feature.Property;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.feature.type.AttributeDescriptor;
import java.io.IOException;
import java.util.*;
/**
* @author
* @DesktopJavaDocable disable
*/
public class DataStoreHbaseDemo {
private static String catalog = "demo";
//集群
private static String zookeepers = "localhost";
private Map getParams() {
Map params = new HashMap();
params.put("hbase.zookeepers", zookeepers);
params.put("hbase.catalog", catalog);
return params;
}
public void printfHbase() {
try {
DataStore dataStore = DataStoreFinder.getDataStore(getParams());
System.out.println(dataStore);
if (dataStore != null) {
//获取Catalog下所有的数据表
String[] typeNames = dataStore.getTypeNames();
if (typeNames.length > 0) {
//获取第一张数据表的数据信息
SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeNames[0]);
if (featureSource != null) {
//获取该数据表的属性信息
SimpleFeatureType featureType = featureSource.getSchema();
System.out.println("表名:" + featureType.getTypeName());
System.out.println("字段数:" + featureType.getAttributeCount());
//获取数据表的属性(字段)结构
List<AttributeDescriptor> attributeDescriptors = featureType.getAttributeDescriptors();
for (AttributeDescriptor attributeDescriptor : attributeDescriptors) {
System.out.println("字段名:" + attributeDescriptor.getLocalName());
}
//获取数据表的记录信息
SimpleFeatureCollection features = featureSource.getFeatures();
SimpleFeatureIterator featureIterator = features.features();
//打印前10条记录
int counter = 0;
while (featureIterator.hasNext()) {
//一条记录集
SimpleFeature simpleFeature = featureIterator.next();
//获取记录集信息
Collection<Property> properties = simpleFeature.getProperties();
Iterator<Property> iterator = properties.iterator();
while (iterator.hasNext()) {
Property property = iterator.next();
System.out.println(property.getName().getLocalPart() + ":" + property.getValue().toString());
}
if (counter > 10) {
break;
}
counter++;
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
DataStoreHbaseDemo dataStoreHbaseDemo = new DataStoreHbaseDemo();
dataStoreHbaseDemo.printfHbase();
}
}
代码参考网址:
1、https://blog.csdn.net/weixin_41834634/article/details/89184113?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase
2、https://www.cnblogs.com/mohanchen/p/10827878.html