Advertisement
aironman

MyHadoopUtils

Apr 8th, 2015
294
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class MyHadoopUtils {
  2.  
  3. final static Logger LOGGER = Logger.getLogger(MyHadoopUtils.class);
  4. private static FileSystem fs;
  5. private static boolean isFS=false;
  6. static{
  7. Configuration myConf = new Configuration();
  8. myConf.set("fs.defaultFS", MyWatchService.getDEFAULTFS());
  9. //myConf.set("fs.hdfs.impl", org.apache.hadoop.hdfs.DistributedFileSystem.class.getName());
  10. //myConf.set("fs.file.impl", org.apache.hadoop.fs.LocalFileSystem.class.getName());
  11. try {
  12. fs = FileSystem.get(myConf);
  13. LOGGER.info("HDFS instantiated! name: " + fs.getName());
  14. isFS=true;
  15. }
  16. catch (IOException e) {
  17. e.printStackTrace();
  18. LOGGER.error("ATTENTION! an error occurred when trying to instantiate a hadoop file system handler!. " + e.getLocalizedMessage());
  19. }
  20. catch (Exception e) {
  21. e.printStackTrace();
  22. LOGGER.error("ATTENTION! an error occurred when trying to instantiate a hadoop file system handler!. " + e.getLocalizedMessage());
  23. } catch (Throwable th) {
  24. th.printStackTrace();
  25. LOGGER.error("ATTENTION! an error occurred when trying to instantiate a hadoop file system handler!. " + th.getLocalizedMessage());
  26. }finally{
  27. LOGGER.info("HDFS fs instantiated? " + isFS);
  28. }
  29. }
  30.  
  31. public static boolean saveWithinHDFS(String path,StringBuffer sb) throws IOException{
  32. boolean _isOk=false;
  33. if (!isFS){
  34. LOGGER.warn("ATTENTION, HDFS fs is not available! Check with IT.");
  35. return _isOk;
  36. }
  37. Path pt = new Path(path);
  38. try {
  39. BufferedWriter br = new BufferedWriter(new OutputStreamWriter(fs.create(pt, true)));
  40. br.write(sb.toString());
  41. br.close();
  42. _isOk=true;
  43. } catch (IOException e) {
  44. // TODO Auto-generated catch block
  45. LOGGER.info("ATTENTION! it is been imposible to save a file within HDFS!. path: " + path);
  46. e.printStackTrace();
  47. throw e;
  48. }
  49. return _isOk;
  50. }
  51. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement