Geotools系列之Geotools连接Hbase数据库并读取数据
本文主要讲通过GeoTools API 连接Hbase数据库,并且获得数据
- 添加pom依赖
<properties> <geotools.version>20.0</geotools.version> <hbase.version>1.4.5</hbase.version> </properties> <dependencies> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-main</artifactId> <version>${geotools.version}</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>
- Demo代码
package org.geotools.tutorial.quickstart.demo; 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 = "China"; //集群 private static String zookeepers = "172.16.18.8:2181"; 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()); 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(); } } }
关于Hbase数连接参数的说明:
hbase.catalog为Hbase数据库meta信息表,为必填参数;
hbase.zookeepers为Hbase数据zk地址,如果是本机的Hbase数据库可以不设置,不设置的默认值为localhost;
还有一些选填参数,可以根据需求设置:
- 打印结果
版权声明:本文为mohanchen原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。