文章目录
前言
Configuration是配置类,用于手工或者shell自动化配置的那四个文件里面的,常规基础配置 随着生态圈多,就需要调用configuration.setString("dsf.namenpde.secondary.http-address","master-:9001")和configuration.setBoolean("dfs.permissions",false);方法 总结:随着Hadoop分布式集群集成生态圈软件越多时,配置就会额外增加,此时则需Configuration类来增加对应的配置
一、Hadoop是什么?
Hadoop是一个能够对大量数据进行分布式处理的软件框架。具有 可靠、高效、可伸缩的特点。
Hadoop的核心是HDFS和Mapreduce,hadoop2.0还包括YARN
二、使用步骤
1.得到文件目录FileSystem
代码如下(示例):
step1实例化得到Configuration对象 Configuration configuration = new Configuration() { step2得到FileSystem对象,总结: 方案1,得到fileSystem对象 FileSystem.setDefaultUri(configuration,"hdfs://192.168.1.107:9000"); 源码:public static FileSystem newInstance(configuration conf) FileSystem fileSystem = FileSystem.newInstance(configuration); 方案2, 源码:public static FileSystem get(configuration conf) FileSystem.setDefaultUri(configuration,"hdfs://192.168.1.107:9000"); FileSystem fileSystem = FileSystem.get(configuration); 方案3: public static FileSystem newINstance(URI uri,configuration conf) URI uri = new URI("hdfs://192.168.1.107:90000"); FileSystem fileSystem = FileSystem.newInstance(uri,configuration) 方案4: public static FileSystem getINstance(URI uri,configuration conf) URI uri = new URI("hdfs://192.168.1.107:9000"); FileSystem fileSystem = FileSystem.get(uri,configuration); FileSystem fileSystem1 = FileSystem.get(uri,configuration,"root");
2.Hadoop命令操作-创建目录
代码如下(示例):
hdfs dfs -mkdir /user 创建一层目录 hdfs dfs -mkdir -p /user/test 创建两层目录 boolean flag = fileSystem.mkdirs(new Path("/youce")); 引用匿名对象,本来需要实例化得到path对象,写法是Path path = new Path("/youce");,因为只用一次可以用当前写法 if (flag){ System.out.println("目录创建成功"); }else { System.out.println("目录创建失败"); }
3.Hadoop命令操作-删除目录
代码如下(示例):
public boolean deleteOnExit(Path f) 删除一个存在的路径 public abstract boolean delete(Path f,boolean recursive) 递归删除,只要写最上一层的目录即可,如果是true则是,false则不递归 public boolean delete(Path f) 删除一个文件 public boolean cancelDeleteOnExit(Path f) 取消删除路径 public void deleteSnapshot(Path path,String snapshotName) 删除快照,一般不用删 删除目录命令: hdfs dfs -rm /user 删除一个文件活递归删除目录 hdfs dfs -mkdir -p /user/a.test 删除一个文件 boolean flag = fileSystem.delete(new Path("/user/test"),true); if (flag){ System.out.println("目录删除成功"); }else { System.out.println("目录删除失败"); }
4.Hadoop命令操作-上传文件或目录
代码如下(示例):
源码:src-本地路径 dst-目标路径 delSrc-删除原路径,一般不删 overwrite覆盖重复文件及内容 public void copyFromLocalFile(Path src,Path dst) 上传指定文件到目标路径中 public void copyFromLocalFile(boolean delSrc,Path src,Path dst) public void copyFromLocalFile(boolean delSrc,boolean overwrite,Path[] srcs,Path dst) public void copyFromLocalFile(boolean delSrc,boolean overwrite,Path src,Path dst) 将本地文件或目录上传到hdfs中的路径目录命令: hdfs dsf -put /opt/a.txt 上传文件 hdfs dsf -put /opt/test/user 上传目录 fileSystem.copyFromLocalFile(new Path("D:\\test\\catalina.out"),new Path("/youce")); fileSystem.copyFromLocalFile(true,new Path("D:\\test\\catalina.out"),new Path("/youce")); Path[] path = {new Path("D:\\test\\2.txt"),new Path("D:\\test\\3.txt")}; fileSystem.copyFromLocalFile(true,false,path,new Path("/youce"));