Cloudera CDH/CDP 및 Hadoop EcoSystem, Semantic IoT등의 개발/운영 기술을 정리합니다. gooper@gooper.com로 문의 주세요.
Hadoop 2.7.x에서 사용할 수 있는 파일/디렉토리 관련 utils
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FSDataInputStream; import org.apache.hadoop.fs.FSDataOutputStream; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; public class HDFSUtils { /** * create a existing file from local filesystem to hdfs * @param source * @param dest * @param conf * @throws IOException */ public void addFile(String source, String dest, Configuration conf) throws IOException { FileSystem fileSystem = FileSystem.get(conf); // Get the filename out of the file path String filename = source.substring(source.lastIndexOf('/') + 1,source.length()); // Create the destination path including the filename. if (dest.charAt(dest.length() - 1) != '/') { dest = dest + "/" + filename; } else { dest = dest + filename; } // System.out.println("Adding file to " + destination); // Check if the file already exists Path path = new Path(dest); if (fileSystem.exists(path)) { System.out.println("File " + dest + " already exists"); return; } // Create a new file and write data to it. FSDataOutputStream out = fileSystem.create(path); InputStream in = new BufferedInputStream(new FileInputStream(new File( source))); byte[] b = new byte[1024]; int numBytes = 0; while ((numBytes = in.read(b)) > 0) { out.write(b, 0, numBytes); } // Close all the file descriptors in.close(); out.close(); fileSystem.close(); } /** * read a file from hdfs * @param file * @param conf * @throws IOException */ public void readFile(String file, Configuration conf) throws IOException { FileSystem fileSystem = FileSystem.get(conf); Path path = new Path(file); if (!fileSystem.exists(path)) { System.out.println("File " + file + " does not exists"); return; } FSDataInputStream in = fileSystem.open(path); String filename = file.substring(file.lastIndexOf('/') + 1, file.length()); OutputStream out = new BufferedOutputStream(new FileOutputStream( new File(filename))); byte[] b = new byte[1024]; int numBytes = 0; while ((numBytes = in.read(b)) > 0) { out.write(b, 0, numBytes); } in.close(); out.close(); fileSystem.close(); } /** * delete a directory in hdfs * @param file * @throws IOException */ public void deleteFile(String file, Configuration conf) throws IOException { FileSystem fileSystem = FileSystem.get(conf); Path path = new Path(file); if (!fileSystem.exists(path)) { System.out.println("File " + file + " does not exists"); return; } fileSystem.delete(new Path(file), true); fileSystem.close(); } /** * create directory in hdfs * @param dir * @throws IOException */ public void mkdir(String dir, Configuration conf) throws IOException { FileSystem fileSystem = FileSystem.get(conf); Path path = new Path(dir); if (fileSystem.exists(path)) { System.out.println("Dir " + dir + " already exists"); return; } else { fileSystem.mkdirs(path); fileSystem.close(); } } /** * delete directory in hdfs * @param dir * @throws IOException */ public void rmdir(String dir, Configuration conf) throws IOException { FileSystem fileSystem = FileSystem.get(conf); Path path = new Path(dir); if (fileSystem.exists(path)) { fileSystem.delete(path, true); fileSystem.close(); } else { System.out.println("Dir " + dir + " not exists"); } } /* public static void main(String[] args) throws IOException { if (args.length < 1) { System.out.println("Usage: hdfsclient add/read/delete/mkdir" + " [<local_path> <hdfs_path>]"); System.exit(1); } FileSystemOperations client = new FileSystemOperations(); String hdfsPath = "hdfs://" + args[0] + ":" + args[1]; Configuration conf = new Configuration(); // Providing conf files // conf.addResource(new Path(HDFSAPIDemo.class.getResource("/conf/core-site.xml").getFile())); // conf.addResource(new Path(HDFSAPIDemo.class.getResource("/conf/hdfs-site.xml").getFile())); // (or) using relative paths // conf.addResource(new Path( // "/u/hadoop-1.0.2/conf/core-site.xml")); // conf.addResource(new Path( // "/u/hadoop-1.0.2/conf/hdfs-site.xml")); //(or) // alternatively provide namenode host and port info conf.set("fs.default.name", hdfsPath); if (args[0].equals("add")) { if (args.length < 3) { System.out.println("Usage: hdfsclient add <local_path> " + "<hdfs_path>"); System.exit(1); } client.addFile(args[1], args[2], conf); } else if (args[0].equals("read")) { if (args.length < 2) { System.out.println("Usage: hdfsclient read <hdfs_path>"); System.exit(1); } client.readFile(args[1], conf); } else if (args[0].equals("delete")) { if (args.length < 2) { System.out.println("Usage: hdfsclient delete <hdfs_path>"); System.exit(1); } client.deleteFile(args[1], conf); } else if (args[0].equals("mkdir")) { if (args.length < 2) { System.out.println("Usage: hdfsclient mkdir <hdfs_path>"); System.exit(1); } client.mkdir(args[1], conf); } else { System.out.println("Usage: hdfsclient add/read/delete/mkdir" + " [<local_path> <hdfs_path>]"); System.exit(1); } System.out.println("Done!"); } */ }
댓글 0
번호 | 제목 | 날짜 | 조회 수 |
---|---|---|---|
610 | 부팅을 외장하드에서 하도록 변경하는 방법 | 2015.07.28 | 3453 |
609 | pom.xml에서 build.gradle로 변환 | 2015.09.14 | 3261 |
608 | mongodb 2.6.6 설치(64bit) | 2015.09.30 | 2633 |
607 | SQL문장과 Mongo에서 사용하는 명령어를 비교한 것입니다. | 2015.09.30 | 4149 |
606 | root계정으로 MariaDB설치후 mysql -u root -p로 db에 접근하여 바로 해줘야 하는일..(케릭터셑은 utf8) | 2015.10.02 | 3732 |
605 | DB별 JDBC 드라이버 | 2015.10.02 | 4178 |
604 | Mybatis foreach 문법정리(상황에 따른 사용법) | 2015.11.10 | 4909 |
603 | mybais #과 $의 차이점 | 2015.11.10 | 3674 |
602 | Resource temporarily unavailable(자원이 일시적으로 사용 불가능함) 오류조치 | 2015.11.19 | 11622 |
601 | Runtime.getRuntime().exec(cmd) sample 소스 | 2015.11.19 | 2649 |
600 | sparql에서 concat에제 | 2015.11.27 | 3029 |
599 | 마이바티스(MyBatis)쿼리로그 출력및 정렬하기 | 2015.12.01 | 4346 |
598 | ontology, jena, sparql등 전반에 대한 설명및 예제를 제공하는 사이트 | 2015.12.08 | 2601 |
597 | protege 4.3 다운로드 | 2015.12.09 | 2391 |
596 | git설명 한글판 | 2015.12.09 | 2829 |
595 |
sparql 문법구조 설명
![]() | 2015.12.09 | 3335 |
594 | 대표 오픈소스 라이선스, 한 눈에 보기! | 2015.12.10 | 3079 |
593 | 천문학적, 기후학적, 기상학적, 생물학적, 농사계절 구분 | 2015.12.16 | 2934 |
592 | java quartz 시간 설정 참고사항 | 2015.12.16 | 3147 |
591 | console명령과 API비교 | 2015.12.21 | 4731 |