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); } }