Hadoop分布式文件系统HDFS的JAVA-API实战

Hadoop分布式文件系统HDFS的实战

HDFS JAVA API

搭建Linux下Eclipse开发环境
  • 将eclipse-jee-juno-SR2-linux-gtk-x86_64.tar.gz和hadoop-eclipse-plugin-2.7.1.jar上传/usr/local目录下解压
[root@hadoop0 local]# tar -xvf eclipse-jee-juno-SR2-linux-gtk-x86_64.tar.gz 
为Eclipse安装Hadoop插件

图片15
在这里插入图片描述


  • 打开Eclipse,直接选择/root/workpace,单击菜单中的Window->Open Perspective->Other->Map/Reduce

图片16
在这里插入图片描述


  • 在Map/Reduce Location视图里单击右键,new Hadoop location…

图片17
在这里插入图片描述


  • 填入Hadoop基本信息

图片18
在这里插入图片描述


  • 完成第一大步,文件目录终于可以可视化

图片19
在这里插入图片描述


  • 新建一个工程Project->new Project

图片20
在这里插入图片描述


  • 选择Java,取名hdsf,yes。生成了一个hdfs工程

图片21
在这里插入图片描述


jar包导入
  • 将已经准备好的3hdfs工程复制到eclipse中,因为篇幅的关系,代码放在最后

图片22

在这里插入图片描述


  • 这么多爆红是因为没有jar包,在/usr/local/hadoop/share/hadoop中四个jar包

图片23
在这里插入图片描述


  • 新建alllib放所有需要的jar包,common,hdfs,mapreduce,yarn四个子目录下所有jar包,包括lib中的,最后会有90M,171个jar包

图片25
在这里插入图片描述


  • 右键工程最后一个选项,进入Properties for hdfs -> java build path

图24

在这里插入图片描述


  • Libraries->Add Extemal Jar,找到/usr/local/hadoop/share/hadoop/alllib全选jar包,ok
  • 会有一个增加for的错误,直接鼠标点修改jre1.5以上,自动改,这个跟idea一样的

图片26
在这里插入图片描述


  • 全部正确,准备运行

图片27在这里插入图片描述

测试Hadoop API

public static void main(String[] args) throws Exception {
    //上传文件到hadoop
   uploadFile();
   createFile();
   createDir();
   fileRename();
   deleteFile();
   readFile();
   isFileExists();
    //判断文件的最后修改的信息
   fileLastModify();
    //文件的位置信息
   fileLocation();
    //节点的信息
   nodeList();
}

使用HDFS文件系统并提供服务器路径

static FileSystem getFileSystem() throws Exception {
   URI uri = new URI("hdfs://hadoop0:9000/");
   // 使用HDFS文件系统并提供服务器路径,端口号在core-site.xml中配置
   FileSystem fileSystem = FileSystem.get(uri, new Configuration());
   return fileSystem;
}
  • 测试getFileSystem

图片28

在这里插入图片描述

run as->java appliction->ok

图片29

在这里插入图片描述

其他api不一一实现了,很简单的,就是JDBC最简单的那种

Code

package zuosheng.Binarytree;


import java.net.URI;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hdfs.DistributedFileSystem;
import org.apache.hadoop.hdfs.protocol.DatanodeInfo;
import org.apache.hadoop.io.IOUtils;

public class Hdfs {

   /**
    * @param args
    * @throws Exception
    */
   public static void main(String[] args) throws Exception {
      uploadFile();
      createFile();
      createDir();
      fileRename();
      deleteFile();
      readFile();
      isFileExists();
      fileLastModify();
      fileLocation();
      nodeList();
   }

   static FileSystem getFileSystem() throws Exception {
      URI uri = new URI("hdfs://hadoop0:9000/");
      // 使用HDFS文件系统并提供服务器路径,端口号在core-site.xml中配置
      FileSystem fileSystem = FileSystem.get(uri, new Configuration());
      return fileSystem;
   }

   public static void uploadFile() throws Exception {

      FileSystem hdfs = getFileSystem();
      Path src = new Path("/root/original-ks.cfg");
      Path dst = new Path("/");
      FileStatus files[] = hdfs.listStatus(dst);
      for (FileStatus file : files) {
         System.out.println(file.getPath());
      }
      System.out.println("------------after upload--------------------");
      hdfs.copyFromLocalFile(src, dst);
      files = hdfs.listStatus(dst);
      for (FileStatus file : files) {
         System.out.println(file.getPath());
      }
   }

   public static void createFile() throws Exception {

      byte[] buff = "Hello Hadoop 888@Chinasofti\n".getBytes();
      FileSystem hdfs = getFileSystem();
      Path dfs = new Path("/testcreate");
      FSDataOutputStream outputStream = hdfs.create(dfs);
      outputStream.write(buff, 0, buff.length);
      outputStream.close();

   }

   public static void createDir() throws Exception {
      FileSystem hdfs = getFileSystem();
      Path dfs = new Path("/TestDir");
      hdfs.mkdirs(dfs);
   }

   public static void fileRename() throws Exception {

      FileSystem hdfs = getFileSystem();
      Path frpaht = new Path("/original-ks.cfg");
      Path topath = new Path("/original-ks2.cfg");
      boolean isRename = hdfs.rename(frpaht, topath);
      String result = isRename ? "成功" : "失败";
      System.out.println("文件重命名结果为:" + result);
   }

   public static void deleteFile() throws Exception {
      FileSystem hdfs = getFileSystem();
      Path delef = new Path("/TestDir");
      boolean isDeleted = hdfs.delete(delef, false);
      // 递归删除
      // boolean isDeleted=hdfs.delete(delef,true);
      System.out.println("Delete?" + isDeleted);
   }

   public static void readFile() throws Exception {
      FileSystem fileSystem = getFileSystem();
      FSDataInputStream openStream = fileSystem.open(new Path("/testcreate"));
      IOUtils.copyBytes(openStream, System.out, 1024, false);
      IOUtils.closeStream(openStream);

   }

   public static void isFileExists() throws Exception {
      FileSystem hdfs = getFileSystem();
      Path findf = new Path("/test1");
      boolean isExists = hdfs.exists(findf);
      System.out.println("Exist?" + isExists);
   }

   public static void fileLastModify() throws Exception {
      FileSystem hdfs = getFileSystem();
      Path fpath = new Path("/testcreate");
      FileStatus fileStatus = hdfs.getFileStatus(fpath);
      long modiTime = fileStatus.getModificationTime();
      System.out.println("testcreate的修改时间是" + modiTime);
   }

   public static void fileLocation() throws Exception {
      FileSystem hdfs = getFileSystem();
      Path fpath = new Path("/testcreate");
      FileStatus filestatus = hdfs.getFileStatus(fpath);
      BlockLocation[] blkLocations = hdfs.getFileBlockLocations(filestatus,
            0, filestatus.getLen());
      int blockLen = blkLocations.length;
      for (int i = 0; i < blockLen; i++) {
         String[] hosts = blkLocations[i].getHosts();
         System.out.println("block_" + i + "_location:" + hosts[0]);
      }

   }

   public static void nodeList() throws Exception {
      FileSystem fs = getFileSystem();
      DistributedFileSystem hdfs = (DistributedFileSystem) fs;

      DatanodeInfo[] dataNodeStats = hdfs.getDataNodeStats();
      for (int i = 0; i < dataNodeStats.length; i++) {
         System.out.println("DataNode_" + i + "_Name:"
               + dataNodeStats[i].getHostName());

      }

   }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

木子津

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值