|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.spark.editor |
| 19 | + |
| 20 | +import org.apache.commons.lang3.StringUtils |
| 21 | + |
| 22 | +import java.io.{File, FileOutputStream} |
| 23 | +import scala.xml._ |
| 24 | + |
| 25 | + |
| 26 | +object HiveSiteEditor { |
| 27 | + private val hiveSitePath: String = { |
| 28 | + val sparkVersionSource = scala.io.Source.fromFile("/opt/mapr/spark/sparkversion") |
| 29 | + val sparkVersion = sparkVersionSource.getLines().mkString |
| 30 | + sparkVersionSource.close() |
| 31 | + s"/opt/mapr/spark/spark-${sparkVersion}/conf/hive-site.xml" |
| 32 | + } |
| 33 | + |
| 34 | + private val xml: Elem = XML.loadFile(hiveSitePath) |
| 35 | + private val property: NodeSeq = xml \ "property" |
| 36 | + private val properties = property.map(toProperty) |
| 37 | + private val xmlHeader = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><?xml-stylesheet type=\"text/xsl\" href=\"configuration.xsl\"?><configuration>\n" |
| 38 | + |
| 39 | + case class Property(name: String, value: String, description: String) |
| 40 | + |
| 41 | + def main(args: Array[String]): Unit = { |
| 42 | + if (args(0).equals("delete")) save(delete(args)) |
| 43 | + else if (args(0).equals("create")) save(create(args)) |
| 44 | + else if (args(0).equals("replace")) save(replace(args)) |
| 45 | + else throw new IllegalArgumentException("Need to set operation name as firs argument: create | replace | delete") |
| 46 | + } |
| 47 | + |
| 48 | + // create name=value name1=value1 ... |
| 49 | + def create(args: Array[String]): Seq[Property] = { |
| 50 | + val inputProps = parsePropertiesFromArgs(args, "create") |
| 51 | + val replacedProps = properties.map(p => { |
| 52 | + val i = inputProps.indexWhere(_.name.equals(p.name)) |
| 53 | + if (i >= 0) Property(name = inputProps(i).name, value = inputProps(i).value, description = p.description) else p |
| 54 | + }) |
| 55 | + val create = inputProps.map(np => { |
| 56 | + val i = replacedProps.indexWhere(_.name.equals(np.name)) |
| 57 | + if (i == -1) Property(name = np.name, value = np.value, description = "") else Property("", "", "") |
| 58 | + }).filterNot(_.name.equals("")) |
| 59 | + replacedProps ++ create |
| 60 | + } |
| 61 | + |
| 62 | + // replace name=value name1=value1 ... |
| 63 | + def replace(args: Array[String]): Seq[Property] = { |
| 64 | + val inputProps = parsePropertiesFromArgs(args, "replace") |
| 65 | + properties.map(p => { |
| 66 | + val i = inputProps.indexWhere(_.name.equals(p.name)) |
| 67 | + if (i >= 0) Property(name = inputProps(i).name, value = inputProps(i).value, description = p.description) else p |
| 68 | + }) |
| 69 | + } |
| 70 | + |
| 71 | + // delete name name1 ... |
| 72 | + def delete(args: Array[String]): Seq[Property] = { |
| 73 | + property.filterNot(p => StringUtils.equalsAnyIgnoreCase((p \ "name").text, args: _*)) |
| 74 | + .map(toProperty) |
| 75 | + } |
| 76 | + |
| 77 | + def save(properties: Seq[Property]): Unit = { |
| 78 | + val fos = new FileOutputStream(new File(hiveSitePath)) |
| 79 | + Console.withOut(fos) { |
| 80 | + println(xmlHeader) |
| 81 | + properties.map(toXml).foreach(println) |
| 82 | + println("</configuration>") |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + def parsePropertiesFromArgs(args: Array[String], methodName: String): Seq[Property] = { |
| 87 | + val argsMap = args.filterNot(_.equals(methodName)).map(_.split("=")) |
| 88 | + .map(arr => arr(0) -> arr(1)).toMap |
| 89 | + argsMap.map(x => Property(x._1, x._2, "")).toSeq |
| 90 | + } |
| 91 | + |
| 92 | + def toProperty(node: Node): Property = { |
| 93 | + val name = (node \ "name").text |
| 94 | + val value = (node \ "value").text |
| 95 | + val description = (node \ "description").text |
| 96 | + Property(name, value, description) |
| 97 | + } |
| 98 | + |
| 99 | + def toXml(p: Property): Node = { |
| 100 | + <property> |
| 101 | + <name>{p.name}</name> |
| 102 | + <value>{p.value}</value> |
| 103 | + <description>{p.description}</description> |
| 104 | + </property> |
| 105 | + } |
| 106 | + |
| 107 | +} |
0 commit comments