首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 开源软件 >

Hadoop学习札记-文件新增、删除

2012-07-19 
Hadoop学习笔记-文件新增、删除版本:hadoop-0.21.0OS:Unix(Mac OS X)package cn.com.fri.hadoopimport jav

Hadoop学习笔记-文件新增、删除
版本:hadoop-0.21.0
OS:Unix(Mac OS X)

package cn.com.fri.hadoop;import java.io.FileNotFoundException;import java.io.IOException;import java.net.URI;import java.util.Date;import org.apache.hadoop.fs.FSDataInputStream;import org.apache.hadoop.fs.FileContext;import org.apache.hadoop.fs.FileStatus;import org.apache.hadoop.fs.Path;import org.apache.hadoop.fs.UnsupportedFileSystemException;import org.apache.hadoop.io.IOUtils;import org.apache.hadoop.security.AccessControlException;/** * 读取文件内容以及文件信息 * @author alex * */public class MainRead {public static void main(String[] args) throws AccessControlException, FileNotFoundException, UnsupportedFileSystemException, IOException {FileContext fc = FileContext.getFileContext(URI.create("hdfs://localhost:9000"));FSDataInputStream fsInput = fc.open(new Path("test"));IOUtils.copyBytes(fsInput, System.out, 4090,false);FileStatus status = fc.getFileStatus(new Path("test"));System.out.println("--File status---");System.out.println(new Date(status.getAccessTime()));System.out.println(status.getPath().toUri().getPath());double i = 1024.00;Double len = new Double(status.getLen());len = len/i;System.out.println("File length:"+len+"kb");System.out.println("Block size:"+status.getBlockSize()/1024/1024+"mb");System.out.println(status.getOwner());}}


package cn.com.fri.hadoop;import java.io.BufferedInputStream;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.net.URI;import java.util.EnumSet;import org.apache.hadoop.fs.CreateFlag;import org.apache.hadoop.fs.FSDataOutputStream;import org.apache.hadoop.fs.FileAlreadyExistsException;import org.apache.hadoop.fs.FileContext;import org.apache.hadoop.fs.ParentNotDirectoryException;import org.apache.hadoop.fs.Path;import org.apache.hadoop.io.IOUtils;import org.apache.hadoop.security.AccessControlException;/** * 创建一个文件 * @author alex * */public class MainCreate {public static void main(String[] args) throws AccessControlException, FileAlreadyExistsException, FileNotFoundException, ParentNotDirectoryException, IOException {FileContext fc = FileContext.getFileContext(URI.create("hdfs://localhost:9000"));EnumSet<CreateFlag> es = EnumSet.noneOf(CreateFlag.class);es.add(CreateFlag.CREATE);FSDataOutputStream out = fc.create(new Path("test"), es);InputStream in = new BufferedInputStream(new FileInputStream("/Users/alex/Desktop/persons.rtf"));IOUtils.copyBytes(in, out, 4090,true);}}



在new Path的时候,文件如果加上/则代表放入根路径,否则放入/user/name/路径下,如果hdfs中不存在此路径,则抛出父目录不存在的异常。

热点排行