Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Spark-Processor/src/main/scala/com/powerpanel/kso/OpenTSDBOutput.scala
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
68 lines (61 sloc)
2.45 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.powerpanel.kso; | |
import org.joda.time.DateTime | |
import scala.util.control.NonFatal | |
import com.powerpanel.kso.model._; | |
import org.apache.log4j.Logger | |
import org.apache.spark.streaming.dstream.DStream | |
import org.apache.http.client.methods.HttpPost | |
import org.apache.http.entity.StringEntity | |
import org.apache.http.impl.client.DefaultHttpClient | |
import org.json4s._ | |
import org.json4s.jackson.JsonMethods._ | |
import org.json4s.JsonDSL._ | |
import scala.collection.JavaConversions._ | |
class OpenTSDBOutput extends Serializable { | |
object Holder extends Serializable { | |
@transient lazy val logger = Logger.getLogger(getClass.getName) | |
} | |
def putOpentsdb[T](opentsdbIP: String, stream: DStream[DataPackage]) = { | |
stream.mapPartitions(partition => { | |
var count = 0; | |
partition.foreach((generatorData: DataPackage) => | |
{ | |
var body: String = "["; | |
for(metricData <- generatorData.getData()) { | |
for(dataPoint <- metricData.getDatapoints()) { | |
val metric = metricData.getMetric(); | |
val value = dataPoint.getValue(); | |
val timestamp = dataPoint.getTimestamp(); | |
val generator_id = generatorData.getGenerator(); | |
val organization_id = generatorData.getOrganization(); | |
val region = generatorData.getRegion(); | |
val state = generatorData.getState; | |
val pstring = f"""{ | |
"metric": "$metric", | |
"value": $value, | |
"timestamp": $timestamp, | |
"tags": {"generator": $generator_id, "organization": $organization_id, "state": "$state", "region": "$region"} | |
},""".stripMargin; | |
body = body.concat(pstring); | |
} | |
} | |
body = body.dropRight(1); | |
body = body.concat("]"); | |
// Holder.logger.info("Post body: " + body); | |
var openTSDBUrl = "http://" + opentsdbIP + "/api/put" | |
try { | |
val httpClient = new DefaultHttpClient() | |
val post = new HttpPost(openTSDBUrl) | |
post.setHeader("Content-type", "application/json") | |
post.setEntity(new StringEntity(body)) | |
httpClient.execute(post) | |
} catch { | |
case NonFatal(t) => { | |
} | |
} | |
count += 1 | |
}); | |
Iterator[Integer](count) | |
}); | |
} | |
} |