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;

  还有一些选填参数,可以根据需求设置:

  

 

  

  • 打印结果

  

 

转载于:https://www.cnblogs.com/mohanchen/p/10827878.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值