在执行hdfs命令时,会有一个警告:
WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
我们增加调试信息看问题出在了哪里。
增加调试信息有两种方式,
1、在执行命令前加一条如下命令:
export HADOOP_ROOT_LOGGER=DEBUG,console
比如:
[root@cdh-node01 bin]$ export HADOOP_ROOT_LOGGER=DEBUG,console
[root@cdh-node01 bin]$ hdfs dfs -ls /
2、在$HADOOP_CONF_DIR/log4j.properties(hadoop-3.2.0的路径是/home/apps/hadoop-3.2.0/etc/hadoop/log4j.properties)文件中添加如下代码:
log4j.logger.org.apache.hadoop.util.NativeCodeLoader=DEBUG
我选择的是第二种(因为后面的一种解决方案也需要动这个文件)。
然后执行一条命令,查看调试信息,如下:
[root@cdh-node01 hadoop]# hdfs dfs -ls /
2019-03-13 11:59:58,116 DEBUG util.NativeCodeLoader: Trying to load the custom-built native-hadoop library...
2019-03-13 11:59:58,183 DEBUG util.NativeCodeLoader: Failed to load native-hadoop with error: java.lang.UnsatisfiedLinkError: /home/apps/hadoop-3.2.0/lib/native/libhadoop.so.1.0.0: /lib64/libc.so.6: version `GLIBC_2.14' not found (required by /home/apps/hadoop-3.2.0/lib/native/libhadoop.so.1.0.0)
2019-03-13 11:59:58,183 DEBUG util.NativeCodeLoader: java.library.path=/home/apps/hadoop-3.2.0/lib:/home/apps/hadoop-3.2.0/lib/native
2019-03-13 11:59:58,183 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
Found 5 items
drwxr-xr-x - root supergroup 0 2019-03-05 18:56 /blockrecover
drwxr-xr-x - root supergroup 0 2019-03-13 10:43 /hbase
drwxr-xr-x - root supergroup 0 2019-01-28 16:59 /spark
drwx-wx-wx - root supergroup 0 2019-03-05 15:31 /tmp
drwx------ - root supergroup 0 2019-03-03 19:43 /user
问题出来了。对于这个问题,网上有的解决方案是加两个软链接,即:
ln -s libhadoop.so.1.0.0 libhadoop.so
ln -s libhdfs.so.0.0.0 libhdfs.so
但我加上了这两个软链接,问题依旧。
两种方式解决unable to load native-hadoop library for you platform
我们查看一下libhadoop.so.1.0.0的glibc版本,如下:
[root@cdh-node01 native]# ldd libhadoop.so
./libhadoop.so: /lib64/libc.so.6: version `GLIBC_2.14' not found (required by ./libhadoop.so)
linux-vdso.so.1 => (0x00007ffc005bd000)
libdl.so.2 => /lib64/libdl.so.2 (0x00007f4fd783b000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f4fd761d000)
libc.so.6 => /lib64/libc.so.6 (0x00007f4fd7289000)
/lib64/ld-linux-x86-64.so.2 (0x00000037ccc00000)
问题其实已经出现了,libhadoop.so需要的glibc版本是glibc_2.14,没有找到。
我们再看一下当前系统的glibc版本,如下:
[root@cdh-node01 native]# ldd --version
ldd (GNU libc) 2.12
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Written by Roland McGrath and Ulrich Drepper.
可以看到,当前系统的glibc版本是2.12。
那么怎么解决这个警告呢?这里提供两种方式。
1、配置$HADOOP_CONF_DIR/log4j.properties/log4j.properties文件来忽略掉这个警告,即将前面配置的log4j.logger.org.apache.hadoop.util.NativeCodeLoader=DEBUG该成:
log4j.logger.org.apache.hadoop.util.NativeCodeLoader=ERROR
2、升级系统的glib版本。
下载glibc-2.14.tar.bz2,地址为:http://ftp.ntu.edu.tw/gnu/glibc/
下载glibc-linuxthreads-2.5.tar.bz2,地址为:http://ftp.ntu.edu.tw/gnu/glibc/
安装步骤如下:
1) 把下载的bz2包放到一个文件夹下
[root@cdh-node01 glibc]# ls
glibc-2.14.tar.bz2 glibc-linuxthreads-2.5.tar.bz2
[root@cdh-node01 glibc]# tar -xjvf glibc-2.14.tar.bz2
[root@cdh-node01 glibc]# ls
glibc-2.14.tar.bz2 glibc-2.14 glibc-linuxthreads-2.5.tar.bz2
3) 解压glibc-linuxthreads-2.5.tar.bz2到glibc-2.14中
[root@cdh-node01 glibc]# cd glibc-2.14
[root@cdh-node01 glibc]# tar -xjvf ../glibc-linuxthreads-2.5.tar.bz2
此时,glibc-2.14目录下会多出两个文件夹,即linuxthreads和linuxthreads_db
4) 回到上一级目录,执行如下命令:
//回到上一级目录
[root@cdh-node01 glibc-2.14]$ cd ..
//加上优化开关,否则会出现错误'#error "glibc cannot be compiled without optimization"'
[root@cdh-node01 glibc]$ export CFLAGS="-g -O2"
5) 执行如下命令:
[root@cdh-node01 glibc]$ glibc-2.14/configure --prefix=/usr --disable-profile --enable-add-ons --with-headers=/usr/include --with-binutils=/usr/bin --disable-sanity-checks
6) 执行make
//编译,执行很久(5-10分钟),可能出错,出错再重新执行
[root@cdh-node01 glibc]$ make
7) 执行make install
//安装,必须root用户执行,执行很久
[root@cdh-node01 glibc]$ make install
8) 使用命令ls -l /lib/libc.so.6查看是否升级成功
[root@cdh-node01 glibc]$ ll /lib64/libc.so.6
lrwxrwxrwx 1 root root 11 Nov 12 09:24 /lib/libc.so.6 -> libc-2.14.so
9) 重启hadoop
[root@cdh-node01 glibc]$ stop-dfs.sh
[root@cdh-node01 glibc]$ stop-yarn.sh
10) 执行一条hdfs命令,发现本地库被成功加载
[root@cdh-node01 glibc]$ export HADOOP_ROOT_LOGGER=DEBUG,console
[root@cdh-node01 glibc]$ hdfs dfs -ls /
2019-03-13 04:18:37,383 DEBUG util.NativeCodeLoader: Trying to load the custom-built native-hadoop library...
2019-03-13 04:18:37,388 DEBUG util.NativeCodeLoader: Loaded the native-hadoop library
Found 5 items
drwxr-xr-x - root supergroup 0 2019-03-05 10:56 /blockrecover
drwxr-xr-x - root supergroup 0 2019-03-13 02:43 /hbase
drwxr-xr-x - root supergroup 0 2019-01-28 08:59 /spark
drwx-wx-wx - root supergroup 0 2019-03-05 07:31 /tmp
drwx------ - root supergroup 0 2019-03-03 11:43 /user
[root@cdh-node02 glibc]# hadoop checknative -a
2019-03-14 05:58:41,587 INFO bzip2.Bzip2Factory: Successfully loaded & initialized native-bzip2 library system-native
2019-03-14 05:58:41,624 INFO zlib.ZlibFactory: Successfully loaded & initialized native-zlib library
2019-03-14 05:58:41,647 WARN zstd.ZStandardCompressor: Error loading zstandard native libraries: java.lang.InternalError: Cannot load libzstd.so.1 (libzstd.so.1: cannot open shared object file: No such file or directory)!
2019-03-14 05:58:41,692 WARN erasurecode.ErasureCodeNative: ISA-L support is not available in your platform... using builtin-java codec where applicable
Native library checking:
hadoop: true /home/apps/hadoop-3.2.0/lib/native/libhadoop.so
zlib: true /lib64/libz.so.1
zstd : false
snappy: true /usr/lib64/libsnappy.so.1
lz4: true revision:10301
bzip2: true /lib64/libbz2.so.1
openssl: false Cannot load libcrypto.so (libcrypto.so: cannot open shared object file: No such file or directory)!
ISA-L: false libhadoop was built without ISA-L support
2019-03-14 05:58:42,124 INFO util.ExitUtil: Exiting with status 1: ExitException
附:libc/glibc/glib
glibc和libc都是Linux下的C函数库。
libc是Linux下的ANSIC函数库;glibc是Linux下的GUNC函数库。
glib是用C写的一些utilities,即C的工具库,和libc/glibc没有关系。
glibc是linux下面c标准库的实现,即GNU C Library。glibc本身是GNU旗下的C标准库,后来逐渐成为了Linux的标准c库,而Linux下原来的标准c库Linux libc逐渐不再被维护。
glibc在/lib目录下的.so文件为libc.so.6。
查看当前系统的glibc版本的两种方法:
[root@cdh-node01 ~]# ll /lib64/libc.so.6
lrwxrwxrwx 1 root root 12 Mar 13 12:09 /lib64/libc.so.6 -> libc-2.14.so[root@cdh-node01 ~]# ldd --version
ldd (GNU libc) 2.14
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Written by Roland McGrath and Ulrich Drepper.
这两种方法都可以看到当前系统的glibc的版本是2.14。