├── project ├── build.properties └── plugins.sbt ├── .gitignore ├── hbase_input.py ├── src ├── main │ └── scala │ │ └── examples │ │ ├── pythonConverters.scala │ │ └── HBaseInput.scala └── test │ └── scala │ └── examples │ └── pythonConverterTest.scala ├── README.md ├── sbt ├── sbt-launch-lib.bash └── sbt └── LICENSE /project/build.properties: -------------------------------------------------------------------------------- 1 | // This file should only contain the version of sbt to use. 2 | sbt.version=0.13.7 3 | -------------------------------------------------------------------------------- /project/plugins.sbt: -------------------------------------------------------------------------------- 1 | // Use sbt-spark-package plugin to assembly package 2 | // You may use this file to add plugin dependencies for sbt. 3 | resolvers += "Spark Packages repo" at "https://dl.bintray.com/spark-packages/maven/" 4 | 5 | addSbtPlugin("org.spark-packages" %% "sbt-spark-package" % "0.2.1") 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | *.log 3 | *.swp 4 | 5 | # sbt specific 6 | .cache/ 7 | .history/ 8 | .lib/ 9 | dist/* 10 | target/ 11 | lib_managed/ 12 | src_managed/ 13 | project/boot/ 14 | project/plugins/project/ 15 | 16 | # Scala-IDE specific 17 | .scala_dependencies 18 | .worksheet 19 | 20 | # IntelliJ 21 | *.iml 22 | .idea/ 23 | -------------------------------------------------------------------------------- /hbase_input.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import json 3 | 4 | from pyspark import SparkContext 5 | 6 | """ 7 | Create test data in HBase first: 8 | hbase(main):016:0> create 'test', 'c1' 9 | hbase(main):017:0> put 'test', 'r1', 'c1:a', 'a1' 10 | hbase(main):018:0> put 'test', 'r1', 'c1:b', 'b1' 11 | hbase(main):019:0> put 'test', 'r2', 'c1:a', 'a2' 12 | hbase(main):020:0> put 'test', 'r3', 'c1', '3' 13 | hbase(main):028:0> scan "test" 14 | ROW COLUMN+CELL 15 | r1 column=c1:a, timestamp=1420329575846, value=a1 16 | r1 column=c1:b, timestamp=1420329640962, value=b1 17 | r2 column=c1:a, timestamp=1420329683843, value=a2 18 | r3 column=c1:, timestamp=1420329810504, value=3 19 | """ 20 | if __name__ == "__main__": 21 | if len(sys.argv) != 4: 22 | print >> sys.stderr, """ 23 | Usage: hbase_inputformat 24 | Run with example jar: 25 | ./bin/spark-submit --driver-class-path \ 26 | /path/to/examples/hbase_inputformat.py
27 | Assumes you have some data in HBase already, running on , in
at 28 | More information is available at https://github.com/GenTang/spark_hbase 29 | """ 30 | exit(-1) 31 | host = sys.argv[1] 32 | table = sys.argv[2] 33 | column = sys.argv[3] 34 | sc = SparkContext(appName="HBaseInputFormat") 35 | 36 | # Other options for configuring scan behavior are available. More information available at 37 | # https://github.com/apache/hbase/blob/master/hbase-server/src/main/java/org/apache/hadoop/hbase/mapreduce/TableInputFormat.java 38 | conf = { 39 | "hbase.zookeeper.quorum": host, 40 | "hbase.mapreduce.inputtable": table, 41 | "hbase.mapreduce.scan.columns": column} 42 | keyConv = "examples.pythonconverters.ImmutableBytesWritableToStringConverter" 43 | valueConv = "examples.pythonconverters.HBaseResultToStringConverter" 44 | 45 | hbase_rdd = sc.newAPIHadoopRDD( 46 | "org.apache.hadoop.hbase.mapreduce.TableInputFormat", 47 | "org.apache.hadoop.hbase.io.ImmutableBytesWritable", 48 | "org.apache.hadoop.hbase.client.Result", 49 | keyConverter=keyConv, 50 | valueConverter=valueConv, 51 | conf=conf) 52 | 53 | 54 | hbase_rdd = hbase_rdd.flatMapValues(lambda v: v.split("\n")).mapValues(json.loads) 55 | # hbase_rdd is a RDD[dict] 56 | 57 | output = hbase_rdd.collect() 58 | for (k, v) in output: 59 | print (k, v) 60 | 61 | sc.stop() 62 | -------------------------------------------------------------------------------- /src/main/scala/examples/pythonConverters.scala: -------------------------------------------------------------------------------- 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 examples.pythonconverters 19 | 20 | import scala.collection.JavaConversions._ 21 | import scala.util.parsing.json.JSONObject 22 | 23 | import org.apache.spark.api.python.Converter 24 | import org.apache.hadoop.hbase.client.{Put, Result} 25 | import org.apache.hadoop.hbase.io.ImmutableBytesWritable 26 | import org.apache.hadoop.hbase.util.Bytes 27 | import org.apache.hadoop.hbase.KeyValue.Type 28 | import org.apache.hadoop.hbase.CellUtil 29 | 30 | 31 | /** 32 | * Implementation of [[org.apache.spark.api.python.Converter]] that converts all 33 | * the records in an HBase Result to a String. In the String, it contains row, column, 34 | * qualifier, timesstamp, type and value 35 | */ 36 | 37 | class HBaseResultToStringConverter extends Converter[Any, String]{ 38 | override def convert(obj: Any): String = { 39 | import collection.JavaConverters._ 40 | val result = obj.asInstanceOf[Result] 41 | val output = result.listCells.asScala.map(cell => 42 | Map( 43 | "row" -> Bytes.toStringBinary(CellUtil.cloneFamily(cell)), 44 | "columnFamily" -> Bytes.toStringBinary(CellUtil.cloneFamily(cell)), 45 | "qualifier" -> Bytes.toStringBinary(CellUtil.cloneQualifier(cell)), 46 | "timestamp" -> cell.getTimestamp.toString, 47 | "type" -> Type.codeToType(cell.getTypeByte).toString, 48 | "value" -> Bytes.toStringBinary(CellUtil.cloneValue(cell)) 49 | ) 50 | ) 51 | // output is an instance of Map which will be translated to json String 52 | // Hbase will escape "\n", so it is safe to use "\n" to join json. 53 | output.map(JSONObject(_).toString()).mkString("\n") 54 | } 55 | } 56 | 57 | /** 58 | * Implementation of [[org.apache.spark.api.python.Converter]] that converts an 59 | * ImmutableBytesWritable to a String 60 | */ 61 | 62 | class ImmutableBytesWritableToStringConverter extends Converter[Any, String] { 63 | override def convert(obj: Any): String = { 64 | val key = obj.asInstanceOf[ImmutableBytesWritable] 65 | Bytes.toStringBinary(key.get()) 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/test/scala/examples/pythonConverterTest.scala: -------------------------------------------------------------------------------- 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 examples.pythonconverters 19 | 20 | import org.apache.hadoop.hbase.client.HBaseAdmin 21 | import org.apache.hadoop.hbase.{HBaseConfiguration, HTableDescriptor} 22 | import org.apache.hadoop.hbase.mapreduce.TableInputFormat 23 | import org.apache.hadoop.hbase.HConstants 24 | 25 | import org.apache.spark._ 26 | import org.apache.spark.rdd.RDD 27 | import org.apache.spark.api.python.Converter 28 | 29 | object HBaseTest { 30 | def main(args: Array[String]) { 31 | val sparkConf = new SparkConf().setAppName("HBaseTest").setMaster("localhost") 32 | val sc = new SparkContext(sparkConf) 33 | val conf = HBaseConfiguration.create() 34 | // Other options for hbase configuration are available, please check 35 | // http://hbase.apache.org/apidocs/org/apache/hadoop/hbase/HConstants.html 36 | conf.set(HConstants.ZOOKEEPER_QUORUM, args(0)) 37 | // Other options for configuring scan behavior are available. More information available at 38 | // http://hbase.apache.org/apidocs/org/apache/hadoop/hbase/mapreduce/TableInputFormat.html 39 | conf.set(TableInputFormat.INPUT_TABLE, args(1)) 40 | 41 | // Initialize hBase table if necessary 42 | val admin = new HBaseAdmin(conf) 43 | if (!admin.isTableAvailable(args(1))) { 44 | val tableDesc = new HTableDescriptor(args(1)) 45 | admin.createTable(tableDesc) 46 | } 47 | 48 | val hBaseRDD = sc.newAPIHadoopRDD(conf, classOf[TableInputFormat], 49 | classOf[org.apache.hadoop.hbase.io.ImmutableBytesWritable], 50 | classOf[org.apache.hadoop.hbase.client.Result]) 51 | val keyConverter = new ImmutableBytesWritableToStringConverter().asInstanceOf[Converter[Any,Any]] 52 | val valueConverter = new HBaseResultToStringConverter().asInstanceOf[Converter[Any, Any]] 53 | 54 | val hrdd = convertRDD(hBaseRDD, keyConverter, valueConverter) 55 | hrdd.map(_._2).foreach(println) 56 | 57 | sc.stop() 58 | } 59 | 60 | 61 | // This function is copied from org.apache.spark.api.python.PythonHadoopUtils.convertRDD. 62 | def convertRDD[K, V](rdd: RDD[(K, V)], 63 | keyConverter: Converter[Any, Any], 64 | valueConverter: Converter[Any, Any]): RDD[(Any, Any)] = { 65 | rdd.map { case (k, v) => (keyConverter.convert(k), valueConverter.convert(v)) } 66 | } 67 | 68 | } 69 | -------------------------------------------------------------------------------- /src/main/scala/examples/HBaseInput.scala: -------------------------------------------------------------------------------- 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 examples 19 | 20 | import org.apache.hadoop.hbase.client.HBaseAdmin 21 | import org.apache.hadoop.hbase.{HBaseConfiguration, HTableDescriptor, TableName} 22 | import org.apache.hadoop.hbase.mapreduce.TableInputFormat 23 | import org.apache.hadoop.hbase.KeyValue.Type 24 | import org.apache.hadoop.hbase.HConstants 25 | import org.apache.hadoop.hbase.util.Bytes 26 | import org.apache.hadoop.hbase.CellUtil 27 | 28 | 29 | import org.apache.spark._ 30 | 31 | import scala.collection.JavaConverters._ 32 | 33 | 34 | object HBaseInput { 35 | def main(args: Array[String]) { 36 | val sparkConf = new SparkConf().setAppName("HBaseTest") 37 | val sc = new SparkContext(sparkConf) 38 | val conf = HBaseConfiguration.create() 39 | // Other options for hbase configuration are available, please check 40 | // http://hbase.apache.org/apidocs/org/apache/hadoop/hbase/HConstants.html 41 | conf.set(HConstants.ZOOKEEPER_QUORUM, args(0)) 42 | // Other options for configuring scan behavior are available. More information available at 43 | // http://hbase.apache.org/apidocs/org/apache/hadoop/hbase/mapreduce/TableInputFormat.html 44 | conf.set(TableInputFormat.INPUT_TABLE, args(1)) 45 | 46 | // Initialize hBase table if necessary 47 | val admin = new HBaseAdmin(conf) 48 | if (!admin.isTableAvailable(args(1))) { 49 | val tableDesc = new HTableDescriptor(TableName.valueOf(args(1))) 50 | admin.createTable(tableDesc) 51 | } 52 | 53 | val hBaseRDD = sc.newAPIHadoopRDD(conf, classOf[TableInputFormat], 54 | classOf[org.apache.hadoop.hbase.io.ImmutableBytesWritable], 55 | classOf[org.apache.hadoop.hbase.client.Result]) 56 | 57 | //keyValue is a RDD[java.util.list[hbase.KeyValue]] 58 | val keyValue = hBaseRDD.map(x => x._2).map(_.list) 59 | 60 | //outPut is a RDD[String], in which each line represents a record in HBase 61 | val outPut = keyValue.flatMap(x => x.asScala.map(cell => 62 | "columnFamily=%s,qualifier=%s,timestamp=%s,type=%s,value=%s".format( 63 | Bytes.toStringBinary(CellUtil.cloneFamily(cell)), 64 | Bytes.toStringBinary(CellUtil.cloneQualifier(cell)), 65 | cell.getTimestamp.toString, 66 | Type.codeToType(cell.getTypeByte), 67 | Bytes.toStringBinary(CellUtil.cloneValue(cell)) 68 | ) 69 | ) 70 | ) 71 | 72 | outPut.foreach(println) 73 | 74 | sc.stop() 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | spark_hbase 2 | =========== 3 | 4 | Spark has their own example about integrating HBase and Spark in scala [HBaseTest.scala](https://github.com/apache/spark/blob/master/examples/src/main/scala/org/apache/spark/examples/HBaseTest.scala) and python converter [HBaseConverters.scala](https://github.com/apache/spark/blob/master/examples/src/main/scala/org/apache/spark/examples/pythonconverters/HBaseConverters.scala). 5 | 6 | However, the python converter `HBaseResultToStringConverter` in [HBaseConverters.scala](https://github.com/apache/spark/blob/master/examples/src/main/scala/org/apache/spark/examples/pythonconverters/HBaseConverters.scala) return only the value of first column in the result. And [HBaseTest.scala](https://github.com/apache/spark/blob/master/examples/src/main/scala/org/apache/spark/examples/HBaseTest.scala) stops just at returning *org.apache.hadoop.hbase.client.Result* and doing .count() call. 7 | 8 | Here we provide a new example in Scala about transferring data saved in hbase into `String` by Spark and a new example of python converter. 9 | 10 | The example in scala [HBaseInput.scala](/src/main/scala/examples/HBaseInput.scala) transfers the data saved in hbase into `RDD[String]` which contains *columnFamily, qualifier, timestamp, type, value*. 11 | 12 | The example of converter for python [pythonConverters.scala](/src/main/scala/examples//pythonConverters.scala) transfer the data saved in hbase into string which contains the same information as the example above. We can use `ast` package to easily transfer this string to dictionary 13 | 14 | How to run 15 | ========= 16 | 1. Make sure that you well set up [git](https://help.github.com/articles/set-up-git/#platform-linux) 17 | 2. Download this application by 18 | 19 | ```bash 20 | $ git clone https://github.com/GenTang/spark_hbase.git 21 | ``` 22 | 23 | 3. Build the assembly by using SBT `assembly` 24 | 25 | ```bash 26 | $ /sbt/sbt clean assembly 27 | ``` 28 | 29 | * Run example python script [hbase_input.py](hbase_input.py) which use pythonConverter `ImmutableBytesWritableToStringConverter` and `HBaseResultToStringConverter` to convert the data in hbase to dictionary 30 | 31 | * If you are using `SPARK_CLASSPATH`: 32 | 1. Add `export SPARK_CLASSPATH=$SPARK_CLASSPATH":/lib/*:/target/scala-2.10/spark_hbase-assembly-1.0.jar` to `./conf/spark-env.sh`. 33 | 34 | 2. Launch the script by 35 | ```bash 36 | $ ./bin/spark-submit \ 37 |
38 | ``` 39 | 40 | * You can also use `spark.executor.extraClassPath` and `--driver-class-path` (recommended): 41 | 1. Add `spark.executor.extraClassPath /lib/*` to `spark-defaults.conf`. 42 | 43 | 2. Launch the script by 44 | ```bash 45 | $ ./bin/spark-submit \ 46 | --driver-class-path /target/scala-2.10/spark_hbase-assembly-1.0.jar \ 47 | \ 48 |
49 | ``` 50 | 51 | * Run example scala script [HBaseInput.scala](/src/main/scala/examples/HBaseInput.scala) 52 | * If you are using `SPARK_CLASSPATH`: 53 | 1. Add `export SPARK_CLASSPATH=$SPARK_CLASSPATH":/lib/*` to `./conf/spark-env.sh`. 54 | 55 | 2. Launch the script by 56 | ```bash 57 | $ ./bin/spark-submit \ 58 | --class examples.HBaseInput \ 59 | /target/scala-2.10/spark_hbase-assembly-1.0.jar \ 60 |
61 | ``` 62 | 63 | * You can also use `spark.executor.extraClassPath` and `--driver-class-path` (recommended): 64 | 1. The same configuration as above 65 | 66 | 2. Launch the script by 67 | ```bash 68 | $ ./bin/spark-submit \ 69 | --driver-class-path /lib/*: \ 70 | --class examples.HBaseInput \ 71 | /target/scala-2.10/spark_hbase-assembly-1.0.jar \ 72 |
73 | ``` 74 | 75 | Example of results 76 | ================== 77 | Assume that you have already some data in hbase as follow: 78 | 79 | hbase(main):028:0> scan "test" 80 | ROW COLUMN+CELL 81 | r1 column=c1:a, timestamp=1420329575846, value=a1 82 | r1 column=c1:b, timestamp=1420329640962, value=b1 83 | r2 column=c1:a, timestamp=1420329683843, value=a2 84 | r3 column=c1:, timestamp=1420329810504, value=3 85 | 86 | By launching `$ ./bin/spark-submit --driver-class-path /target/scala-2.10/spark_hbase-assembly-1.0.jar localhost test c1`, you will get 87 | 88 | (u'r1', {'columnFamliy': 'c1', 'timestamp': '1420329575846', 'type': 'Put', 'qualifier': 'a', 'value': 'a1'}) 89 | (u'r1', {'columnFamliy': 'c1', 'timestamp': '1420329640962', 'type': 'Put', 'qualifier': 'b', 'value': 'b1'}) 90 | (u'r2', {'columnFamliy': 'c1', 'timestamp': '1420329683843', 'type': 'Put', 'qualifier': 'a', 'value': 'a2'}) 91 | (u'r3', {'columnFamliy': 'c1', 'timestamp': '1420329810504', 'type': 'Put', 'qualifier': '', 'value': '3'}) 92 | -------------------------------------------------------------------------------- /sbt/sbt-launch-lib.bash: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # 3 | 4 | # A library to simplify using the SBT launcher from other packages. 5 | # Note: This should be used by tools like giter8/conscript etc. 6 | 7 | # TODO - Should we merge the main SBT script with this library? 8 | 9 | if test -z "$HOME"; then 10 | declare -r script_dir="$(dirname "$script_path")" 11 | else 12 | declare -r script_dir="$HOME/.sbt" 13 | fi 14 | 15 | declare -a residual_args 16 | declare -a java_args 17 | declare -a scalac_args 18 | declare -a sbt_commands 19 | declare -a maven_profiles 20 | 21 | if test -x "$JAVA_HOME/bin/java"; then 22 | echo -e "Using $JAVA_HOME as default JAVA_HOME." 23 | echo "Note, this will be overridden by -java-home if it is set." 24 | declare java_cmd="$JAVA_HOME/bin/java" 25 | else 26 | declare java_cmd=java 27 | fi 28 | 29 | echoerr () { 30 | echo 1>&2 "$@" 31 | } 32 | vlog () { 33 | [[ $verbose || $debug ]] && echoerr "$@" 34 | } 35 | dlog () { 36 | [[ $debug ]] && echoerr "$@" 37 | } 38 | 39 | acquire_sbt_jar () { 40 | SBT_VERSION=`awk -F "=" '/sbt\.version/ {print $2}' ./project/build.properties` 41 | URL1=https://dl.bintray.com/typesafe/ivy-releases/org.scala-sbt/sbt-launch/${SBT_VERSION}/sbt-launch.jar 42 | JAR=sbt/sbt-launch-${SBT_VERSION}.jar 43 | 44 | sbt_jar=$JAR 45 | 46 | if [[ ! -f "$sbt_jar" ]]; then 47 | # Download sbt launch jar if it hasn't been downloaded yet 48 | if [ ! -f "${JAR}" ]; then 49 | # Download 50 | printf "Attempting to fetch sbt\n" 51 | JAR_DL="${JAR}.part" 52 | if [ $(command -v curl) ]; then 53 | curl --fail --location --silent ${URL1} > "${JAR_DL}" &&\ 54 | mv "${JAR_DL}" "${JAR}" 55 | elif [ $(command -v wget) ]; then 56 | wget --quiet ${URL1} -O "${JAR_DL}" &&\ 57 | mv "${JAR_DL}" "${JAR}" 58 | else 59 | printf "You do not have curl or wget installed, please install sbt manually from http://www.scala-sbt.org/\n" 60 | exit -1 61 | fi 62 | fi 63 | if [ ! -f "${JAR}" ]; then 64 | # We failed to download 65 | printf "Our attempt to download sbt locally to ${JAR} failed. Please install sbt manually from http://www.scala-sbt.org/\n" 66 | exit -1 67 | fi 68 | printf "Launching sbt from ${JAR}\n" 69 | fi 70 | } 71 | 72 | execRunner () { 73 | # print the arguments one to a line, quoting any containing spaces 74 | [[ $verbose || $debug ]] && echo "# Executing command line:" && { 75 | for arg; do 76 | if printf "%s\n" "$arg" | grep -q ' '; then 77 | printf "\"%s\"\n" "$arg" 78 | else 79 | printf "%s\n" "$arg" 80 | fi 81 | done 82 | echo "" 83 | } 84 | 85 | "$@" 86 | } 87 | 88 | addJava () { 89 | dlog "[addJava] arg = '$1'" 90 | java_args=( "${java_args[@]}" "$1" ) 91 | } 92 | 93 | enableProfile () { 94 | dlog "[enableProfile] arg = '$1'" 95 | maven_profiles=( "${maven_profiles[@]}" "$1" ) 96 | export SBT_MAVEN_PROFILES="${maven_profiles[@]}" 97 | } 98 | 99 | addSbt () { 100 | dlog "[addSbt] arg = '$1'" 101 | sbt_commands=( "${sbt_commands[@]}" "$1" ) 102 | } 103 | addResidual () { 104 | dlog "[residual] arg = '$1'" 105 | residual_args=( "${residual_args[@]}" "$1" ) 106 | } 107 | addDebugger () { 108 | addJava "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=$1" 109 | } 110 | 111 | # a ham-fisted attempt to move some memory settings in concert 112 | # so they need not be dicked around with individually. 113 | get_mem_opts () { 114 | local mem=${1:-2048} 115 | local perm=$(( $mem / 4 )) 116 | (( $perm > 256 )) || perm=256 117 | (( $perm < 4096 )) || perm=4096 118 | local codecache=$(( $perm / 2 )) 119 | 120 | echo "-Xms${mem}m -Xmx${mem}m -XX:MaxPermSize=${perm}m -XX:ReservedCodeCacheSize=${codecache}m" 121 | } 122 | 123 | require_arg () { 124 | local type="$1" 125 | local opt="$2" 126 | local arg="$3" 127 | if [[ -z "$arg" ]] || [[ "${arg:0:1}" == "-" ]]; then 128 | echo "$opt requires <$type> argument" 1>&2 129 | exit 1 130 | fi 131 | } 132 | 133 | is_function_defined() { 134 | declare -f "$1" > /dev/null 135 | } 136 | 137 | process_args () { 138 | while [[ $# -gt 0 ]]; do 139 | case "$1" in 140 | -h|-help) usage; exit 1 ;; 141 | -v|-verbose) verbose=1 && shift ;; 142 | -d|-debug) debug=1 && shift ;; 143 | 144 | -ivy) require_arg path "$1" "$2" && addJava "-Dsbt.ivy.home=$2" && shift 2 ;; 145 | -mem) require_arg integer "$1" "$2" && sbt_mem="$2" && shift 2 ;; 146 | -jvm-debug) require_arg port "$1" "$2" && addDebugger $2 && shift 2 ;; 147 | -batch) exec path to global settings/plugins directory (default: ~/.sbt) 67 | -sbt-boot path to shared boot directory (default: ~/.sbt/boot in 0.11 series) 68 | -ivy path to local Ivy repository (default: ~/.ivy2) 69 | -mem set memory options (default: $sbt_mem, which is $(get_mem_opts $sbt_mem)) 70 | -no-share use all local caches; no sharing 71 | -no-global uses global caches, but does not use global ~/.sbt directory. 72 | -jvm-debug Turn on JVM debugging, open at the given port. 73 | -batch Disable interactive mode 74 | 75 | # sbt version (default: from project/build.properties if present, else latest release) 76 | -sbt-version use the specified version of sbt 77 | -sbt-jar use the specified jar as the sbt launcher 78 | -sbt-rc use an RC version of sbt 79 | -sbt-snapshot use a snapshot version of sbt 80 | 81 | # java version (default: java from PATH, currently $(java -version 2>&1 | grep version)) 82 | -java-home alternate JAVA_HOME 83 | 84 | # jvm options and output control 85 | JAVA_OPTS environment variable, if unset uses "$java_opts" 86 | SBT_OPTS environment variable, if unset uses "$default_sbt_opts" 87 | .sbtopts if this file exists in the current directory, it is 88 | prepended to the runner args 89 | /etc/sbt/sbtopts if this file exists, it is prepended to the runner args 90 | -Dkey=val pass -Dkey=val directly to the java runtime 91 | -J-X pass option -X directly to the java runtime 92 | (-J is stripped) 93 | -S-X add -X to sbt's scalacOptions (-S is stripped) 94 | -PmavenProfiles Enable a maven profile for the build. 95 | 96 | In the case of duplicated or conflicting options, the order above 97 | shows precedence: JAVA_OPTS lowest, command line options highest. 98 | EOM 99 | } 100 | 101 | process_my_args () { 102 | while [[ $# -gt 0 ]]; do 103 | case "$1" in 104 | -no-colors) addJava "-Dsbt.log.noformat=true" && shift ;; 105 | -no-share) addJava "$noshare_opts" && shift ;; 106 | -no-global) addJava "-Dsbt.global.base=$(pwd)/project/.sbtboot" && shift ;; 107 | -sbt-boot) require_arg path "$1" "$2" && addJava "-Dsbt.boot.directory=$2" && shift 2 ;; 108 | -sbt-dir) require_arg path "$1" "$2" && addJava "-Dsbt.global.base=$2" && shift 2 ;; 109 | -debug-inc) addJava "-Dxsbt.inc.debug=true" && shift ;; 110 | -batch) exec /dev/null) 147 | if [[ ! $? ]]; then 148 | saved_stty="" 149 | fi 150 | } 151 | 152 | saveSttySettings 153 | trap onExit INT 154 | 155 | run "$@" 156 | 157 | exit_status=$? 158 | onExit 159 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | --------------------------------------------------------------------------------