zoukankan      html  css  js  c++  java
  • Hadoop 学习笔记(一) HDFS API


    http://www.cnblogs.com/liuling/p/2013-6-17-01.html 这个也不错
    http://www.teamwiki.cn/hadoop/thrift thrift编程

    1.上传本地文件到HDFS

    package
    proj; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileStatus; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; public class CopyFile { public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); //要点:没有这句会传到本地文件系统,而不是hdfs conf.set("fs.default.name","hdfs://localhost:9000"); FileSystem hdfs = FileSystem.get(conf); Path src = new Path("/codes/c/hello.c"); Path dst = new Path("in"); hdfs.copyFromLocalFile(src, dst); System.out.println("Upload to" + conf.get("fs.default.name")); FileStatus[] files = hdfs.listStatus(dst); for (FileStatus fileStatus : files) { System.out.println(fileStatus.getPath()); } } }
    2.创建HDFS文件
    package
    proj; import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FSDataOutputStream; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; public class CreateFile { public static void main(String[] args) throws IOException { Configuration conf = new Configuration(); //要点:没有这句会传到本地文件系统,而不是hdfs conf.set("fs.default.name","hdfs://localhost:9000"); FileSystem hdfs = FileSystem.get(conf); Path dfs = new Path("in/test.txt"); FSDataOutputStream outputStream = hdfs.create(dfs); byte[] buff = "hello prince of persia".getBytes(); outputStream.write(buff, 0, buff.length); System.out.println("run over"); } }
    3.重命名
    package
    proj; import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; public class Rename { public static void main(String[] args) throws IOException { Configuration conf = new Configuration(); //要点:没有这句会传到本地文件系统,而不是hdfs conf.set("fs.default.name","hdfs://localhost:9000"); FileSystem hdfs = FileSystem.get(conf); Path frpath = new Path("in/test.txt"); Path topath = new Path("in/test3.txt"); boolean isRename = hdfs.rename(frpath, topath); System.out.println(isRename); } }
  • 相关阅读:
    牛逼哄哄的 RPC 框架,底层到底什么原理?
    你只会用 StringBuilder?试试 StringJoiner,真香!
    厉害了,淘宝千万并发,14 次架构演进…
    微服务中 Zookeeper 应用及原理
    牛逼哄哄的零拷贝是什么?
    排名前 16 的 Java 工具类,哪个你没用过?
    Spring Boot 2.4 正式发布,重大调整!!!
    pl/sql 实例精解 07
    ArcGIS 符号和样式
    SQL Server系统视图有什么用?
  • 原文地址:https://www.cnblogs.com/i80386/p/3438795.html
Copyright © 2011-2022 走看看