GDAL获取Hadoop hdfs tif文件信息(java)
系统版本
系统Centos7
软件版本
hadoop-3.1.2
jdk-8u221
curl-7.64.1
gdal-2.4.0
apache-ant-1.10.5-bin
-
安装JDK
-
卸载原系统的openJDK
#查看java版本 java -version #查看 openjdk 相关安装包 rpm -qa | grep openjdk #执行批量删除命令,删除openjdk rpm -qa | grep openjdk |xargs rpm -e --nodeps #再次查看 openjdk 相关安装包 rpm -qa | grep openjdk #查看java版本 java -version
如果已经查看不到java版本,则说明已经卸载
-
安装jdk-8u221
# 进入home路径下 cd /home # 创建jdk文件夹,用于存放jdk mkdir jdk # 将下载的jdk-8u221-linux-x64.tar.gz放入该目录并解压 tar -zxvf jdk-8u221-linux-x64.tar.gz
#配置jdk环境变量 vim /etc/profile #写入环境变量 export JAVA_HOME=/home/jdk/jdk1.8.0_221 export CLASSPATH=.:$JAVA_HOME/jre/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar export PATH=$PATH:$JAVA_HOME/bin #使环境变量生效 source /etc/profile #查看Java版本信息 是否是我们配置的jdk 是否配置成功 java -version
-
ssh免密码登录
-
安装hadoop
# 创建目录/home/hadoop,并将hadoop-3.1.2.tar.gz放在该目录下 cd /home mkdir hadoop # 解压hadoop-3.1.2.tar.gz tar -zxvf hadoop-3.1.2.tar.gz # 在/home/hadoop目录下创建数据存放的文件夹 tmp、hdfs、hdfs/data、hdfs/name mkdir tmp hdfs hdfs/data hdfs/name
-
hadoop文件配置(/home/hadoop/hadoop-3.1.2/etc/hadoop)
-
core-site.xml
<configuration> <property> <name>fs.defaultFS</name> <value>hdfs://192.168.126.128:9000</value><!--NameNode的URL,hdfs://主机名:端口/--> </property> <property> <name>hadoop.tmp.dir</name><!--Hadoop的默认临时路径,如果在新增节点或者其他情况下莫名其妙的DataNode启动不了,就删除此文件中的tmp目录即可。如果删除了NameNode机器的此目录,那么需要重新执行NameNode格式化命令--> <value>file:/home/hadoop/tmp</value> </property> <property> <name>io.file.buffer.size</name> <value>131702</value> </property> </configuration>
-
hdfs-site.xml
<configuration> <property> <name>dfs.namenode.name.dir</name> <value>file:/home/hadoop/hdfs/name</value> </property> <property> <name>dfs.datanode.data.dir</name> <value>file:/home/hadoop/hdfs/data</value> </property> <property> <name>dfs.replication</name> <value>2</value> </property> <property> <name>dfs.namenode.secondary.http-address</name> <value>192.168.126.128:9001</value> </property> <property> <name>dfs.webhdfs.enabled</name> <value>true</value> </property> </configuration>
-
mapred-site.xml
<configuration> <property> <name>mapreduce.framework.name</name> <value>yarn</value> </property> <property> <name>mapreduce.jobhistory.address</name> <value>192.168.126.128:10020</value> </property> <property> <name>mapreduce.jobhistory.webapp.address</name> <value>192.168.126.128:19888</value> </property> </configuration>
-
yarn-site.xml
<configuration> <!-- Site specific YARN configuration properties --> <property> <name>yarn.nodemanager.aux-services</name> <value>mapreduce_shuffle</value> </property> <property> <name>yarn.nodemanager.auxservices.mapreduce.shuffle.class</name> <value>org.apache.hadoop.mapred.ShuffleHandler</value> </property> <property> <name>yarn.resourcemanager.address</name> <value>192.168.126.128:8032</value> </property> <property> <name>yarn.resourcemanager.scheduler.address</name> <value>192.168.126.128:8030</value> </property> <property> <name>yarn.resourcemanager.resource-tracker.address</name> <value>192.168.126.128:8031</value> </property> <property> <name>yarn.resourcemanager.admin.address</name> <value>192.168.126.128:8033</value> </property> <property> <name>yarn.resourcemanager.webapp.address</name> <value>192.168.126.128:8088</value> </property> <property> <name>yarn.nodemanager.resource.memory-mb</name> <value>20480</value><!--这里768需要大些20480,不然有问题,后续查找为什么--> </property> <property> <name>yarn.application.classpath</name> <value>/home/hadoop/hadoop-3.1.2/etc/hadoop:/home/hadoop/hadoop-3.1.2/share/hadoop/common/lib/*:/home/hadoop/hadoop-3.1.2/share/hadoop/common/*:/home/hadoop/hadoop-3.1.2/share/hadoop/hdfs:/home/hadoop/hadoop-3.1.2/share/hadoop/hdfs/lib/*:/home/hadoop/hadoop-3.1.2/share/hadoop/hdfs/*:/home/hadoop/hadoop-3.1.2/share/hadoop/mapreduce/lib/*:/home/hadoop/hadoop-3.1.2/share/hadoop/mapreduce/*:/home/hadoop/hadoop-3.1.2/share/hadoop/yarn:/home/hadoop/hadoop-3.1.2/share/hadoop/yarn/lib/*:/home/hadoop/hadoop-3.1.2/share/hadoop/yarn/*</value> </property> </configuration>
-
**hadoop-env.sh **
#添加环境变量 export JAVA_HOME=/home/jdk/jdk1.8.0_221
-
yarn-env.sh
#添加环境变量 export JAVA_HOME=/home/jdk/jdk1.8.0_221
-
在/etc/profile中设置环境变量
#打开profile vim /etc/profile #添加环境变量 export LD_LIBRARY_PATH=/home/hadoop/hadoop-3.1.2/lib/native:/home/jdk/jdk1.8.0_221/jre/lib/amd64/server:$LD_LIBRARY_PATH #hadoop环境 以及 gdal编译安装路径/home/gdalinstall/bin export HADOOP_HOME=/home/hadoop/hadoop-3.1.2 export CLASSPATH=$($HADOOP_HOME/bin/hadoop classpath --glob):$CLASSPATH export PATH=$PATH:$JAVA_HOME/bin:/home/hadoop/hadoop-3.1.2/bin:/home/hadoop/hadoop-3.1.2/sbin/:/home/gdalinstall/bin #使更改生效 source /etc/profile
-
启动hadoop
#进入hadoop cd /home/hadoop/hadoop3.1.2 # 初始化 hdfs namenode -format #启动hadoop sbin/start-all.sh #停止hadoop stop-all.sh #关闭防火墙 systemctl stop firewalld
-
安装ant
#创建放置ant的文件夹 cd /home mkdir ant cd ant # 解压ant tar -zxvf apache-ant-1.10.5-bin.tar.gz #进入apache-ant-1.10.5文件夹 cd apache-ant-1.10.5 #配置ant环境变量 export PATH=$PATH:/home/ant/apache-ant-1.10.5/bin
-
测试是否安装成功
http://192.168.126.128:9870
-
在hadoop放入一个tif文件
# hadoop fs –put [本地地址] [hadoop目录] hadoop fs -put /home/test /gdal_info
-
-
安装curl-7.64.1.tar.gz
-
解压并安装
#创建文件夹 cd /home mkdir curl cd /curl # 解压 tar -zxvf curl-7.64.1.tar.gz cd curl-7.64.1 ./configure make make intsll
-
-
安装gdal
#创建文件夹 cd /home mkdir gdal cd /gdal #解压 tar -zxvf gdal-2.4.0.tar.gz cd gdal-2.4.0 ./configure --prefix=/home/gdalinstall --with-java=/home/jdk/jdk1.8.0_221/ --with-hdfs=/home/hadoop/hadoop-3.1.2/ --with-jvm-lib=/home/jdk/jdk1.8.0_221/jre/lib/amd64/server/libjvm.so --with-jvm-lib-add-rpath make make install
-
gdalinfo访问hdfs tif文件信息
gdalinfo /vsihdfs/hdfs://192.168.126.128:9000/gdal_test/dt.tif gdalinfo /vsiwebhdfs/http://192.168.126.128:9870/webhdfs/v1/gdal_test/dt.tif
-
生成gdal.jar
#修改gdal根目录/swig/java/java.opt 添加JAVA_HOME的值为java的根目录 cd /home/gdal/gdal-2.4.0/swig/java/ vim java.opt #添加 JAVA_HOME JAVA_HOME=/home/jdk/jdk1.8.0_221 #安装swig yum install -y swig #gdal根目录/swig/java中执行make && make install make && make install #可以看到生成了 libgdalalljin.so libgdalalljin.la gdal.jar
-
把gdal根目录/swig/java中生成的gdal.jar文件复制到$JAVA_HOME/jre/lib/ext下
-
把*.libgdalalljin.so libgdalalljin.la 或者(.so、.lo、.o、.la、*.a)复制到$JAVA_HOME/jre/lib/amd64/server
-
查看环境变量中是否有$JAVA_HOME/jre/lib/amd64/server
export LD_LIBRARY_PATH=.:$JAVA_HOME/jre/lib/amd64/server
-
改完后 执行source /etc/profile
-
就安装完成,可以使用swig/java/apps里的程序测试一下
-
-
我的环境变量如下
export JAVA_HOME=/home/jdk/jdk1.8.0_221 export HADOOP_HOME=/home/hadoop/hadoop-3.1.2 export CLASSPATH=.:$JAVA_HOME/jre/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar export PATH=$PATH:$JAVA_HOME/bin export LD_LIBRARY_PATH=/home/hadoop/hadoop-3.1.2/lib/native:/home/jdk/jdk1.8.0_221/jre/lib/amd64/server:$LD_LIBRARY_PATH export CLASSPATH=$($HADOOP_HOME/bin/hadoop classpath --glob):$CLASSPATH export PATH=$PATH:$JAVA_HOME/bin:/home/hadoop/hadoop-3.1.2/bin:/home/hadoop/hadoop-3.1.2/sbin/:/home/gdalinstall/bin export PATH=$PATH:/home/ant/apache-ant-1.10.5/bin
-
我的测试程序
-
给hadoop中存储tif文件
#/home/test下面存储的是tif文件 hadoop fs -put /home/test gdal_test #查看存储的tif文件 hdfs dfs -ls / hdfs dfs -ls /gdal_test
-
测试的java程序
import org.gdal.gdal.Dataset; import org.gdal.gdal.gdal; import org.gdal.gdalconst.gdalconst; public class GdalHdfsTest { public static void main(String[] args) { // register gdal gdal.AllRegister(); System.out.println("success"); // set support chinese gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8","YES"); //String tifName = "/vsihdfs/hdfs://192.168.126.129:9000/gdal_test/dt.tif"; String tifName = "/vsiwebhdfs/http://192.168.126.129:9870/webhdfs/v1/gdal_test/dt.tif"; //read tis data Dataset dataset = gdal.Open(tifName, gdalconst.GA_ReadOnly); //get raster x size int xSize = dataset.getRasterXSize(); System.out.println("raster x size :" + xSize); } }
-
-
文章评论