大数据-HDFS(二)

                                     大数据-HDFS(二)

目录

HDFS 之 java API 开发 

HDFS 之 IO流操作文件


本章节主要介绍使用javaAPI对hdfs进行文件的读写操作。

每天进步一小点,加油鸭~

HDFS 之 java API 开发 

第一步:配置Windows的 hadoop 环境变量

1、解压资料当中的 hadoop-2.6.0-cdh5.14.2_windows环境配置安装包.rar 这个压缩文件文件到一个没有中文没有空格的目录下

2、然后在windows当中配置hadoop的环境变量 

3、然后将hadoop.dll文件拷贝到C:\Windows\System32

注意:如果没有 配置好windows的hadoop的环境变量,那么就会报以下错误

第二步:创建maven工程并导入jar包

由于cdh版本的所有的软件涉及版权的问题,所以并没有将所有的jar包托管到maven仓库当中去,而是托管在了CDH自己的服务器上面,所以我们默认去maven的仓库下载不到,需要自己手动的添加repository去CDH仓库进行下载,以下两个地址是官方文档说明,请仔细查阅。

https://www.cloudera.com/documentation/enterprise/release-notes/topics/cdh_vd_cdh5_maven_repo.html

https://www.cloudera.com/documentation/enterprise/release-notes/topics/cdh_vd_cdh5_maven_repo_514x.html

<repositories>
    <repository>
        <id>cloudera</id>
        <url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
    </repository>
 </repositories>
 <dependencies>
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-client</artifactId>
        <version>2.6.0-mr1-cdh5.14.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-common</artifactId>
        <version>2.6.0-cdh5.14.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-hdfs</artifactId>
        <version>2.6.0-cdh5.14.2</version>
    </dependency>
 
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-mapreduce-client-core</artifactId>
        <version>2.6.0-cdh5.14.2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/junit/junit -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>RELEASE</version>
    </dependency>
 </dependencies>
 <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.0</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <encoding>UTF-8</encoding>
                <!--   <verbal>true</verbal>-->
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.4.3</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <minimizeJar>true</minimizeJar>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
 </build>

第三步:开发hdfs的javaAPI操作

  • 创建文件夹
@Test
 public void mkdirToHdfs() throws IOException {
    Configuration configuration = new Configuration();
    configuration.set("fs.defaultFS","hdfs://node01:8020");
    FileSystem fileSystem = FileSystem.get(configuration);
    fileSystem.mkdirs(new Path("/hlbdx/dir1"));
    fileSystem.close();
 }
  • 文件上传
 @Test
 public void uploadFile() throws IOException {
       Configuration configuration = new Configuration();
       configuration.set("fs.defaultFS","hdfs://node01:8020");
       FileSystem fileSystem = FileSystem.get(configuration);
       fileSystem.copyFromLocalFile(new Path("file:///d:\\hello.txt") , new     
       Path("hdfs://node01:8020/hlbdx/dir1"));
       fileSystem.close();
   }
  • 文件下载
@Test
public void downloadFile() throws IOException, URISyntaxException {
        Configuration configuration = new Configuration();
        FileSystem fileSystem =FileSystem.get(new URI("hdfs://node01:8020"),configuration);
        fileSystem.copyToLocalFile(new Path("hdfs://node01:8020/edits.txt"),new Path("file:///d:\\hello2.txt"));
        fileSystem.close();
   }
  • 文件删除
 @Test
public  void delFile() throws IOException, URISyntaxException {
        Configuration configuration = new Configuration();
        FileSystem fileSystem =FileSystem.get(new URI("hdfs://node01:8020"),configuration);
        fileSystem.deleteOnExit(new Path("hdfs://node01:8020/edits.txt"));
        fileSystem.close();
}
  • 文件重命名
@Test
public void renameFile() throws IOException {
      Configuration configuration = new Configuration();
      configuration.set("fs.defaultFS","hdfs://node01:8020");
      FileSystem fileSystem = FileSystem.get(configuration);
      fileSystem.rename(new Path("hdfs://node01:8020/hlbdx/dir/aa.txt"),
                new Path("hdfs://node01:8020/kkb/dir/bb.xml"));
      fileSystem.close();
}
  • 查看hdfs文件信息
@Test
public void testListFiles() throws IOException, InterruptedException, URISyntaxException{
        // 1获取文件系统
        Configuration configuration = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://node01:8020"), configuration);
        // 2 获取文件详情
        RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);
        while(listFiles.hasNext()){
            LocatedFileStatus status = listFiles.next();
            // 输出详情
            // 文件名称
            System.out.println(status.getPath().getName());
            // 长度
            System.out.println(status.getLen());
            // 权限
            System.out.println(status.getPermission());
            // 分组
            System.out.println(status.getGroup());
            // 获取存储的块信息
            BlockLocation[] blockLocations = status.getBlockLocations();
 
            for (BlockLocation blockLocation : blockLocations) {
                // 获取块存储的主机节点
                String[] hosts = blockLocation.getHosts();
                for (String host : hosts) {
                    System.out.println(host);
                }
            }
        }
        // 3 关闭资源
        fs.close();
    }

HDFS 之 IO流操作文件

  • 通过io流进行数据上传
@Test
 public void putFileToHDFS() throws IOException, InterruptedException, URISyntaxException {
        // 1 获取文件系统
        Configuration configuration = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://node01:8020"), configuration);
        // 2 创建输入流
        FileInputStream fis = new FileInputStream(new File("d:\\helo.txt"));
        // 3 获取输出流
        FSDataOutputStream fos = fs.create(new Path("hdfs://node01:8020/outresult.txt"));
        // 4 流对拷
        IOUtils.copy(fis, fos);
        // 5 关闭资源
        IOUtils.closeQuietly(fos);
        IOUtils.closeQuietly(fis);
        fs.close();
    }
  • 通过IO流从hdfs上面下载文件
 @Test
public void pullFileFromHDFSTolocal() throws IOException {
        Configuration configuration = new Configuration();
        configuration.set("fs.defaultFS","hdfs://node01:8020");
        FileSystem fileSystem = FileSystem.get(configuration);
        FileInputStream fis = new FileInputStream(new File("F:\\testfile\\aa.txt"));
        FSDataOutputStream fos = fileSystem.create(new Path("hdfs://node01:8020/hlbdx/dir/cc.txt"));
        IOUtils.copy(fis,fos);
        IOUtils.closeQuietly(fos);
        IOUtils.closeQuietly(fis);
}
  • hdfs的小文件合并
/**
* 小文件合并
*/
@Test
public   void  mergeFile() throws URISyntaxException, IOException, InterruptedException {
         //获取分布式文件系统hdfs
         FileSystem fileSystem = FileSystem.get(new URI("hdfs://node01:8020"), new 
         Configuration(), "hadoop");
         FSDataOutputStream fsDataOutputStream = fileSystem.create(new 
         Path("hdfs://node01:8020/bigfile.xml"));
         //获取本地文件系统 localFileSystem
         LocalFileSystem localFileSystem = FileSystem.getLocal(new Configuration());
         //读取本地的文件
         FileStatus[] fileStatuses = localFileSystem.listStatus(new Path("file:///D:test/"));
         for (FileStatus fileStatus : fileStatuses) {
             //获取每一个本地的文件路径
             Path path = fileStatus.getPath();
             //读取本地小文件
             FSDataInputStream fsDataInputStream = localFileSystem.open(path);
             IOUtils.copy(fsDataInputStream,fsDataOutputStream);
             IOUtils.closeQuietly(fsDataInputStream);
        }
         IOUtils.closeQuietly(fsDataOutputStream);
         localFileSystem.close();
         fileSystem.close();
         //读取所有本地小文件,写入到hdfs的大文件里面去
    }

此博文仅供学习参考,如有错误欢迎指正。

上一篇《大数据-HDFS(一)

下一篇《大数据-HDFS(三)

希望对大数据相关技术感兴趣的友友们关注一下,大家可以一起交流学习哦~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值