Skip to content

Instantly share code, notes, and snippets.

@mehikmat
Created April 5, 2016 07:42
Show Gist options
  • Save mehikmat/000c20561b1d8c6946f2bac86d2e48f1 to your computer and use it in GitHub Desktop.
Save mehikmat/000c20561b1d8c6946f2bac86d2e48f1 to your computer and use it in GitHub Desktop.
private static void setNumMappers(int numNodes) {
String mapred_side_xml_path = "/opt/hadoop-2.3.0-cdh5.0.1/etc/hadoop/mapred-site.xml";
try {
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setIgnoringComments(true);
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse(new File(mapred_side_xml_path));
NodeList nodes = doc.getElementsByTagName("configuration");
Text n = doc.createTextNode("mapreduce.job.running.map.limit");
Element name = doc.createElement("name");
name.appendChild(n);
Text v = doc.createTextNode(String.valueOf(numNodes));
Element value = doc.createElement("value");
value.appendChild(v);
Element property = doc.createElement("property");
property.appendChild(name);
property.appendChild(value);
nodes.item(0).appendChild(property);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
Result output = new StreamResult(new File(mapred_side_xml_path));
Source input = new DOMSource(doc);
transformer.transform(input, output);
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment