前言:
要想在eclipse中操作HDFS,就必须先安装hadoop插件,hadoop插件安装教程
1、先介绍最简单的方法,该方法虽然简单,但只能用一次,因此不建议。
public class hadoopTest { // 因为satic是静态独占型的,所以只能执行一次。 static { // 让java虚拟机能够识别Hadoop的hdfs URL 方案 URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory()); } /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub String address = "hdfs://node1:9000/input2/file3.txt"; // 输入流 InputStream in = null; try { in = new URL(address).openStream();// 用IO文件流来读 IOUtils.copyBytes(in, System.out, 512, true); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } /* * finally{ IOUtils.closeStream(in); } */ } }
2、使用hadoop的FileSystem API(最常用)
public class hadoopTest { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { //注意:首先要把hadoop/etc/hadoop目录下的配置文件core-site.xml,复制到项目的src目录里 //获取本地配置信息core-site.xml Configuration conf=new Configuration(); //获取文件系统实例 FileSystem fs=FileSystem.get(conf); try { //1、(从到数据库中)读取文件 String address="hdfs://node1:9000/input2/file3.txt"; Path path=new Path(address) InputStream in=fs.open(path);//用hadoop FileSystem API来读 IOUtils.copyBytes(in, System.out, 512, true); //2、创建文件(到数据库中) String address="hdfs://node1:9000/input2/file3.txt"; Path path=new Path(address); FSDataOutputStream outputStream=fs.create(path); InputStream is=new ByteArrayInputStream("这里是文件内容".getBytes("UTF-8")); IOUtils.copyBytes(is, outputStream, 512, true); //3、上传文件(到数据库中) String srcFile="/home/sa/abc.txt";//要复制的文件路径 String dstFile="hdfs://node1:9000/input2/";//要复制到的数据库位置 Path srcPath=new Path(srcFile); Path dstPath=new Path(dstFile); fs.copyFromLocalFile(srcPath, dstPath); //4、(从数据库中)下载文件 String fromFile="hdfs://node1:9000/input2/123.txt";//要下载的文件路径 String toFile="/home/sa/";//要下载到哪个位置 Path fromPath=new Path(fromFile); Path toPath=new Path(toFile); fs.copyToLocalFile(fromPath, toPath); //5、删除文件 String file="hdfs://node1:9000/input2/123.txt"; Path path=new Path(file); fs.delete(path,false);//如果要删除文件夹,就设置为true(递归删除) //6、重命名数据库文件 String fromFile="hdfs://node1:9000/input2/111.txt"; String toFile="hdfs://node1:9000/input2/777.txt"; Path fromPath=new Path(fromFile); Path toPath=new Path(toFile); fs.rename(fromPath, toPath); //7、文件查询 String file="hdfs://node1:9000/input2/music1.txt"; Path path=new Path(file); FileStatus status=fs.getFileStatus(path); System.out.println("文件路径:"+status.getPath()); System.out.println("文件块大小:"+convertFileSize(status.getBlockSize())); System.out.println("文件大小:"+convertFileSize(status.getLen())); System.out.println("副本数量:"+status.getReplication());//备份数量 System.out.println("用户:"+status.getOwner()); System.out.println("用户组:"+status.getGroup()); System.out.println("权限:"+status.getPermission().toString()); //8、查询文件夹列表信息 String file="hdfs://node1:9000/input2/"; Path path=new Path(file); FileStatus[] status= fs.listStatus(path); if(fs.exists(path)){ //该目录存在 for (FileStatus item:status) { System.out.println("文件路径:"+item.getPath()); System.out.println("文件大小:"+convertFileSize(item.getLen())); System.out.println("用户:"+item.getOwner()); System.out.println("用户组:"+item.getGroup()); System.out.println("权限:"+item.getPermission().toString()); } }else{ //该目录不存在 System.out.println("该目录不存在"); } */ //9、检查目录是否存在 String file="hdfs://node1:9000/input3/"; Path path=new Path(file); if(fs.exists(path)){ //该目录存在 System.out.println("该目录存在!"); }else{ //该目录不存在 System.out.println("该目录不存在"); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } // 转换文件大小格式 public static String convertFileSize(long size) { long kb = 1024; long mb = kb * 1024; long gb = mb * 1024; if (size >= gb) { return String.format("%.1f GB", (float) size / gb); } else if (size >= mb) { float f = (float) size / mb; return String.format(f > 100 ? "%.0f MB" : "%.1f MB", f); } else if (size >= kb) { float f = (float) size / kb; return String.format(f > 100 ? "%.0f KB" : "%.1f KB", f); } else return String.format("%d B", size); } }