Skip to content

Commit

Permalink
HIVE-6037 - Synchronize HiveConf with hive-default.xml.template and s…
Browse files Browse the repository at this point in the history
…upport show conf (Navis via Brock)

git-svn-id: https://svn.apache.org/repos/asf/hive/trunk@1610279 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
Brock Noland committed Jul 13, 2014
1 parent 5952ef5 commit 3ebc861
Show file tree
Hide file tree
Showing 27 changed files with 5,121 additions and 3,821 deletions.
28 changes: 28 additions & 0 deletions common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@
<artifactId>commons-compress</artifactId>
<version>${commons-compress.version}</version>
</dependency>
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant</artifactId>
<version>${ant.version}</version>
</dependency>
<!-- test inter-project -->
<dependency>
<groupId>junit</groupId>
Expand Down Expand Up @@ -106,6 +111,14 @@
</profiles>

<build>
<resources>
<resource>
<directory>../conf/</directory>
<includes>
<include>hive-default.xml.template</include>
</includes>
</resource>
</resources>
<sourceDirectory>${basedir}/src/java</sourceDirectory>
<testSourceDirectory>${basedir}/src/test</testSourceDirectory>
<scriptSourceDirectory>${basedir}/src/scripts</scriptSourceDirectory>
Expand Down Expand Up @@ -136,6 +149,21 @@
<goal>run</goal>
</goals>
</execution>
<execution>
<id>generate-template</id>
<phase>package</phase>
<configuration>
<target>
<property name="compile.classpath" refid="maven.runtime.classpath"/>
<taskdef name="templategen" classname="org.apache.hadoop.hive.ant.GenHiveTemplate"
classpath="${compile.classpath}"/>
<templategen templateFile="${basedir}/../conf/hive-default.xml.template"/>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
Expand Down
169 changes: 169 additions & 0 deletions common/src/java/org/apache/hadoop/hive/ant/GenHiveTemplate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.hadoop.hive.ant;

import org.apache.hadoop.hive.conf.HiveConf;
import org.apache.hadoop.hive.shims.ShimLoader;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import java.net.URL;

/**
* Generates hive-default.xml.template from HiveConf.ConfVars
*/
public class GenHiveTemplate extends Task {

private String templateFile;

public String getTemplateFile() {
return templateFile;
}

public void setTemplateFile(String templateFile) {
this.templateFile = templateFile;
}

private void generate() throws Exception {
File current = new File(templateFile);
if (current.exists()) {
ClassLoader loader = GenHiveTemplate.class.getClassLoader();
URL url = loader.getResource("org/apache/hadoop/hive/conf/HiveConf.class");
if (url != null) {
File file = new File(url.getFile());
if (file.exists() && file.lastModified() < current.lastModified()) {
return;
}
}
}
writeToFile(current, generateTemplate());
}

private Document generateTemplate() throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
Document doc = docBuilder.newDocument();
doc.appendChild(doc.createProcessingInstruction(
"xml-stylesheet", "type=\"text/xsl\" href=\"configuration.xsl\""));

doc.appendChild(doc.createComment("\n" +
" Licensed to the Apache Software Foundation (ASF) under one or more\n" +
" contributor license agreements. See the NOTICE file distributed with\n" +
" this work for additional information regarding copyright ownership.\n" +
" The ASF licenses this file to You under the Apache License, Version 2.0\n" +
" (the \"License\"); you may not use this file except in compliance with\n" +
" the License. You may obtain a copy of the License at\n" +
"\n" +
" http://www.apache.org/licenses/LICENSE-2.0\n" +
"\n" +
" Unless required by applicable law or agreed to in writing, software\n" +
" distributed under the License is distributed on an \"AS IS\" BASIS,\n" +
" WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" +
" See the License for the specific language governing permissions and\n" +
" limitations under the License.\n"));

Element root = doc.createElement("configuration");
doc.appendChild(root);

root.appendChild(doc.createComment(
" WARNING!!! This file is auto generated for documentation purposes ONLY! "));
root.appendChild(doc.createComment(
" WARNING!!! Any changes you make to this file will be ignored by Hive. "));
root.appendChild(doc.createComment(
" WARNING!!! You must make your changes in hive-site.xml instead. "));

root.appendChild(doc.createComment(" Hive Execution Parameters "));

Thread.currentThread().setContextClassLoader(ShimLoader.class.getClassLoader());
for (HiveConf.ConfVars confVars : HiveConf.ConfVars.values()) {
if (confVars.isExcluded()) {
// thought of creating template for each shims, but I couldn't generate proper mvn script
continue;
}
Element property = appendElement(root, "property", null);
appendElement(property, "key", confVars.varname);
appendElement(property, "value", confVars.getDefaultExpr());
appendElement(property, "description", normalize(confVars.getDescription()));
// wish to add new line here.
}
return doc;
}

private String normalize(String description) {
int index = description.indexOf('\n');
if (index < 0) {
return description;
}
int prev = 0;
StringBuilder builder = new StringBuilder(description.length() << 1);
for (;index > 0; index = description.indexOf('\n', prev = index + 1)) {
builder.append("\n ").append(description.substring(prev, index));
}
if (prev < description.length()) {
builder.append("\n ").append(description.substring(prev));
}
builder.append("\n ");
return builder.toString();
}

private void writeToFile(File template, Document document) throws Exception {
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(template);
transformer.transform(source, result);
}

private Element appendElement(Element parent, String name, String text) {
Document document = parent.getOwnerDocument();
Element child = document.createElement(name);
parent.appendChild(child);
if (text != null) {
Text textNode = document.createTextNode(text);
child.appendChild(textNode);
}
return child;
}

@Override
public void execute() throws BuildException {
try {
generate();
} catch (Exception e) {
throw new BuildException(e);
}
}

public static void main(String[] args) throws Exception {
GenHiveTemplate gen = new GenHiveTemplate();
gen.generate();
}
}
Loading

0 comments on commit 3ebc861

Please sign in to comment.