分布式文件系统HDFS课堂测试分析

参考链接:分布式文件系统HDFS学习指南http://dblab.xmu.edu.cn/blog/290-2/
1、需要使用hadoop登录,在之下进行HDFS的编程
在之前登录时,一直是系统用户自动登录,需要切换hadoop进行。现了解到如何去除自动登录,点击设置中的用户账号,再点击右上角的锁定进行解锁,之后就可以选择是否开启自动登录。
2、VMware-tools安装
为了虚拟机与主机的交互性更强,文件可以相互拖拽。之前在非hadoop账户下安装的tools,在hadoop下不能使用。然后在hadoop下再安装tools,在虚拟机下选项安装tools工具,之后解压(tar -zxvf  路径名),解压完成后进入vmware-tools-distrib中,用命令行运行vmware-install.pl ,之后一路敲y,即可安装成功。在hadoop用户(非超级用户)安装时,需切换为root用户,方可安装。
3、添加jar包
新建project文件时,需要导入加载该Java工程所需要用到的JAR包,这些包中包含可以访问的HDFS的javaAPI。
为了编写一个能够与HDFS交互的Java应用程序,一般需要向Java工程中添加以下JAR包:
(1)”/usr/local/hadoop/share/hadoop/common”目录下的hadoop-common-2.7.1.jar和haoop-nfs-2.7.1.jar;
(2)/usr/local/hadoop/share/hadoop/common/lib”目录下的所有JAR包;
(3)“/usr/local/hadoop/share/hadoop/hdfs”目录下的haoop-hdfs-2.7.1.jar和haoop-hdfs-nfs-2.7.1.jar;
(4)“/usr/local/hadoop/share/hadoop/hdfs/lib”目录下的所有JAR包。
4、编译运行程序
在开始编译运行程序之前,请一定确保Hadoop已经启动运行,如果还没有启动,需要打开一个Linux终端,输入以下命令启动Hadoop
cd /usr/local/hadoop
./sbin/start-dfs.sh
5、错误Name node is in safe mode的解决方法
这是因为在分布式文件系统启动的时候,开始的时候会有安全模式,当分布式文件系统处于安全模式的情况下,文件系统中的内容不允许修改也不允许删除,直到安全模式结束。安全模式主要是为了系统启动的时候检查各个DataNode上数据块的有效性,同时根据策略必要的复制或者删除部分数据块。运行期通过命令也可以进入安全模式。在实践过程中,系统启动的时候去修改和删除文件也会有安全模式不允许修改的出错提示,只需要等待一会儿即可。
 
可以通过以下命令来手动离开安全模式:
 
bin/hadoop dfsadmin -safemode leave
 
用户可以通过dfsadmin -safemode value 来操作安全模式,参数value的说明如下:
enter - 进入安全模式
leave - 强制NameNode离开安全模式
get - 返回安全模式是否开启的信息
wait - 等待,一直到安全模式结束。
6、错误could only be replicated to 0 nodes, instead of 1
解决方案:
删除 /tmp/ 下的所有文件,重新format。
7、错误:Failed to replace a bad datanode on the existing pipeline due to no more good
在程序中进行如下设置:
conf.set("dfs.client.block.write.replace-datanode-on-failure.policy","NEVER");
conf.set("dfs.client.block.write.replace-datanode-on-failure.enable","true"); 
 
8、编程实现下列要求:
1、创建一个自己姓名首字母的文件夹;
2、在该文件夹下创建一个hdfstest1.txt文件,向文件内输入“班级学号姓名HDFS课堂测试”的文字内容;
3、在文件加下再创建一个hdfstest2.txt文件,并将hdfstest1文件的内容写到该文件中,并将hdfstest2的内容输出。
  1  
  2 import java.io.*;
  3  
  4 import org.apache.hadoop.conf.Configuration;
  5 import org.apache.hadoop.fs.FileSystem;
  6 import org.apache.hadoop.fs.*;
  7  
  8  
  9 public class hdfs {
 10     //路径是否存在
 11     public static boolean test(Configuration conf,String path) throws  IOException
 12     {
 13         FileSystem fs=FileSystem.get(conf);
 14         return fs.exists(new Path(path));
 15     }
 16     //创建目录
 17     public static boolean mkdir (Configuration conf ,String remoteDir)throws IOException
 18     {
 19     FileSystem fs=FileSystem.get(conf);
 20     Path dirPath=new Path(remoteDir);
 21     boolean result=fs.mkdirs(dirPath);
 22     fs.close();
 23     return result;
 24     }
 25     //创建文件
 26     public static void touchz(Configuration conf,String remoteFilePath )throws IOException
 27     {
 28         FileSystem fs=FileSystem.get(conf);
 29         Path remotePath=new Path(remoteFilePath);
 30         FSDataOutputStream outputStream =fs.create(remotePath);
 31         outputStream.close();
 32         fs.close();
 33     }
 34     //删除文件
 35     public static boolean rm(Configuration conf,String remoteFilePath)throws IOException
 36     {
 37         FileSystem fs=FileSystem.get(conf);
 38         Path remotePath=new Path(remoteFilePath);
 39         boolean result=fs.delete(remotePath,false);
 40         fs.close();
 41         return result;
 42     }
 43     //读文件
 44     public static void cat(Configuration conf,String FilePath)throws IOException
 45     {
 46         FileSystem fs=FileSystem.get(conf);
 47         Path file=new Path(FilePath);
 48         FSDataInputStream getIt=fs.open(file);
 49         BufferedReader d=new BufferedReader(new InputStreamReader(getIt));
 50         String content=d.readLine();
 51         System.out.println(content);
 52         d.close();
 53         fs.close();
 54     }
 55     //追加文件内容
 56     public static void appendContentToFile(Configuration conf,String content,String remoteFilePath)throws IOException
 57     {
 58         FileSystem fs=FileSystem.get(conf);
 59         Path remotePath=new Path(remoteFilePath);
 60         FSDataOutputStream out=fs.append(remotePath);
 61         out.write(content.getBytes());
 62         out.close();
 63         fs.close();
 64     }
 65     //将文件1写入文件2
 66     public static void appendContentToFile2(Configuration conf,String  remoteFilePath,String remoteFilePath2)throws IOException
 67     {
 68         FileSystem fs=FileSystem.get(conf);
 69         Path file=new Path(remoteFilePath);
 70         FSDataInputStream  getIt=fs.open(file);
 71         BufferedReader d=new BufferedReader(new InputStreamReader(getIt));
 72         String content1=d.readLine();
 73         
 74         Path remotePath=new Path(remoteFilePath2);
 75         FSDataOutputStream out=fs.append(remotePath);
 76         out.write(content1.getBytes());
 77         d.close();
 78         out.close();
 79         fs.close();
 80     }
 81     
 82     public static void main(String[] args)
 83     {
 84         Configuration conf=new Configuration();
 85         conf.set("dfs.client.block.write.replace-datanode-on-failure.policy","NEVER");
 86  
 87         conf.set("dfs.client.block.write.replace-datanode-on-failure.enable","true");
 88         conf.set("fs.default.name", "hdfs://localhost:9000");
 89         conf.set("fs.hdfs.impl","org.apache.hadoop.hdfs.DistributedFileSystem");
 90         String remoteDir ="/user/hadoop/YL";//HDFS目录
 91         String remoteFilePath="/user/hadoop/YL/hdfstest1.txt";
 92         String remoteFilePath2="/user/hadoop/YL/hdfstest2.txt";
 93         String content="信1605-320163619杨蕾HDFS课堂测试";
 94         //创建目录
 95         try {
 96             if(!hdfs.test(conf, remoteDir))
 97             {
 98                 hdfs.mkdir(conf, remoteDir);
 99                 System.out.println("创建目录"+remoteDir);
100             }
101             else
102             {
103                 System.out.println(remoteDir+"目录已存在");
104             }
105     //创建文件
106             if(!hdfs.test(conf, remoteFilePath))
107             {
108                 hdfs.touchz(conf, remoteFilePath);
109                 System.out.println("创建文件"+remoteFilePath);
110             }
111             else
112             {
113                 System.out.println(remoteFilePath+"已存在");
114             }
115                     //向文件内输入
116             hdfs.appendContentToFile(conf, content, remoteFilePath);
117             hdfs.cat(conf, remoteFilePath);
118             //创建文件2
119             if(!hdfs.test(conf, remoteFilePath2))
120             {
121                 hdfs.touchz(conf, remoteFilePath2);
122                 System.out.println("创建文件"+remoteFilePath2);
123             }
124             else
125             {
126                 System.out.println(remoteFilePath2+"已存在");
127             }
128             //向文件2内写入
129             hdfs.appendContentToFile2(conf, remoteFilePath, remoteFilePath2);
130             hdfs.cat(conf, remoteFilePath2);
131         } catch (IOException e) {
132         e.printStackTrace();
133         }
134     }
135 }
hdfs

 

 
 
posted @ 2018-10-04 21:28  天明宝  阅读(514)  评论(0)    收藏  举报