This repository has been archived by the owner on Mar 7, 2018. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement production cassandra schema
- Loading branch information
Showing
10 changed files
with
125 additions
and
9 deletions.
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
src/main/scala/com/microsoft/partnercatalyst/fortis/spark/dto/AnalyzedItem.scala
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
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
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
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
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
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
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
96 changes: 87 additions & 9 deletions
96
...in/scala/com/microsoft/partnercatalyst/fortis/spark/sinks/cassandra/CassandraSchema.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,95 @@ | ||
package com.microsoft.partnercatalyst.fortis.spark.sinks.cassandra | ||
|
||
import java.time.Instant.now | ||
import java.util.UUID | ||
|
||
import com.microsoft.partnercatalyst.fortis.spark.dto.AnalyzedItem | ||
import com.microsoft.partnercatalyst.fortis.spark.sinks.cassandra.Utils.{mean, rescale} | ||
import com.microsoft.partnercatalyst.fortis.spark.transforms.sentiment.SentimentDetector.Neutral | ||
|
||
case class Sentiment( | ||
pos_avg: Float, | ||
neg_avg: Float) | ||
|
||
case class Gender( | ||
male_mentions: Int, | ||
female_mentions: Int) | ||
|
||
// todo: update with real schema once defined on the cassandra db | ||
case class CassandraRow( | ||
created_at: Long, | ||
pipeline: String) | ||
case class Entities( | ||
name: String, | ||
externalsource: String, | ||
externalrefid: String, | ||
count: Float) | ||
|
||
case class Feature( | ||
mentions: Int, | ||
sentiment: Set[Sentiment], | ||
gender: Set[Gender], | ||
entities: Set[Entities]) | ||
|
||
case class Event( | ||
pipeline: String, | ||
externalid: String, | ||
computedfeatures: Set[Feature], | ||
detectedkeywords: Set[String], | ||
detectedplaceids: Set[String], | ||
event_time: Long, | ||
eventlangcode: String, | ||
id: UUID, | ||
insertion_time: Long, | ||
messagebody: String, | ||
sourceid: String, | ||
sourceurl: String, | ||
title: String) | ||
|
||
object CassandraSchema { | ||
def apply(item: AnalyzedItem): CassandraRow = { | ||
CassandraRow( | ||
created_at = item.createdAtEpoch, | ||
pipeline = item.publisher | ||
) | ||
def apply(item: AnalyzedItem): Event = { | ||
Event( | ||
pipeline = item.publisher, | ||
externalid = null, // todo | ||
computedfeatures = Set(getFeature(item)), | ||
detectedkeywords = item.analysis.keywords.map(_.name).toSet, | ||
detectedplaceids = item.analysis.locations.map(_.wofId).toSet, | ||
event_time = item.createdAtEpoch, | ||
eventlangcode = item.analysis.language.orNull, | ||
id = item.id, | ||
insertion_time = now.getEpochSecond, | ||
messagebody = item.body, | ||
sourceid = null, // todo | ||
sourceurl = item.sourceUrl, | ||
title = item.title) | ||
} | ||
|
||
private def getFeature(item: AnalyzedItem): Feature = { | ||
val genderCounts = item.analysis.genders.map(_.name).groupBy(identity).mapValues(_.size) | ||
val entityCounts = item.analysis.entities.map(_.name).groupBy(identity).mapValues(_.size) | ||
val (positiveSentiments, negativeSentiments) = item.analysis.sentiments.partition(_ > Neutral) | ||
Feature( | ||
mentions = -1, | ||
sentiment = Set(Sentiment( | ||
pos_avg = mean(rescale(positiveSentiments, 0, 1)).toFloat, | ||
neg_avg = mean(rescale(negativeSentiments, 0, 1)).toFloat)), | ||
gender = Set(Gender( | ||
male_mentions = genderCounts("M"), | ||
female_mentions = genderCounts("F"))), | ||
entities = entityCounts.map(kv => Entities( | ||
name = kv._1, | ||
count = kv._2, | ||
externalsource = null, | ||
externalrefid = null)).toSet) | ||
} | ||
} | ||
|
||
object Utils { | ||
def mean(items: List[Double]): Double = { | ||
items.sum / items.length | ||
} | ||
|
||
/** @see https://stats.stackexchange.com/a/25897 */ | ||
def rescale(items: List[Double], min_new: Double, max_new: Double): List[Double] = { | ||
val min_old = items.min | ||
val max_old = items.max | ||
val coef = (max_new - min_new) / (max_old - min_old) | ||
items.map(v => coef * (v - max_old) + max_new) | ||
} | ||
} |
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
18 changes: 18 additions & 0 deletions
18
src/test/scala/com/microsoft/partnercatalyst/fortis/spark/sinks/cassandra/UtilsSpec.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.microsoft.partnercatalyst.fortis.spark.sinks.cassandra | ||
|
||
import java.text.DecimalFormat | ||
|
||
import org.scalatest.FlatSpec | ||
|
||
class UtilsSpec extends FlatSpec { | ||
"The mean function" should "compute the mean" in { | ||
assert(Utils.mean(List(1, 2, 3, 4)) == 2.5) | ||
assert(Utils.mean(List(1.2, -0.2, 1.0, 2)) == 1.0) | ||
} | ||
|
||
"The rescale function" should "rescale numbers" in { | ||
val formatter = new DecimalFormat("#.#") | ||
assert(Utils.rescale(List(10, 20, 30), 1, 3) == List(1, 2, 3)) | ||
assert(Utils.rescale(List(0.2, 0.4, 0.6), 0, 1).map(formatter.format) == List("0", "0.5", "1")) | ||
} | ||
} |