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); } }
  • 相关阅读:
    Github 代码在线在vscode中打开
    TP5如何查询字段为空
    浏览器总是报 'https://static.hae123.cn/gc/gc3.js 错误
    Sublime 复制到word 如何保留样式?
    pdf 在浏览器中是下载,而不是打开如何实现?
    Shell脚本中的set指令,比如set -x 和 set -e【转】
    Python面向对象进阶【转】
    Redis为什么变慢了?常见延迟问题定位与分析【转】
    史上最全的 Linux Shell 文本处理工具集锦【转】
    Linux运维常用命令总结【转】
  • 原文地址:https://www.cnblogs.com/i80386/p/3438795.html
Copyright © 2011-2022 走看看