1. 从HDFS中读取文件内容
使用URL
import org.apache.hadoop.fs.FsUrlStreamHandlerFactory; import org.apache.hadoop.io.IOUtils; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; /** * Created by zhangyan on 2017/1/4. */ public class URLCat {
static{ URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory()); } public static void main(String[] args) throws MalformedURLException,IOException{ InputStream in=null; try{ in = new URL(args[0]).openStream(); IOUtils.copyBytes(in, System.out, 4096, false); } finally{ IOUtils.closeStream(in); } }}/** * 要从Hadoop文件系统中读取文件,最简单的方法是使用java.net.URL对象打开数据流,进而从中读取数据 * 要让Java程序能够识别Hadoop的hdfs URL, * 需要采用的方法是 FsUrlStreamHandlerFactory实例作为URL中setURLStreamHandlerFactory方法的参数。 * 由于Java虚拟机只能调用一次上述方法,因此通常在静态方法 中调用上述方法。 * * 没有以下的static块中的内容,会报出异常 java.net.MalformedURLException: unknown protocol: hdfs */
有时无法在应用中设置URLStreamHandlarFactory实例,例如不受控制的第三方组件如果已经声明了一个URLStreamHandlerFactory实例,你将无法再使用上述方法从Hadoop中读取数据,这种情况下,需要使用FileSystem API来打开一个文件的输入流。
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IOUtils; import java.io.InputStream; /** * Created by zhangyan on 2017/1/5. */ /** * Hadoop的Filesystem类就是与Hadoop的某一文件系统进行交互的 APl, * Filesystem类是一个抽象类,是需要被继承的,所以只能通过get方法得到具体的类 * * FileSystem是一个通用的文件系统API,所以第一步是检索需要使用的文 件系统实例,这里是HDFS */ public class FileSystemCat { public static void main(String[] args) throws Exception { String uri = args[0]; Configuration conf = new Configuration(); //以下两句定义FileSystem对象的方法都可以读取HDFS内文件的内容 //FileSystem fs = FileSystem.get(URI.create(uri), conf); FileSystem fs = FileSystem.get(conf); //创建目录 fs.mkdirs(new Path("hdfs://myhadoop/apiPath")); InputStream in = null; try { //调用open()函数来获取文件的输入流 in = fs.open(new Path(uri)); IOUtils.copyBytes(in, System.out, 4096, false); } finally { IOUtils.closeStream(in); } } }
2. 向HDFS中写入数据import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IOUtils; import java.io.*; import java.net.URI; /** * Created by zhangyan on 2017/1/4. */ public class FileCopyToHDFS { public static void main(String[] args) throws IOException{ String localSrc = args[0]; String dst = args[1]; InputStream in=new BufferedInputStream(new FileInputStream(localSrc)); Configuration conf=new Configuration(); //创建FileSystem对象。可以使用URI,也可以不使用URI //FileSystem fs=FileSystem.get(URI.create(dst),conf); FileSystem fs=FileSystem.get(conf); // 创建写入数据的输出流 // 给准备创建的文件指定一个Path对象 // create()方法能够为需要写入且当前不存在的文件创建父目录 OutputStream out=fs.create(new Path(dst)); IOUtils.copyBytes(in, out, 4096, true); } }
3. Delete HDFS中的Path
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import java.io.IOException; /** * Created by zhangyan on 2017/1/4. */ public class MyUtils { public static void deleteDir(Configuration conf, String dirPath) throws IOException{ FileSystem fs=FileSystem.get(conf); Path target=new Path(dirPath); //判断Path对象在FileSystem中是否存在 if(fs.exists(target)){ //删除路径 // 如果是directory的话,第二个参数为true则删除文件夹及其包含的文件,为false则抛异常 // 如果是文件的话,第二个参数为true或false都可以。。 boolean delResult = fs.delete(target,true); if(delResult){ System.out.println(target + " has been deleted sucessfullly."); } else { System.out.println(target + " deletion failed."); } } } }
4. FileStatus,包含了文件元信息import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileStatus; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import java.io.IOException; /** * Created by zhangyan on 2017/1/5. */ public class TestFileStatus { public static void main(String[] args) throws IOException{ Configuration conf=new Configuration(); FileSystem fs = FileSystem.get(conf); FileStatus fstat= fs.getFileStatus(new Path(args[0])); System.out.println(fstat.getAccessTime()); System.out.println(fstat.getBlockSize()); System.out.println(fstat.getLen()); System.out.println(fstat.getReplication()); System.out.println(fstat.getOwner()); } }