09_java访问Hadoop的HDFS

09_java访问Hadoop的HDFS

项目说明:本项目基于maven jdk8
《POM.xml》

<?xml version="1.0" encoding="UTF-8"?>
<project>
  <modelVersion>4.0.0</modelVersion>

  <repositories>
    <repository>
      <id>central</id>
      <name>Central Repository</name>
      <url>https://repo.maven.apache.org/maven2</url>
      <layout>default</layout>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
  </repositories>

  <pluginRepositories>
    <pluginRepository>
      <id>central</id>
      <name>Central Repository</name>
      <url>https://repo.maven.apache.org/maven2</url>
      <layout>default</layout>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <releases>
        <updatePolicy>never</updatePolicy>
      </releases>
    </pluginRepository>
  </pluginRepositories>

  <build>
    <directory>${project.basedir}/target</directory>
    <outputDirectory>${project.build.directory}/classes</outputDirectory>
    <finalName>${project.artifactId}-${project.version}</finalName>
    <testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>
    <sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
    <scriptSourceDirectory>${project.basedir}/src/main/scripts</scriptSourceDirectory>
    <testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
    <resources>
      <resource>
        <directory>${project.basedir}/src/main/resources</directory>
      </resource>
    </resources>
    <testResources>
      <testResource>
        <directory>${project.basedir}/src/test/resources</directory>
      </testResource>
    </testResources>
    <pluginManagement>
      <!-- NOTE: These plugins will be removed from future versions of the super POM -->
      <!-- They are kept for the moment as they are very unlikely to conflict with lifecycle mappings (MNG-4453) -->
      <plugins>
        <plugin>
          <artifactId>maven-antrun-plugin</artifactId>
          <version>1.3</version>
        </plugin>
        <plugin>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>2.2-beta-5</version>
        </plugin>
        <plugin>
          <artifactId>maven-dependency-plugin</artifactId>
          <version>2.8</version>
        </plugin>
        <plugin>
          <artifactId>maven-release-plugin</artifactId>
          <version>2.5.3</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>

  <reporting>
    <outputDirectory>${project.build.directory}/site</outputDirectory>
  </reporting>

  <profiles>
    <!-- NOTE: The release profile will be removed from future versions of the super POM -->
    <profile>
      <id>release-profile</id>

      <activation>
        <property>
          <name>performRelease</name>
          <value>true</value>
        </property>
      </activation>

      <build>
        <plugins>
          <plugin>
            <inherited>true</inherited>
            <artifactId>maven-source-plugin</artifactId>
            <executions>
              <execution>
                <id>attach-sources</id>
                <goals>
                  <goal>jar</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <inherited>true</inherited>
            <artifactId>maven-javadoc-plugin</artifactId>
            <executions>
              <execution>
                <id>attach-javadocs</id>
                <goals>
                  <goal>jar</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <inherited>true</inherited>
            <artifactId>maven-deploy-plugin</artifactId>
            <configuration>
              <updateReleaseInfo>true</updateReleaseInfo>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>

</project>
 * Hello world!
 *使用Java程序读取hadoop的存储上的文件
 */
public class HelloHDFS
{

    public static void main(String[] args) throws IOException {
        commonReadFun();
    }
    public static  void   commonReadFun() throws IOException {
       //创建配置对象
        Configuration conf = new Configuration();
        //设置dfs的连接信息
        conf.set("fs.defaultFS","hdfs://192.168.40.57:9000");
       //通过连接信息 得到文件系统
        FileSystem fileSystem = FileSystem.get(conf);
//        //使用文件系统在hdfs的根目录下创建目录lanqiao   覆盖创建
//        boolean success = fileSystem.mkdirs(new Path("/lanqiao"));
//        System.out.println(success);
//        //判断文件是否存在
//        success =fileSystem.exists(new Path("/hello.txt"));
//        System.out.println(success);
//        //删除目录   参数一:文件路径   参数二:文件是否真正的从hdfs删除
//        success = fileSystem.delete(new Path("/lanqiao"),true);
//        System.out.println(success);
//        //检查目录是否存在
//       success =  fileSystem.exists(new Path("/lanqiao"));
//        System.out.println(success);
        //上传文件到hdfs
/*        FSDataOutputStream out =  fileSystem.create(new Path("/test.data"),true);
        FileInputStream in = new FileInputStream("d://test.log");
        IOUtils.copyBytes(in,out,1024,true);*/
        //获取指定目录下的文件列表
        FileStatus[] fileStatus = fileSystem.listStatus(new Path("/"));
        for (FileStatus fs :fileStatus){
            System.out.println(fs.getPath());//文件路径
            System.out.println(fs.getPermission());//文件的读写权限
            System.out.println(fs.getReplication());//文件分几块

        }


    }

    public static  void  firstReade() throws IOException {
        /**
         * 第一种方式
         */
        //由于URL默认只支持http协议,而hadoop的HDFS使用的是HDFS协议,所以在这里设置URL,使其支持hdfs协议
        URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());
        //使用URL 访问HDFS 使用协议为hdfs   此时的hello.txt存在于hadoop存储的根目录下
        URL url = new URL("hdfs://master:9000/hello.txt");
        //调用url的openStrem()方法获取一个InputStrem
        InputStream in = url.openStream();
        //使用hadoop提供的IOUtils的copyBytes(输入流,输出流,缓冲区大小,流是否在使用完之后自动关闭)
        IOUtils.copyBytes(in ,System.out,1024,true);
    }
}





版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/111278.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • ESP8266简介:三种编程方式「建议收藏」

    ESP8266简介:三种编程方式「建议收藏」随着互联网的日益发展,智能家居的观念也逐渐深入人心。想要玩转智能家居,就离不开互联网,今天给大家介绍一款模块——ESP8266。小编将手把手教大家利用8266实现家电的控制。ESP8266可以用来做串口透传,PWM调控,远程控制开关:控制插座、开关、电器等。该模块有三种工作模式,大家可以根据自己的具体情况来选择:STA模式:ESP8266模块通过路由器连接互联网,手机或电脑通过互联网…

  • 狂神说Vue笔记整理「建议收藏」

    狂神说Vue笔记整理「建议收藏」狂神说Vue笔记​ 想要成为真正的“互联网Java全栈工程师”还有很长的一段路要走,其中前端是绕不开的一门必修课。本阶段课程的主要目的就是带领Java后台程序员认识前端、了解前端、掌握前端,为实现成为“互联网Java全栈工程师”再向前迈进一步。一、前端核心分析1.1、概述Soc原则:关注点分离原则Vue的核心库只关注视图层,方便与第三方库或既有项目整合。HTML+CSS+JS:视图:给用户看,刷新后台给的数据网络通信:axios页面跳转:vue-router状态管

  • VirtualBox安装Mac OS 10.11——虚拟机安装黑苹果

    VirtualBox安装MacOS10.11,安装日期:2016/5/14用虚拟机装黑苹果本人也装了不下3次了,这次为了做这个教程还特意把virtualbox和旧版的MacOS删了,重新再装一遍。所以保证能运行,不像网上其他教程都是导出复制,还不要脸的贴个原创。VirtualBox是官网下的最新版:5.0.20forWindowshostsx

  • leapftp乱码_如何用网格本做笔记

    leapftp乱码_如何用网格本做笔记生活对我下了手2019年7月23星期二大晴天1.主要掌握怎么连接服务器2.单个文件上传3.整个文件夹上传leapftp界面主要功能板块介绍1.管理ftp服务器配置的地方2.服务器网站文件窗口界面3.上传状态的窗口界面4.正在上传的文件窗口界面5.本地电脑文件窗口界面怎么连接ftp服务器服务器上要有ftp服务,1.你要有ftp服务器的账号,2.你要有ftp服务器的密…

    2022年10月28日
  • 条件运算符嵌套_条件运算符(?:)的运算过程是什么

    条件运算符嵌套_条件运算符(?:)的运算过程是什么条件运算符(?:)都知道就不细说了,最简单也是最常用的例子:n=(a>b)?a:b;    在C++primerplus第六版180页看到了一个例子,如下所示:    constcharx[2][20]={“Jason”,”atyourservices\n”}    constchar*y=”Quillstone”;

  • java socket发送中文乱码_java Socket接收数据乱码问题「建议收藏」

    java socket发送中文乱码_java Socket接收数据乱码问题「建议收藏」));}问题:1.此出输出的数据与我发送的数据不一致2.如果我用strSql=String.valueOf(buffer,0,nDataLen-1);则输出的是方块3.同样我用另外一个程序测试端口6789的数据,打印出来的也是方块,不知道是什么原因,请各位老大帮帮忙分析一下原因,三叩首了!!![/B]测试程序:importjava.nio.channels.ServerSocketCh…

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号