package com.intel.alex.Utils;

import java.io.*;
import java.text.DecimalFormat;
import java.util.Map;

/**
 * Created by root on 3/2/16.
 */
public class HadoopConfUtil {
    private static final String[] confArray={"yarn.scheduler.maximum-allocation-mb","yarn.scheduler.minimum-allocation-mb",
            "yarn.resourcemanager.scheduler.class","yarn.nodemanager.resource.memory-mb",
            "yarn.nodemanager.resource.cpu-vcores","yarn.app.mapreduce.am.resource.mb",
            "mapreduce.map.memory.mb","mapreduce.reduce.memory.mb",
            "mapreduce.map.java.opts","mapreduce.reduce.java.opts","mapreduce.job.reduce.slowstart.completedmaps"};
    private static final String[] paramArray={"schedulermax","schedulermin","schedulerclass","containermemory","cpuvcores","mapreduceamresource","mapmemory",
            "reducememory","mapopts","reduceopts","slowstart"};
    private static final File yarnFile=new File("yarn-site.xml");
    private static final File mapFile=new File("/etc/hadoop/conf/mapred-site.xml");
    private static final File hadoopEnvFile=new File("/etc/hadoop/conf/hadoop-env.sh");
    private static final File hiveEnvFile=new File("/etc/hive/conf/hive-env.sh");

    public void getHadoopConfiguration(Map<String, Object> dataMap){
        org.apache.hadoop.conf.Configuration conf=new org.apache.hadoop.conf.Configuration(true);
        InputStream yarnIn=null;
        InputStream mapredIn=null;
        BufferedReader hadoopEnvReader=null;
        BufferedReader hiveEnvReader=null;
        String hadoopEnv=null,hiveEnv=null;
        try{
            //get configurations from yarn-site.xml, mapred-site.xml
            yarnIn=new FileInputStream(yarnFile);
            mapredIn=new FileInputStream(mapFile);

            conf.addResource(yarnIn);
            conf.addResource(mapredIn);

            for(int i=0;i<confArray.length;i++)
                if(i==2||i==4||i==10)
                    dataMap.put(paramArray[i],conf.get(confArray[i]));
                else if(i==8||i==9){
                    String[] res=conf.get(confArray[i]).split(" ");
                    for(String s:res){
                        if(s.contains("-Xmx")){
                            dataMap.put(paramArray[i],getOptsGval(s.substring(4)));
                        }
                    }
                }
                else{
                    dataMap.put(paramArray[i],getmbGval(conf.get(confArray[i])));

                }
            //get configurations from hadoop-env.sh, hive-env.sh
            hadoopEnvReader=new BufferedReader(new FileReader(hadoopEnvFile));
            hiveEnvReader=new BufferedReader(new FileReader(hiveEnvFile));
            String hadoopline,hiveline;
            while((hadoopline=hadoopEnvReader.readLine())!=null){
                if(hadoopline.charAt(0)!='#'&& hadoopline.contains("export YARN_OPTS=")){
                    hadoopEnv=hadoopline;
                    break;
                }

            }
            while((hiveline=hiveEnvReader.readLine())!=null){
                if(hiveline.charAt(0)!='#'&& hiveline.contains("export HADOOP_CLIENT_OPTS=")){
                    hiveEnv=hiveline;
                    break;
                }

            }
            dataMap.put("yarnopts",getOptsGval(getXmxVal(hadoopEnv,"-Xmx")));
            dataMap.put("hiveopts",getOptsGval(getXmxVal(hiveEnv,"-Xmx")));
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }
        finally {
            try{
                yarnIn.close();
                mapredIn.close();
                hadoopEnvReader.close();
                hiveEnvReader.close();

            } catch (IOException e){
                e.printStackTrace();
            }
        }


    }
    private String getXmxVal(String line,String parttern){
        String[] res=line.split("=");
        String vals=res[1].substring(1,res[1].length()-1);
        String[] param=vals.split(" ");
        for(String s:param){
            if(s.contains(parttern)){
                return s.substring(4);
            }
        }
        return null;
    }
    private  String getOptsGval(String value){
        DecimalFormat df = new DecimalFormat("##.##");
        Double d=Double.parseDouble(value)/1024.0/1024.0/1024.0;
        return df.format(d)+"G";
    }
    private  String getmbGval(String value){
        DecimalFormat df = new DecimalFormat("##.##");
        Double d=Double.parseDouble(value)/1024.0;
        return df.format(d)+"G";
    }
}

@@ -1871,16 +1871,18 @@
 gth;i++)
+ %7B
 %0A       
@@ -1896,24 +1896,35 @@
   if
+ 
 (i
+ 
 ==
-2%7C%7Ci==4%7C%7Ci
+ 2 %7C%7C i == 4 %7C%7C i 
 ==
+ 
 10)%0A
@@ -1969,16 +1969,17 @@
 rray%5Bi%5D,
+ 
 conf.get
@@ -2022,20 +2022,28 @@
 e if
+ 
 (i
+ 
 ==
-8%7C%7Ci
+ 8 %7C%7C i 
 ==
+ 
 9)
+ 
 %7B%0A  
@@ -2072,17 +2072,19 @@
 ng%5B%5D res
-=
+ = 
 conf.get
@@ -2125,32 +2125,33 @@
              for
+ 
 (String s:res)%7B%0A
@@ -2147,13 +2147,16 @@
 ng s
-:
+ : 
 res)
+ 
 %7B%0A  
@@ -2171,32 +2171,33 @@
               if
+ 
 (s.contains(%22-Xm
@@ -2200,16 +2200,17 @@
 %22-Xmx%22))
+ 
 %7B%0A      
@@ -2257,16 +2257,17 @@
 rray%5Bi%5D,
+ 
 getOptsG
@@ -2353,37 +2353,22 @@
        %7D
-%0A               
  else
+ 
 %7B%0A      
@@ -2407,16 +2407,17 @@
 rray%5Bi%5D,
+ 
 getmbGva
@@ -2455,32 +2455,46 @@
               %7D%0A
+            %7D%0A
             //ge
@@ -2837,36 +2837,8 @@
 ne.c
-harAt(0)!='#'&& hadoopline.c
 onta
@@ -2853,32 +2853,60 @@
 ort YARN_OPTS=%22)
+&& hadoopline.charAt(0)!='#'
 )%7B%0A             
@@ -3079,34 +3079,8 @@
  if(
-hiveline.charAt(0)!='#'&& 
 hive
@@ -3122,16 +3122,42 @@
 _OPTS=%22)
+&& hiveline.charAt(0)!='#'
 )%7B%0A     

fix typo