zoukankan      html  css  js  c++  java
  • 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;

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

      

      

    • 打印结果

      

  • 相关阅读:
    PHP学习笔记:APACHE配置虚拟目录、一个站点使用多域名配置方式
    转载:分页原理+分页代码+分页类制作
    PHP学习笔记:数据库学习心得
    PHP学习笔记:用mysqli连接数据库
    PHP学习笔记:MySQL数据库的操纵
    PHP学习笔记:利用时间和mt_rand函数获取随机名字
    PHP学习笔记:等比例缩放图片
    前端学习(一) html介绍和head标签
    Python 协程
    Python 线程----线程方法,线程事件,线程队列,线程池,GIL锁,协程,Greenlet
  • 原文地址:https://www.cnblogs.com/mohanchen/p/10827878.html
Copyright © 2011-2022 走看看