大数据-HDFS(三)

陌路散爱 提交于 2020-02-14 12:29:44

                                     大数据-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操作

1、创建文件夹

@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("/kaikeba/dir1"));
    fileSystem.close();
 }

2、文件上传

 @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/kaikeba/dir1"));
       fileSystem.close();
   }

3、文件下载

@Test
public void downloadFile() throws IOException {
       Configuration configuration = new Configuration();
       configuration.set("fs.defaultFS","hdfs://node01:8020");
       FileSystem fileSystem = FileSystem.get(configuration);
       fileSystem.copyToLocalFile(new Path("hdfs://node01:8020/kaikeba/dir1/hello.txt"),new 
       Path("file:///d:\\hello2.txt"));
       fileSystem.close();
   }

4、自主完成hdfs文件删除操作

5、自主完成hdfs文件重命名操作

6、查看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 的 javaAPI 操作延伸阅读之通过IO流操作 hdfs 上面的文件

1、通过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("file:///e:\\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();
    }

2、自主实现通过IO流从hdfs上面下载文件

3、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:\\开课吧课程资料\\Hadoop&ZooKeeper课件\\最新版本课件\\hadoop与zookeeper课件资料\\1、第一天\\小文件合并"));
         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的大文件里面去
    }

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!