zoukankan      html  css  js  c++  java
  • Hadoop HDFS 用java API 进行读写

    public class HdfsApp {
    
        public static FileSystem getFileSystem() throws Exception {
    
            Configuration conf = new Configuration();
    //将配置文件copy 到资源文件
    // cp /opt/modules/hadoop-2.5.0/etc/hadoop/core-site.xml /opt/modules/hadoop-2.5.0/etc/hadoop/hdfs-site.xml /opt/tools/eclipse-workspace/hadoop/src/main/resources
    // 加载日志配置
    // cp /opt/modules/hadoop-2.5.0/etc/hadoop/log4j.properties /opt/tools/eclipse-workspace/hadoop/src/main/resources/ // create a filesystem FileSystem fileSystem = FileSystem.get(conf); return fileSystem; } // read data from hdfs public static void read(String filename) throws Exception { FileSystem fileSystem = getFileSystem(); Path path = new Path(filename); FSDataInputStream fis = fileSystem.open(path); try { IOUtils.copyBytes(fis, System.out, 4096, false); } catch (Exception e) { e.printStackTrace(); } finally { IOUtils.closeStream(fis); } } //write data in hdfs public static void write(String writeFileName) throws Exception { FileSystem fileSystem = getFileSystem(); // hdfs site Path path = new Path(writeFileName); FSDataOutputStream fos = fileSystem.create(path); FileInputStream fis = new FileInputStream(new File("/opt/modules/hadoop-2.5.0/hdfs.input")); try { IOUtils.copyBytes(fis, fos, 4096, false); } catch (Exception e) { e.printStackTrace(); } finally { IOUtils.closeStream(fis); IOUtils.closeStream(fos); } } public static void main(String[] args) throws Exception { // String filename = "/user/chris/mapreduce/wordcount/input/wc.input"; // read(filename); String writeFileName = "/user/chris/put-wc.input"; write(writeFileName); } }

    maven  Denpendency

     <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <hadoop.version>2.5.0</hadoop.version>
      </properties>
    
      <dependencies>
        <dependency>
          <groupId>org.apache.hadoop</groupId>
          <artifactId>hadoop-client</artifactId>
          <version>${hadoop.version}</version>
        </dependency>
        <dependency>
           <groupId>junit</groupId>
           <artifactId>junit</artifactId>
           <version>4.10</version>
        </dependency>
      </dependencies>
  • 相关阅读:
    HDU 6076
    HDU 6069
    ZOJ
    HDU
    斜率优化dp
    UVA 11752 The Super Powers
    poj 3761 bubble sort (排列组合)
    UVA 11174 Stand in a Line
    Caffe初学者第二部:Ubuntu16.04上安装caffe(CPU)+Matlab2014a+Opencv3的详细过程 (亲测成功, 20180529更新)
    Caffe初学者第一部:Ubuntu14.04上安装caffe(CPU)+Python的详细过程 (亲测成功, 20180524更新)
  • 原文地址:https://www.cnblogs.com/pickKnow/p/10756022.html
Copyright © 2011-2022 走看看