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;
- private static boolean isFS=false;
- static{
- Configuration myConf = new Configuration();
- myConf.set("fs.defaultFS", MyWatchService.getDEFAULTFS());
- //myConf.set("fs.hdfs.impl", org.apache.hadoop.hdfs.DistributedFileSystem.class.getName());
- //myConf.set("fs.file.impl", org.apache.hadoop.fs.LocalFileSystem.class.getName());
- try {
- fs = FileSystem.get(myConf);
- LOGGER.info("HDFS instantiated! name: " + fs.getName());
- isFS=true;
- }
- catch (IOException e) {
- e.printStackTrace();
- LOGGER.error("ATTENTION! an error occurred when trying to instantiate a hadoop file system handler!. " + e.getLocalizedMessage());
- }
- catch (Exception e) {
- e.printStackTrace();
- LOGGER.error("ATTENTION! an error occurred when trying to instantiate a hadoop file system handler!. " + e.getLocalizedMessage());
- } catch (Throwable th) {
- th.printStackTrace();
- LOGGER.error("ATTENTION! an error occurred when trying to instantiate a hadoop file system handler!. " + th.getLocalizedMessage());
- }finally{
- LOGGER.info("HDFS fs instantiated? " + isFS);
- }
- }
- public static boolean saveWithinHDFS(String path,StringBuffer sb) throws IOException{
- boolean _isOk=false;
- if (!isFS){
- LOGGER.warn("ATTENTION, HDFS fs is not available! Check with IT.");
- return _isOk;
- }
- Path pt = new Path(path);
- try {
- BufferedWriter br = new BufferedWriter(new OutputStreamWriter(fs.create(pt, true)));
- br.write(sb.toString());
- br.close();
- _isOk=true;
- } catch (IOException e) {
- // TODO Auto-generated catch block
- LOGGER.info("ATTENTION! it is been imposible to save a file within HDFS!. path: " + path);
- e.printStackTrace();
- throw e;
- }
- return _isOk;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement