最近一段时间又在忙hadoop了,针对最近开发过程中遇到的问题总结一下:
(1)HDFS命令 hadoop fs -rmr /* 的一个漏洞问题
实例如下:
解决方案:将HDFS的具体访问路径在命令行中写出:
hadoop fs -rmr hdfs://hadoop20:9000/*
(2) HDFS中block块的具体理解:我们都知道文件在HDFS中是以block块的形式来进行存储的,但归根到底文件最终还是存储在了本地文件系统linux中,而其具体存储路径是由下面的配置文件决定的:
在实战中无意间发现了下面一个有趣的问题:block块的内容竟然是可以阅读的!这正好说明了block块是按固定的大小,顺序的对文件进行划分–流式的划分,然而在内容上并不对我们的文件进行任何的处理。
(3)eclipse中导入jar包的一个优异方式
以前导入jar包的时候,是用的这种方式:
这种方式虽然简单,但是缺点也是显而易见的,就是导入的jar包太冗余,这几天学会了一种新的导入jar包的方式:自己建立library,进而在library中导入相应的jar包
(4)Hadoop中HDFS的Java Api—–FileSystem的具体用法:
注意:
1>上传文件的前提:必须在hdfs中先创建一个文件 create
2>下载文件的前提:必须在hdfs中先打开这个文件 open
①创建文件夹
public class App1
{
public static void main(String[] args) throws Exception
{
Configuration conf = new Configuration();
FileSystem fileSystem = FileSystem.get(new URI("hdfs://hadoop20:9000/"), conf, "root");
//创建文件夹的方式:可以迭代创建
fileSystem.mkdirs(new Path("hdfs://hadoop20:9000/dir1/dir2"));
}
}
②从本地文件系统(本地参考的是eclipse安装在linux上还是windows上)上传文件到HDFS上
public class App1
{
public static void main(String[] args) throws Exception
{
Configuration conf = new Configuration();
FileSystem fileSystem = FileSystem.get(new URI("hdfs://hadoop20:9000/"), conf, "root");
//从本地上传文件到HDFS
FSDataOutputStream fw = fileSystem.create(new Path("/dir1/dir2/file.txt"));
FileInputStream fr = new FileInputStream("C:\\file.txt");
IOUtils.copyBytes(fr,fw,1024,true);
}
}
上面的代码也可以用下面简单的代码来替代:
public class App1
{
public static void main(String[] args) throws Exception
{
Configuration conf = new Configuration();
FileSystem fileSystem = FileSystem.get(new URI("hdfs://hadoop20:9000/"), conf, "root");
//从本地上传文件到HDFS
fileSystem.copyFromLocalFile(new Path("C:\\file.txt"), new Path("hdfs://hadoop20:9000/dir1/dir2/file.txt"));
}
}
③从HDFS中下载文件到本地(本地参考的是eclipse安装在linux上还是windows上)
public class App1
{
public static void main(String[] args) throws Exception
{
Configuration conf = new Configuration();
FileSystem fileSystem = FileSystem.get(new URI("hdfs://hadoop20:9000/"), conf, "root");
//从hdfs中下载文件到本地
FSDataInputStream fr = fileSystem.open(new Path("hdfs://hadoop20:9000/dir1/dir2/file.txt"));
IOUtils.copyBytes(fr,System.out, 1024, true);
}
}
public class App1
{
public static void main(String[] args) throws Exception
{
Configuration conf = new Configuration();
FileSystem fileSystem = FileSystem.get(new URI("hdfs://hadoop20:9000/"), conf, "root");
//从hdfs中下载文件到本地
fileSystem.copyToLocalFile(new Path("hdfs://hadoop20:9000/dir1/dir2/file.txt"), new Path("D:\\file.txt"));
}
}
运行结果:
调试方式:
fileSystem.copyToLocalFile(new Path("hdfs://hadoop20:9000/dir1/dir2/file.txt"), new Path("D:\\file.txt"));
改为:
fileSystem.copyToLocalFile(false,new Path("hdfs://hadoop20:9000/dir1/dir2/file.txt"), new Path("D:\\file.txt"),true);
错误原因:
在使用copyToLocalFile(Path src, Path dst)方法时可能导致 setPermission文件本地文件效验失败,因此使用copyToLocalFile( boolean delSrc,Path src, Path dst, boolean useRawLocalFileSystem),其中:
boolean delSrc 指是否将原文件删除
Path src 指要下载的文件路径
Path dst 指将文件下载到的路径
boolean useRawLocalFileSystem 是否开启文件效验
④删除文件夹
public class App1
{
public static void main(String[] args) throws Exception
{
Configuration conf = new Configuration();
FileSystem fileSystem = FileSystem.get(new URI("hdfs://hadoop20:9000/"), conf, "root");
//删除文件或文件夹
fileSystem.delete(new Path("hdfs://hadoop20:9000/dir1/dir2/file.txt"), false);
}
}
(5)Exception in thread “main” org.apache.hadoop.security.AccessControlException: Permission denied: user=Administrator, access=EXECUTE, inode=”/tmp”:hadoop:supergroup:drwx——
这句话的含义当前用户是Administrator、想对/tmp这个目录执行执行操作,但是不具有相应的权限。
处理方式:
<property>
<name>dfs.permissions</name>
<value>false</value>
</property>
</configuration>
FileSystem fileSystem = FileSystem.get(new URI("hdfs://hadoop20:9000/"), conf);
改为:
FileSystem fileSystem = FileSystem.get(new URI("hdfs://hadoop20:9000/"), conf, "root");
//指定以root的身份去访问
hadoop fs -chmod 777 /tmp 从而修改/tmp目录的权限
从而适合:
对于以上问题,如有问题,欢迎留言指正!