Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MyHadoopUtils {
- final static Logger LOGGER = Logger.getLogger(MyHadoopUtils.class);
- private static FileSystem fs;
- static{
- boolean initiated=false;
- Configuration myConf = new Configuration();
- //this is namenode url
- myConf.set("fs.defaultFS", CalculateLevelFiles.getDEFAULT_FS());
- LOGGER.info("Started using " + CalculateLevelFiles.getDEFAULT_FS());
- try {
- fs = FileSystem.get(myConf);
- initiated=true;
- } catch (IOException e) {
- LOGGER.fatal("ATTENTION! an error occurred when trying to instantiate HDFS handler!" + e.getLocalizedMessage());
- e.printStackTrace();
- }finally{
- LOGGER.debug("Completed. initiated? " + initiated);
- }
- }
- /***
- * a method that writes data provided by the StringBuffer to a path.
- * The path must exists
- * @param path the absolute path within HDFS
- * @param sb the buffer with data
- * @return true if ok, false if not
- */
- public static boolean saveWithinHDFS(String path,StringBuffer sb){
- LOGGER.debug("Started with path: " + path);
- boolean isOk=false;
- Path pt = new Path(path);
- try {
- BufferedWriter br = new BufferedWriter(new OutputStreamWriter(fs.create(pt, true)));
- br.write(sb.toString());
- br.close();
- isOk=true;
- String [] adata = sb.toString().split("\n");
- //LOGGER.info("ATTENTION A file has been written to HDFS! path: " + path);
- LOGGER.info(adata!=null && adata.length > 0?adata.length:"Zero " + " records written to " + path);
- } catch (IOException e) {
- LOGGER.warn("ATTENTION! it is been imposible to save a file within HDFS!. path: " + path);
- e.printStackTrace();
- } finally{
- LOGGER.debug("Completed isOk: " + isOk);
- }
- return isOk;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement