java.util.HashMap Scala Examples

The following examples show how to use java.util.HashMap. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Source File: SchematicBreedingReactor.scala    From Electrodynamics   with GNU Lesser General Public License v3.0 5 votes vote down vote up
package com.calclavia.edx.quantum.schematic

import java.util.HashMap

import com.calclavia.edx.electrical.ElectricalContent
import com.calclavia.edx.quantum.QuantumContent
import ElectricalContent
import QuantumContent
import net.minecraft.block.Block
import net.minecraft.init.Blocks
import net.minecraftforge.common.util.ForgeDirection
import resonantengine.lib.collection.Pair
import resonantengine.lib.schematic.Schematic
import resonantengine.lib.transform.vector.Vector3

class SchematicBreedingReactor extends Schematic
{
  override def getName: String =
  {
    return "schematic.breedingReactor.name"
  }

  override def getStructure(dir: ForgeDirection, size: Int): HashMap[Vector3, Pair[Block, Integer]] =
  {
    val returnMap: HashMap[Vector3, Pair[Block, Integer]] = new HashMap[Vector3, Pair[Block, Integer]]
    var r: Int = Math.max(size, 2)

    for (x <- -r to r)
    {
      for (z <- -r to r)
      {
        returnMap.put(new Vector3(x, 0, z), new Pair[Block, Integer](Blocks.water, 0))
      }
    }

    r -= 1

    for (x <- -r to r)
    {
      for (z <- -r to r)
      {
        val targetPosition: Vector3 = new Vector3(x, 1, z)
        if (new Vector3(x, 0, z).magnitude <= 2)
        {
          if (!((x == -r || x == r) && (z == -r || z == r)))
          {
            returnMap.put(new Vector3(x, 0, z), new Pair[Block, Integer](QuantumContent.blockReactorCell, 0))
            returnMap.put(new Vector3(x, -3, z), new Pair[Block, Integer](ElectricalContent.blockSiren, 0))
            returnMap.put(new Vector3(x, -2, z), new Pair[Block, Integer](Blocks.redstone_wire, 0))
          }
          else
          {
            returnMap.put(new Vector3(x, -1, z), new Pair[Block, Integer](QuantumContent.blockControlRod, 0))
            returnMap.put(new Vector3(x, -2, z), new Pair[Block, Integer](Blocks.piston, 1))
          }
        }
      }
    }

    returnMap.put(new Vector3(0, -2, 0), new Pair[Block, Integer](Blocks.stone, 0))
    returnMap.put(new Vector3(0, -3, 0), new Pair[Block, Integer](Blocks.stone, 0))
    returnMap.put(new Vector3, new Pair[Block, Integer](QuantumContent.blockReactorCell, 0))
    return returnMap
  }

} 
Example 2
Source File: RddToDataFrame.scala    From spark-sframe   with BSD 2-Clause "Simplified" License 5 votes vote down vote up
package org.apache.spark.turi

import org.graphlab.create.GraphLabUtil
import org.apache.spark.sql.{SQLContext, Row, DataFrame}
import org.apache.spark.rdd.RDD
import scala.collection.JavaConversions._
import org.apache.spark.sql.types._
import scala.collection.mutable.ListBuffer
import scala.collection.mutable.ArrayBuffer
import scala.collection.immutable.Map
import java.util.HashMap
import java.util.ArrayList
import java.util.{Date,GregorianCalendar}
import java.sql.Date

object EvaluateRDD {
  
  def inferSchema(obj: Any): DataType = {
    if(obj.isInstanceOf[Int]) { 
      IntegerType
    } else if(obj.isInstanceOf[String]) { 
      StringType
    } else if(obj.isInstanceOf[Double]) { 
      DoubleType
    } else if(obj.isInstanceOf[Long]) { 
      LongType
    } else if(obj.isInstanceOf[Float]) { 
      FloatType
    } else if(obj.isInstanceOf[Map[_,_]]) {
      MapType(inferSchema(obj.asInstanceOf[Map[_,_]].head._1),inferSchema(obj.asInstanceOf[Map[_,_]].head._2))
    } else if(obj.isInstanceOf[java.util.HashMap[_,_]]) {
      MapType(inferSchema(obj.asInstanceOf[java.util.HashMap[_,_]].head._1),inferSchema(obj.asInstanceOf[java.util.HashMap[_,_]].head._2))
    } else if(obj.isInstanceOf[Array[_]]) {
      ArrayType(inferSchema(obj.asInstanceOf[Array[_]](0)))
    } else if(obj.isInstanceOf[java.util.ArrayList[_]]) {
      ArrayType(inferSchema(obj.asInstanceOf[java.util.ArrayList[_]](0)))
    } else if(obj.isInstanceOf[java.util.GregorianCalendar]) {
      TimestampType
    } else if(obj.isInstanceOf[java.util.Date] || obj.isInstanceOf[java.sql.Date]) {
      DateType
    } else { 
      StringType
    }
  }

  def toScala(obj: Any): Any = {
    if (obj.isInstanceOf[java.util.HashMap[_,_]]) {
      val jmap = obj.asInstanceOf[java.util.HashMap[_,_]]
      jmap.map { case (k,v) => toScala(k) -> toScala(v) }.toMap
    }
    else if(obj.isInstanceOf[java.util.ArrayList[_]]) {
      val buf = ArrayBuffer[Any]()
      val jArray = obj.asInstanceOf[java.util.ArrayList[_]]
      for(item <- jArray) {
        buf += toScala(item)
      }
      buf.toArray
    } else if(obj.isInstanceOf[java.util.GregorianCalendar]) {
      new java.sql.Timestamp(obj.asInstanceOf[java.util.GregorianCalendar].getTime().getTime())
    } else {
      obj
    }
  }
  def toSparkDataFrame(sqlContext: SQLContext, rdd: RDD[java.util.HashMap[String,_]]): DataFrame = { 
    val scalaRDD = rdd.map(l => toScala(l))
    val rowRDD = scalaRDD.map(l => Row.fromSeq(l.asInstanceOf[Map[_,_]].values.toList))
    
    var sample_data: java.util.HashMap[String,_] = rdd.take(1)(0)
    
    var schema_list: ListBuffer[StructField] = new ListBuffer[StructField]()
    for ((name,v) <- sample_data) { 
      schema_list.append(StructField(name,inferSchema(v)))
    }
    sqlContext.createDataFrame(rowRDD,StructType(schema_list))
  }
} 
Example 3
Source File: TestUtils.scala    From shc   with Apache License 2.0 5 votes vote down vote up
package org.apache.spark.sql

import java.nio.ByteBuffer
import java.util.{ArrayList, HashMap}

import scala.util.Random

object TestUtils {

  def generateRandomByteBuffer(rand: Random, size: Int): ByteBuffer = {
    val bb = ByteBuffer.allocate(size)
    val arrayOfBytes = new Array[Byte](size)
    rand.nextBytes(arrayOfBytes)
    bb.put(arrayOfBytes)
  }

  def generateRandomMap(rand: Random, size: Int): java.util.Map[String, Int] = {
    val jMap = new HashMap[String, Int]()
    for (i <- 0 until size) {
      jMap.put(rand.nextString(5), i)
    }
    jMap
  }

  def generateRandomArray(rand: Random, size: Int): ArrayList[Boolean] = {
    val vec = new ArrayList[Boolean]()
    for (i <- 0 until size) {
      vec.add(rand.nextBoolean())
    }
    vec
  }
} 
Example 4
Source File: GraphUtil.scala    From lms-clean   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package lms.util

import java.util.{ArrayDeque, HashMap}


object GraphUtil {
  
  class Ref[T](init: T) {
    var value: T = init
  }
  
  
  def stronglyConnectedComponents[T](start: List[T], succ: T=>List[T]): List[List[T]] = {

    val id: Ref[Int] = new Ref(0)
    val stack = new ArrayDeque[T]
    val mark = new HashMap[T,Int]

    val res = new Ref[List[List[T]]](Nil)
    for (node <- start)
      visit(node,succ,id,stack,mark,res)

    res.value
  }

  def visit[T](node: T, succ: T=>List[T], id: Ref[Int], stack: ArrayDeque[T], 
            mark: HashMap[T,Int], res: Ref[List[List[T]]]): Int = {

    
    if (mark.containsKey(node)) 
      mark.get(node)
    else {
      id.value = id.value + 1

      mark.put(node, id.value)
      stack.addFirst(node)
//    println("push " + node)

      var min: Int = id.value
      for (child <- succ(node)) {
        val m = visit(child, succ, id, stack, mark, res)

        if (m < min) 
          min = m
      }

      if (min == mark.get(node)) {
        var scc: List[T] = Nil
        var loop: Boolean = true
        do {
          val element = stack.removeFirst()
//        println("appending " + element)
          scc ::= element
          mark.put(element, Integer.MAX_VALUE)
          loop = element != node
        } while (loop)
        res.value ::= scc
      }
      min
    }
  }
  
} 
Example 5
Source File: AnyVals.scala    From sigmastate-interpreter   with MIT License 5 votes vote down vote up
package scalan

import java.util.HashMap


class AVHashMap[K,V](val hashMap: HashMap[K,V]) extends AnyVal {
  @inline final def isEmpty: Boolean = hashMap.isEmpty
  @inline final def get(key: K): Nullable[V] = Nullable(hashMap.get(key))
  @inline final def apply(key: K): V = hashMap.get(key)
  @inline final def containsKey(key: K): Boolean = hashMap.containsKey(key)
  @inline final def put(key: K, value: V): V = hashMap.put(key, value)
  @inline final def clear(): Unit = {
    hashMap.clear()
  }
  final def getOrElseUpdate(key: K, op: => V): V = {
    var v = hashMap.get(key)
    if (v == null) {
      v = op
      hashMap.put(key, v)
    }
    v
  }
  @inline final def keySet: java.util.Set[K] = hashMap.keySet()
}
object AVHashMap {
  def apply[K,V](initialCapacity: Int) = new AVHashMap[K,V](new HashMap[K,V](initialCapacity))
} 
Example 6
Source File: HogEvent.scala    From hogzilla   with GNU General Public License v2.0 5 votes vote down vote up
package org.hogzilla.event

import java.util.HashMap
import java.util.Map
import org.apache.hadoop.hbase.client.Put
import org.apache.hadoop.hbase.util.Bytes
import org.hogzilla.hbase.HogHBaseRDD
import org.hogzilla.util.HogFlow
import java.net.InetAddress


class HogEvent(flow:HogFlow) 
{
	var sensorid:Int=0
	var signature_id:Double=0
	var priorityid:Int=0
	var text:String=""
	var data:Map[String,String]=new HashMap()
  var ports:String=""
  var title:String=""
  var username:String=""
  var coords:String=""
 
  
  def formatIPtoBytes(ip:String):Array[Byte] =
  {
    try {
       // Eca! Snorby doesn't support IPv6 yet. See https://github.com/Snorby/snorby/issues/65
    if(ip.contains(":"))
      InetAddress.getByName("255.255.6.6").getAddress
    else  
      InetAddress.getByName(ip).getAddress
    } catch {
      case t: Throwable => 
        // Bogus address!
        InetAddress.getByName("255.255.1.1").getAddress
    }   
   
  }

  
   def alert()
   {
	   val put = new Put(Bytes.toBytes(flow.get("flow:id")))
     put.add(Bytes.toBytes("event"), Bytes.toBytes("note"), Bytes.toBytes(text))
     put.add(Bytes.toBytes("event"), Bytes.toBytes("lower_ip"), formatIPtoBytes(flow.lower_ip))
     put.add(Bytes.toBytes("event"), Bytes.toBytes("upper_ip"), formatIPtoBytes(flow.upper_ip))
     put.add(Bytes.toBytes("event"), Bytes.toBytes("lower_ip_str"), Bytes.toBytes(flow.lower_ip))
     put.add(Bytes.toBytes("event"), Bytes.toBytes("upper_ip_str"), Bytes.toBytes(flow.upper_ip))
     put.add(Bytes.toBytes("event"), Bytes.toBytes("signature_id"), Bytes.toBytes("%.0f".format(signature_id)))
     put.add(Bytes.toBytes("event"), Bytes.toBytes("time"), Bytes.toBytes(System.currentTimeMillis))
     put.add(Bytes.toBytes("event"), Bytes.toBytes("ports"), Bytes.toBytes(ports))
     put.add(Bytes.toBytes("event"), Bytes.toBytes("title"), Bytes.toBytes(title))
     
     if(!username.equals(""))
       put.add(Bytes.toBytes("event"), Bytes.toBytes("username"), Bytes.toBytes(username))
     if(!coords.equals(""))
       put.add(Bytes.toBytes("event"), Bytes.toBytes("coords"), Bytes.toBytes(coords))
     
     HogHBaseRDD.hogzilla_events.put(put)

     //println(f"ALERT: $text%100s\n\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
   }
} 
Example 7
Source File: KafkaWordCount.scala    From BigDatalog   with Apache License 2.0 5 votes vote down vote up
// scalastyle:off println
package org.apache.spark.examples.streaming

import java.util.HashMap

import org.apache.kafka.clients.producer.{ProducerConfig, KafkaProducer, ProducerRecord}

import org.apache.spark.streaming._
import org.apache.spark.streaming.kafka._
import org.apache.spark.SparkConf


object KafkaWordCount {
  def main(args: Array[String]) {
    if (args.length < 4) {
      System.err.println("Usage: KafkaWordCount <zkQuorum> <group> <topics> <numThreads>")
      System.exit(1)
    }

    StreamingExamples.setStreamingLogLevels()

    val Array(zkQuorum, group, topics, numThreads) = args
    val sparkConf = new SparkConf().setAppName("KafkaWordCount")
    val ssc = new StreamingContext(sparkConf, Seconds(2))
    ssc.checkpoint("checkpoint")

    val topicMap = topics.split(",").map((_, numThreads.toInt)).toMap
    val lines = KafkaUtils.createStream(ssc, zkQuorum, group, topicMap).map(_._2)
    val words = lines.flatMap(_.split(" "))
    val wordCounts = words.map(x => (x, 1L))
      .reduceByKeyAndWindow(_ + _, _ - _, Minutes(10), Seconds(2), 2)
    wordCounts.print()

    ssc.start()
    ssc.awaitTermination()
  }
}

// Produces some random words between 1 and 100.
object KafkaWordCountProducer {

  def main(args: Array[String]) {
    if (args.length < 4) {
      System.err.println("Usage: KafkaWordCountProducer <metadataBrokerList> <topic> " +
        "<messagesPerSec> <wordsPerMessage>")
      System.exit(1)
    }

    val Array(brokers, topic, messagesPerSec, wordsPerMessage) = args

    // Zookeeper connection properties
    val props = new HashMap[String, Object]()
    props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, brokers)
    props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,
      "org.apache.kafka.common.serialization.StringSerializer")
    props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,
      "org.apache.kafka.common.serialization.StringSerializer")

    val producer = new KafkaProducer[String, String](props)

    // Send some messages
    while(true) {
      (1 to messagesPerSec.toInt).foreach { messageNum =>
        val str = (1 to wordsPerMessage.toInt).map(x => scala.util.Random.nextInt(10).toString)
          .mkString(" ")

        val message = new ProducerRecord[String, String](topic, null, str)
        producer.send(message)
      }

      Thread.sleep(1000)
    }
  }

}
// scalastyle:on println 
Example 8
Source File: KafkaWordCount.scala    From spark1.52   with Apache License 2.0 5 votes vote down vote up
// scalastyle:off println
package org.apache.spark.examples.streaming

import java.util.HashMap

import org.apache.kafka.clients.producer.{KafkaProducer, ProducerConfig, ProducerRecord}
import org.apache.spark.SparkConf
import org.apache.spark.streaming._
import org.apache.spark.streaming.kafka.KafkaUtils



    StreamingExamples.setStreamingLogLevels()

    val Array(zkQuorum, group, topics, numThreads) = Array("localhost:2181","","topic1,topic2,topic3,topic4","1")//args
    val sparkConf = new SparkConf().setAppName("KafkaWordCount").setMaster("local")
    val ssc = new StreamingContext(sparkConf, Seconds(2))
    ssc.checkpoint("checkpoint")

    val topicMap = topics.split(",").map((_, numThreads.toInt)).toMap
    val lines = KafkaUtils.createStream(ssc, zkQuorum, group, topicMap).map(_._2)
    val words = lines.flatMap(_.split(" "))
    val wordCounts = words.map(x => (x, 1L))
      .reduceByKeyAndWindow(_ + _, _ - _, Minutes(10), Seconds(2), 2)
    wordCounts.print()

    ssc.start()
    ssc.awaitTermination()
  }
}

// Produces some random words between 1 and 100.
//
object KafkaWordCountProducer {

  def main(args: Array[String]) {
    if (args.length < 4) {
      System.err.println("Usage: KafkaWordCountProducer <metadataBrokerList> <topic> " +
        "<messagesPerSec> <wordsPerMessage>")
      System.exit(1)
    }

    val Array(brokers, topic, messagesPerSec, wordsPerMessage) = args

    // Zookeeper connection properties
    val props = new HashMap[String, Object]()
    props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, brokers)
    props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,
      "org.apache.kafka.common.serialization.StringSerializer")
    props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,
      "org.apache.kafka.common.serialization.StringSerializer")

    val producer = new KafkaProducer[String, String](props)

    // Send some messages
    while(true) {
      (1 to messagesPerSec.toInt).foreach { messageNum =>
        val str = (1 to wordsPerMessage.toInt).map(x => scala.util.Random.nextInt(10).toString)
          .mkString(" ")

        val message = new ProducerRecord[String, String](topic, null, str)
        producer.send(message)
      }

      Thread.sleep(1000)
    }
  }

}
// scalastyle:on println 
Example 9
Source File: KafkaWordCount.scala    From iolap   with Apache License 2.0 5 votes vote down vote up
package org.apache.spark.examples.streaming

import java.util.HashMap

import org.apache.kafka.clients.producer.{ProducerConfig, KafkaProducer, ProducerRecord}

import org.apache.spark.streaming._
import org.apache.spark.streaming.kafka._
import org.apache.spark.SparkConf


object KafkaWordCount {
  def main(args: Array[String]) {
    if (args.length < 4) {
      System.err.println("Usage: KafkaWordCount <zkQuorum> <group> <topics> <numThreads>")
      System.exit(1)
    }

    StreamingExamples.setStreamingLogLevels()

    val Array(zkQuorum, group, topics, numThreads) = args
    val sparkConf = new SparkConf().setAppName("KafkaWordCount")
    val ssc = new StreamingContext(sparkConf, Seconds(2))
    ssc.checkpoint("checkpoint")

    val topicMap = topics.split(",").map((_, numThreads.toInt)).toMap
    val lines = KafkaUtils.createStream(ssc, zkQuorum, group, topicMap).map(_._2)
    val words = lines.flatMap(_.split(" "))
    val wordCounts = words.map(x => (x, 1L))
      .reduceByKeyAndWindow(_ + _, _ - _, Minutes(10), Seconds(2), 2)
    wordCounts.print()

    ssc.start()
    ssc.awaitTermination()
  }
}

// Produces some random words between 1 and 100.
object KafkaWordCountProducer {

  def main(args: Array[String]) {
    if (args.length < 4) {
      System.err.println("Usage: KafkaWordCountProducer <metadataBrokerList> <topic> " +
        "<messagesPerSec> <wordsPerMessage>")
      System.exit(1)
    }

    val Array(brokers, topic, messagesPerSec, wordsPerMessage) = args

    // Zookeeper connection properties
    val props = new HashMap[String, Object]()
    props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, brokers)
    props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,
      "org.apache.kafka.common.serialization.StringSerializer")
    props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,
      "org.apache.kafka.common.serialization.StringSerializer")

    val producer = new KafkaProducer[String, String](props)

    // Send some messages
    while(true) {
      (1 to messagesPerSec.toInt).foreach { messageNum =>
        val str = (1 to wordsPerMessage.toInt).map(x => scala.util.Random.nextInt(10).toString)
          .mkString(" ")

        val message = new ProducerRecord[String, String](topic, null, str)
        producer.send(message)
      }

      Thread.sleep(1000)
    }
  }

} 
Example 10
Source File: KafkaWordCount.scala    From multi-tenancy-spark   with Apache License 2.0 5 votes vote down vote up
// scalastyle:off println
package org.apache.spark.examples.streaming

import java.util.HashMap

import org.apache.kafka.clients.producer.{KafkaProducer, ProducerConfig, ProducerRecord}

import org.apache.spark.SparkConf
import org.apache.spark.streaming._
import org.apache.spark.streaming.kafka._


object KafkaWordCount {
  def main(args: Array[String]) {
    if (args.length < 4) {
      System.err.println("Usage: KafkaWordCount <zkQuorum> <group> <topics> <numThreads>")
      System.exit(1)
    }

    StreamingExamples.setStreamingLogLevels()

    val Array(zkQuorum, group, topics, numThreads) = args
    val sparkConf = new SparkConf().setAppName("KafkaWordCount")
    val ssc = new StreamingContext(sparkConf, Seconds(2))
    ssc.checkpoint("checkpoint")

    val topicMap = topics.split(",").map((_, numThreads.toInt)).toMap
    val lines = KafkaUtils.createStream(ssc, zkQuorum, group, topicMap).map(_._2)
    val words = lines.flatMap(_.split(" "))
    val wordCounts = words.map(x => (x, 1L))
      .reduceByKeyAndWindow(_ + _, _ - _, Minutes(10), Seconds(2), 2)
    wordCounts.print()

    ssc.start()
    ssc.awaitTermination()
  }
}

// Produces some random words between 1 and 100.
object KafkaWordCountProducer {

  def main(args: Array[String]) {
    if (args.length < 4) {
      System.err.println("Usage: KafkaWordCountProducer <metadataBrokerList> <topic> " +
        "<messagesPerSec> <wordsPerMessage>")
      System.exit(1)
    }

    val Array(brokers, topic, messagesPerSec, wordsPerMessage) = args

    // Zookeeper connection properties
    val props = new HashMap[String, Object]()
    props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, brokers)
    props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,
      "org.apache.kafka.common.serialization.StringSerializer")
    props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,
      "org.apache.kafka.common.serialization.StringSerializer")

    val producer = new KafkaProducer[String, String](props)

    // Send some messages
    while(true) {
      (1 to messagesPerSec.toInt).foreach { messageNum =>
        val str = (1 to wordsPerMessage.toInt).map(x => scala.util.Random.nextInt(10).toString)
          .mkString(" ")

        val message = new ProducerRecord[String, String](topic, null, str)
        producer.send(message)
      }

      Thread.sleep(1000)
    }
  }

}
// scalastyle:on println 
Example 11
Source File: KafkaWordCount.scala    From sparkoscope   with Apache License 2.0 5 votes vote down vote up
// scalastyle:off println
package org.apache.spark.examples.streaming

import java.util.HashMap

import org.apache.kafka.clients.producer.{KafkaProducer, ProducerConfig, ProducerRecord}

import org.apache.spark.SparkConf
import org.apache.spark.streaming._
import org.apache.spark.streaming.kafka._


object KafkaWordCount {
  def main(args: Array[String]) {
    if (args.length < 4) {
      System.err.println("Usage: KafkaWordCount <zkQuorum> <group> <topics> <numThreads>")
      System.exit(1)
    }

    StreamingExamples.setStreamingLogLevels()

    val Array(zkQuorum, group, topics, numThreads) = args
    val sparkConf = new SparkConf().setAppName("KafkaWordCount")
    val ssc = new StreamingContext(sparkConf, Seconds(2))
    ssc.checkpoint("checkpoint")

    val topicMap = topics.split(",").map((_, numThreads.toInt)).toMap
    val lines = KafkaUtils.createStream(ssc, zkQuorum, group, topicMap).map(_._2)
    val words = lines.flatMap(_.split(" "))
    val wordCounts = words.map(x => (x, 1L))
      .reduceByKeyAndWindow(_ + _, _ - _, Minutes(10), Seconds(2), 2)
    wordCounts.print()

    ssc.start()
    ssc.awaitTermination()
  }
}

// Produces some random words between 1 and 100.
object KafkaWordCountProducer {

  def main(args: Array[String]) {
    if (args.length < 4) {
      System.err.println("Usage: KafkaWordCountProducer <metadataBrokerList> <topic> " +
        "<messagesPerSec> <wordsPerMessage>")
      System.exit(1)
    }

    val Array(brokers, topic, messagesPerSec, wordsPerMessage) = args

    // Zookeeper connection properties
    val props = new HashMap[String, Object]()
    props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, brokers)
    props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,
      "org.apache.kafka.common.serialization.StringSerializer")
    props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,
      "org.apache.kafka.common.serialization.StringSerializer")

    val producer = new KafkaProducer[String, String](props)

    // Send some messages
    while(true) {
      (1 to messagesPerSec.toInt).foreach { messageNum =>
        val str = (1 to wordsPerMessage.toInt).map(x => scala.util.Random.nextInt(10).toString)
          .mkString(" ")

        val message = new ProducerRecord[String, String](topic, null, str)
        producer.send(message)
      }

      Thread.sleep(1000)
    }
  }

}
// scalastyle:on println 
Example 12
Source File: KafkaWordCount.scala    From drizzle-spark   with Apache License 2.0 5 votes vote down vote up
// scalastyle:off println
package org.apache.spark.examples.streaming

import java.util.HashMap

import org.apache.kafka.clients.producer.{KafkaProducer, ProducerConfig, ProducerRecord}

import org.apache.spark.SparkConf
import org.apache.spark.streaming._
import org.apache.spark.streaming.kafka._


object KafkaWordCount {
  def main(args: Array[String]) {
    if (args.length < 4) {
      System.err.println("Usage: KafkaWordCount <zkQuorum> <group> <topics> <numThreads>")
      System.exit(1)
    }

    StreamingExamples.setStreamingLogLevels()

    val Array(zkQuorum, group, topics, numThreads) = args
    val sparkConf = new SparkConf().setAppName("KafkaWordCount")
    val ssc = new StreamingContext(sparkConf, Seconds(2))
    ssc.checkpoint("checkpoint")

    val topicMap = topics.split(",").map((_, numThreads.toInt)).toMap
    val lines = KafkaUtils.createStream(ssc, zkQuorum, group, topicMap).map(_._2)
    val words = lines.flatMap(_.split(" "))
    val wordCounts = words.map(x => (x, 1L))
      .reduceByKeyAndWindow(_ + _, _ - _, Minutes(10), Seconds(2), 2)
    wordCounts.print()

    ssc.start()
    ssc.awaitTermination()
  }
}

// Produces some random words between 1 and 100.
object KafkaWordCountProducer {

  def main(args: Array[String]) {
    if (args.length < 4) {
      System.err.println("Usage: KafkaWordCountProducer <metadataBrokerList> <topic> " +
        "<messagesPerSec> <wordsPerMessage>")
      System.exit(1)
    }

    val Array(brokers, topic, messagesPerSec, wordsPerMessage) = args

    // Zookeeper connection properties
    val props = new HashMap[String, Object]()
    props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, brokers)
    props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,
      "org.apache.kafka.common.serialization.StringSerializer")
    props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,
      "org.apache.kafka.common.serialization.StringSerializer")

    val producer = new KafkaProducer[String, String](props)

    // Send some messages
    while(true) {
      (1 to messagesPerSec.toInt).foreach { messageNum =>
        val str = (1 to wordsPerMessage.toInt).map(x => scala.util.Random.nextInt(10).toString)
          .mkString(" ")

        val message = new ProducerRecord[String, String](topic, null, str)
        producer.send(message)
      }

      Thread.sleep(1000)
    }
  }

}
// scalastyle:on println 
Example 13
Source File: SchematicFusionReactor.scala    From Electrodynamics   with GNU Lesser General Public License v3.0 5 votes vote down vote up
package com.calclavia.edx.quantum.schematic

import java.util.HashMap
import com.calclavia.edx.quantum.QuantumContent
import net.minecraft.block.Block
import net.minecraft.init.Blocks
import net.minecraftforge.common.util.ForgeDirection
import resonantengine.lib.collection.Pair
import resonantengine.lib.schematic.Schematic
import resonantengine.lib.transform.vector.Vector3

class SchematicFusionReactor extends Schematic
{
  override def getName: String =
  {
    return "schematic.fusionReactor.name"
  }

  def getStructure(dir: ForgeDirection, size: Int): HashMap[Vector3, Pair[Block, Integer]] =
  {
    val returnMap: HashMap[Vector3, Pair[Block, Integer]] = new HashMap[Vector3, Pair[Block, Integer]]
    val r: Int = size + 2
    for (y <- 0 to size; x <- -r to r; z <- -r to r)
    {
      val position: Vector3 = new Vector3(x, y, z)
      val magnitude: Double = Math.sqrt(x * x + z * z)
      if (!returnMap.containsKey(position))
      {
        returnMap.put(position, new Pair[Block, Integer](Blocks.air, 0))
      }
      if (magnitude <= r)
      {
        if (y == 0 || y == size)
        {
          if (magnitude >= 1)
          {
            val yDeviation: Double = (if (y == 0) size / 3 else -size / 3) + (if (y == 0) -1 else 1) * Math.sin(magnitude / r * Math.PI) * size / 2d
            val newPos: Vector3 = position.clone.add(0, yDeviation, 0)
            returnMap.put(newPos.round, new Pair[Block, Integer](QuantumContent.blockElectromagnet, 1))
          }
        }
        else if (magnitude > r - 1)
        {
          returnMap.put(position, new Pair[Block, Integer](QuantumContent.blockElectromagnet, 0))
        }
      }
    }

    for (y <- 0 to size)
    {
      returnMap.put(new Vector3(0, y, 0), new Pair[Block, Integer](QuantumContent.blockReactorCell, 0))
      returnMap.put(new Vector3(1, y, 0), new Pair[Block, Integer](QuantumContent.blockElectromagnet, 0))
      returnMap.put(new Vector3(0, y, 1), new Pair[Block, Integer](QuantumContent.blockElectromagnet, 0))
      returnMap.put(new Vector3(0, y, -1), new Pair[Block, Integer](QuantumContent.blockElectromagnet, 0))
      returnMap.put(new Vector3(-1, y, 0), new Pair[Block, Integer](QuantumContent.blockElectromagnet, 0))

    }
    returnMap.put(new Vector3(0, 0, 0), new Pair[Block, Integer](QuantumContent.blockReactorCell, 0))
    return returnMap
  }
} 
Example 14
Source File: SchematicAccelerator.scala    From Electrodynamics   with GNU Lesser General Public License v3.0 5 votes vote down vote up
package com.calclavia.edx.quantum.schematic

import java.util.HashMap

import com.calclavia.edx.quantum.QuantumContent
import QuantumContent
import net.minecraft.block.Block
import net.minecraft.init.Blocks
import net.minecraftforge.common.util.ForgeDirection
import resonantengine.lib.collection.Pair
import resonantengine.lib.schematic.Schematic
import resonantengine.lib.transform.vector.Vector3

class SchematicAccelerator extends Schematic
{
  override def getName: String =
  {
    return "schematic.accelerator.name"
  }

  def getStructure(dir: ForgeDirection, size: Int): HashMap[Vector3, Pair[Block, Integer]] =
  {
    val returnMap: HashMap[Vector3, Pair[Block, Integer]] = new HashMap[Vector3, Pair[Block, Integer]]

    //Bottom
    returnMap.putAll(getBox(new Vector3(0, 0, 0), QuantumContent.blockElectromagnet, 1, size))
    returnMap.putAll(getBox(new Vector3(0, 0, 0), QuantumContent.blockElectromagnet, 0, size - 1))
    returnMap.putAll(getBox(new Vector3(0, 0, 0), QuantumContent.blockElectromagnet, 0, size + 1))
    //Mid
    returnMap.putAll(getBox(new Vector3(0, 1, 0), Blocks.air, 0, size))
    returnMap.putAll(getBox(new Vector3(0, 1, 0), QuantumContent.blockElectromagnet, 1, size - 1))
    returnMap.putAll(getBox(new Vector3(0, 1, 0), QuantumContent.blockElectromagnet, 1, size + 1))
    //Top
    returnMap.putAll(getBox(new Vector3(0, 2, 0), QuantumContent.blockElectromagnet, 1, size))
    returnMap.putAll(getBox(new Vector3(0, 2, 0), QuantumContent.blockElectromagnet, 0, size - 1))
    returnMap.putAll(getBox(new Vector3(0, 2, 0), QuantumContent.blockElectromagnet, 0, size + 1))

    return returnMap
  }
} 
Example 15
Source File: SchematicFissionReactor.scala    From Electrodynamics   with GNU Lesser General Public License v3.0 5 votes vote down vote up
package com.calclavia.edx.quantum.schematic

import java.util.HashMap

import com.calclavia.edx.electrical.ElectricalContent
import ElectricalContent
import com.calclavia.edx.quantum.QuantumContent
import net.minecraft.block.Block
import net.minecraft.init.Blocks
import net.minecraftforge.common.util.ForgeDirection
import resonantengine.lib.collection.Pair
import resonantengine.lib.schematic.Schematic
import resonantengine.lib.transform.vector.Vector3

class SchematicFissionReactor extends Schematic
{
  override def getName: String =
  {
    return "schematic.fissionReactor.name"
  }

  def getStructure(dir: ForgeDirection, size: Int): HashMap[Vector3, Pair[Block, Integer]] =
  {
    val returnMap: HashMap[Vector3, Pair[Block, Integer]] = new HashMap[Vector3, Pair[Block, Integer]]
    if (size <= 1)
    {
      var r: Int = 2

      for (x <- -r to r; z <- -r to r)
      {
        val targetPosition: Vector3 = new Vector3(x, 0, z)
        returnMap.put(targetPosition, new Pair[Block, Integer](Blocks.water, 0))
      }

      r -= 1
      for (x <- -r to r; z <- -r to r)
      {
        val targetPosition: Vector3 = new Vector3(x, 1, z)
        returnMap.put(targetPosition, new Pair[Block, Integer](Block.getBlockFromName("electricTurbine"), 0))
        if (!((x == -r || x == r) && (z == -r || z == r)) && new Vector3(x, 0, z).magnitude <= 1)
        {
          returnMap.put(new Vector3(x, -1, z), new Pair[Block, Integer](QuantumContent.blockControlRod, 0))
          returnMap.put(new Vector3(x, -2, z), new Pair[Block, Integer](Blocks.sticky_piston, 1))
        }
      }

      returnMap.put(new Vector3(0, -3, 0), new Pair[Block, Integer](ElectricalContent.blockSiren, 0))
      returnMap.put(new Vector3(0, -2, 0), new Pair[Block, Integer](Blocks.redstone_wire, 0))
      returnMap.put(new Vector3, new Pair[Block, Integer](QuantumContent.blockReactorCell, 0))
    }
    else
    {
      val r: Int = 2

      for (y <- 0 to size; x <- -r to r; z <- -r to r)
      {
        val targetPosition: Vector3 = new Vector3(x, y, z)
        val leveledPosition: Vector3 = new Vector3(0, y, 0)
        if (y < size - 1)
        {
          if (targetPosition.distance(leveledPosition) == 2)
          {
            returnMap.put(targetPosition, new Pair[Block, Integer](QuantumContent.blockControlRod, 0))
            var rotationMetadata: Int = 0
            val offset: Vector3 = new Vector3(x, 0, z).normalize
            for (checkDir <- ForgeDirection.VALID_DIRECTIONS)
            {
              if (offset.x == checkDir.offsetX && offset.y == checkDir.offsetY && offset.z == checkDir.offsetZ)
              {
                rotationMetadata = checkDir.getOpposite.ordinal
              }
            }
            returnMap.put(targetPosition + offset, new Pair[Block, Integer](Blocks.sticky_piston, rotationMetadata))
          }
          else if (x == -r || x == r || z == -r || z == r)
          {
            returnMap.put(targetPosition, new Pair[Block, Integer](Blocks.glass, 0))
          }
          else if (x == 0 && z == 0)
          {
            returnMap.put(targetPosition, new Pair[Block, Integer](QuantumContent.blockReactorCell, 0))
          }
          else
          {
            returnMap.put(targetPosition, new Pair[Block, Integer](Blocks.water, 0))
          }
        }
        else if (targetPosition.distance(leveledPosition) < 2)
        {
          returnMap.put(targetPosition, new Pair[Block, Integer](Block.getBlockFromName("electricTurbine"), 0))
        }
      }
    }
    return returnMap
  }
} 
Example 16
Source File: TSDBUpdater.scala    From sprue   with Apache License 2.0 5 votes vote down vote up
package com.cloudera.sprue

import java.io._
import org.apache.commons._
import org.apache.http._
import org.apache.http.client._
import org.apache.http.client.methods.HttpPost
import java.util.ArrayList
import org.apache.http.client.entity.UrlEncodedFormEntity
import com.google.gson.Gson
import java.util.HashMap
import java.lang.reflect.Type
import com.google.gson.reflect.TypeToken
import org.apache.http.entity.StringEntity
import org.apache.http.impl.client.DefaultHttpClient
import org.apache.spark.sql.Row

 
case class MetricsTags(state: String)
case class OpenTSDBMessageElement(metric: String, timestamp: Long, value: Long, tags: MetricsTags)

object TSDBUpdater {
    val client = new DefaultHttpClient()
    // val client = HttpClientBuilder.create.build
}


class TSDBUpdater (url : String) extends Serializable {
       
  def loadPatientStats (row : Row) {
      
     val metricList = new ArrayList[OpenTSDBMessageElement]()
     val jmap = new MetricsTags(row.getString(0))
     val evalTimestamp = row.getLong(1)
     
     val sirsMetric = new OpenTSDBMessageElement("sirs", evalTimestamp, row.getLong(2), jmap)
     metricList.add(sirsMetric)
     
     val sepsisMetric = new OpenTSDBMessageElement("sepsis", evalTimestamp, row.getLong(3), jmap)
     metricList.add(sepsisMetric)
     
     val severeSepsisMetric = new OpenTSDBMessageElement("severeSepsis", evalTimestamp, row.getLong(4), jmap)
     metricList.add(severeSepsisMetric)

    val septicShockMetric = new OpenTSDBMessageElement("septicShock", evalTimestamp, row.getLong(5), jmap)
     metricList.add(septicShockMetric)
     
    val organMetric = new OpenTSDBMessageElement("organDysfunctionSyndrome", evalTimestamp, row.getLong(6), jmap)
     metricList.add(organMetric)

    val metricsAsJson = new Gson().toJson(metricList)
      
    val post = new HttpPost(url)
    
    post.setHeader("Content-type", "application/json");
    post.setEntity(new StringEntity(metricsAsJson));

    val response = TSDBUpdater.client.execute(post) 
  //  println("response =====" + response.toString())
  }
} 
Example 17
Source File: DictEncodingEncoders.scala    From filo   with Apache License 2.0 5 votes vote down vote up
package org.velvia.filo.codecs

import com.google.flatbuffers.FlatBufferBuilder
import java.nio.ByteBuffer
import java.util.HashMap
import scala.collection.mutable.{ArrayBuffer, BitSet}
import scala.language.postfixOps
import scalaxy.loops._

import org.velvia.filo._
import org.velvia.filo.vector._


object DictEncodingEncoders extends ThreadLocalBuffers {
  import Utils._

  var count = 0

  // Note: This is a way to avoid storing null and dealing with NPEs for NA values
  val NaString = ""

  def toStringVector(data: Seq[String], naMask: BitSet, stringSet: collection.Set[String]): ByteBuffer = {
    import DictStringVector._

    count += 1
    val builder = AutoIntegralDVBuilders.IntDataVectBuilder

    // Convert the set of strings to an encoding
    val uniques = stringSet.toSeq
    // NOTE: sorry but java's HashMap is just much faster (for the next step)
    // This used to be `uniques.zipWithIndex.toMap`
    val strToCode = new HashMap[String, Int]()
    for { i <- 0 until uniques.length optimized } {
      strToCode.put(uniques(i), i)
    }

    // Encode each string to the code per the map above
    // Again we could have used data.zipWithIndex.map(....) but this is much faster.
    val codes = ArrayBuffer.fill(data.length)(0)
    for { i <- 0 until data.length optimized } {
      if (!naMask(i)) codes(i) = strToCode.get(data(i)) + 1
    }

    val fbb = new FlatBufferBuilder(getBuffer)
    val ((dataOffset, nbits), signed) = builder.build(fbb, codes, 0, stringSet.size + 1)
    val dictVect = stringVect(fbb, Seq(NaString) ++ uniques)
    startDictStringVector(fbb)
    addDictionary(fbb, dictVect)
    addLen(fbb, data.length)
    addCodes(fbb, dataOffset)
    addInfo(fbb, DataInfo.createDataInfo(fbb, nbits, signed))
    finishDictStringVectorBuffer(fbb, endDictStringVector(fbb))
    putHeaderAndGet(fbb, WireFormat.VECTORTYPE_DICT, WireFormat.SUBTYPE_STRING)
  }
} 
Example 18
Source File: Schema.scala    From circe-json-schema   with Apache License 2.0 5 votes vote down vote up
package io.circe.schema

import cats.data.{ Validated, ValidatedNel }
import io.circe.{ Json, JsonNumber, JsonObject }
import java.util.HashMap
import org.everit.json.schema.{ Schema => EveritSchema, ValidationException }
import org.everit.json.schema.loader.SchemaLoader
import org.json.{ JSONArray, JSONObject, JSONTokener }
import scala.util.Try

trait Schema {
  def validate(value: Json): ValidatedNel[ValidationError, Unit]
}

object Schema {
  def load(value: Json): Schema = new EveritSchemaImpl(
    SchemaLoader.builder().schemaJson(fromCirce(value)).draftV7Support().build().load().build()
  )

  def loadFromString(value: String): Try[Schema] = Try(
    new EveritSchemaImpl(
      SchemaLoader.builder().schemaJson(new JSONTokener(value).nextValue).draftV7Support().build().load().build()
    )
  )

  private[this] class EveritSchemaImpl(schema: EveritSchema) extends Schema {
    def validate(value: Json): ValidatedNel[ValidationError, Unit] =
      try {
        schema.validate(fromCirce(value))
        Validated.valid(())
      } catch {
        case e: ValidationException => Validated.invalid(ValidationError.fromEverit(e))
      }
  }

  private[this] val fromCirceVisitor: Json.Folder[Object] = new Json.Folder[Object] {
    def onNull: Object = JSONObject.NULL
    def onBoolean(value: Boolean): Object = Predef.boolean2Boolean(value)
    def onString(value: String): Object = value
    def onNumber(value: JsonNumber): Object =
      value.toInt match {
        case Some(asInt) => Predef.int2Integer(asInt)
        case None        => new JSONTokener(value.toString).nextValue
      }
    def onArray(value: Vector[Json]): Object = new JSONArray(value.map(_.foldWith(this)).toArray)
    def onObject(value: JsonObject): Object = {
      val map = new HashMap[String, Object](value.size)
      val iter = value.toIterable.iterator

      while (iter.hasNext) {
        val (k, v) = iter.next
        map.put(k, v.foldWith(this))
      }
      new JSONObject(map)
    }
  }

  private[this] def fromCirce(value: Json): Object = value.foldWith(fromCirceVisitor)
} 
Example 19
Source File: AllowRule.scala    From Hive-JDBC-Proxy   with Apache License 2.0 5 votes vote down vote up
package com.enjoyyin.hive.proxy.jdbc.rule

import com.enjoyyin.hive.proxy.jdbc.thrift.ProxySession
import com.enjoyyin.hive.proxy.jdbc.domain.User
import com.enjoyyin.hive.proxy.jdbc.thrift.EventInfo
import com.enjoyyin.hive.proxy.jdbc.domain.UserHQL
import com.enjoyyin.hive.proxy.jdbc.domain.ThriftServerName
import com.enjoyyin.hive.proxy.jdbc.util.ProxyConf
import java.util.HashMap
import scala.collection.JavaConversions._
import scala.collection.mutable.HashSet
import com.enjoyyin.hive.proxy.jdbc.rule.basic.DefaultThriftServerNameRule
import com.enjoyyin.hive.proxy.jdbc.util.Logging
import com.enjoyyin.hive.proxy.jdbc.domain.HQLPriority
import com.enjoyyin.hive.proxy.jdbc.rule.basic.BalancerInfo



  override def dealOrNot(params: Map[String, String]): ThriftServerName
  
  def canDeal(params: Map[String, String]): Boolean
}

object ThriftServerNameRule extends Logging{
  val THRIFT_CONNECTION_NAME = ProxyConf.THRIFT_CONNECTION_NAME
  val USERNAME_NAME = "username"
  val IPADDRESS_NAME = "ipAddress"
  
  type JMap[K, V] = java.util.Map[K, V]
  
	private val registeredRules: HashSet[ThriftServerNameRule] = HashSet[ThriftServerNameRule]()
  
  private def toParamsMap(conf: JMap[String, String], username: String, ipAddress: String): Map[String, String] = {
    var params = conf
    if(conf == null) {
      params = new HashMap[String, String]
    }
    params += USERNAME_NAME -> username
    params += IPADDRESS_NAME -> ipAddress
    params.toMap
  }
  
  private def register(ruleName: String): Unit = {
    val ruleClass = Class.forName(ruleName).newInstance.asInstanceOf[ThriftServerNameRule]
    registeredRules.synchronized(registeredRules += ruleClass)
    logInfo("Registered a thrift-server-name-rule " + ruleName)
  }
  
  def register(ruleNames: Array[String]): Unit = {
    if(ruleNames.isEmpty) return
    registeredRules.synchronized {
      registeredRules.clear
      ruleNames.foreach(register)
    }
  }
  
  def getThriftServerName(conf: JMap[String, String], username: String, ipAddress: String): ThriftServerName = {
    val params = toParamsMap(conf, username, ipAddress)
    var rule = registeredRules.synchronized(registeredRules.find(_.canDeal(params)))
    if(rule.isEmpty) {
      rule = Some(DefaultThriftServerNameRule)
    }
    rule.get.dealOrNot(params)
  }
} 
Example 20
Source File: MemoryContextStore.scala    From dbpedia-spotlight-model   with Apache License 2.0 5 votes vote down vote up
package org.dbpedia.spotlight.db.memory

import java.util.{HashMap, Map}

import com.esotericsoftware.kryo.io.{Input, Output}
import com.esotericsoftware.kryo.{Kryo, KryoException, KryoSerializable}
import org.apache.commons.lang.NotImplementedException
import org.dbpedia.spotlight.db.model.{ContextStore, TokenTypeStore}
import org.dbpedia.spotlight.model.{DBpediaResource, TokenType}



  def calculateTotalTokenCounts(){
    var i = 0
    while(i < counts.size){

      if (counts(i).isInstanceOf[Array[Short]]){
        var j = 0

        while(j < counts(i).size ){
          totalTokenCounts(i) += qc(counts(i)(j))
          j += 1
        }

      }
      i += 1
    }
  }


  def read(kryo: Kryo, input: Input) {
    val size = input.readInt()

    tokens = new Array[Array[Int]](size)
    counts = new Array[Array[Short]](size)
    totalTokenCounts = new Array[Int](size)

    var i = 0
    var j = 0

    while(i < size) {
      val subsize = input.readInt()

      if (subsize > 0) {
        tokens(i) = new Array[Int](subsize)
        counts(i) = new Array[Short](subsize)

        j = 0
        while(j < subsize) {
          tokens(i)(j) = input.readInt()
          j += 1
        }

        j = 0
        while(j < subsize) {
          counts(i)(j) = input.readShort()
          j += 1
        }
     }

     i += 1
   }

   if(input.readChar() != '#')
     throw new KryoException("Error in deserializing context store...")

  }

} 
Example 21
Source File: DefaultWriteDataMapper.scala    From spark-riak-connector   with Apache License 2.0 5 votes vote down vote up
package com.basho.riak.spark.writer.mapper

import com.basho.riak.spark._
import com.basho.riak.spark.rdd.BucketDef
import com.basho.riak.spark.writer.{ WriteDataMapper, WriteDataMapperFactory }
import java.util.HashMap

class DefaultWriteDataMapper[T](bucketDef: BucketDef) extends WriteDataMapper[T, KeyValue] {
  override def mapValue(value: T): KeyValue = {
    // scalastyle:off null
    value match {
      // HashMap and Array are used for processing objects comming from python
      // TODO: Move to specific data mappers like in com.basho.riak.spark.writer.mapper.TupleWriteDataMapper
      case m: HashMap[_, _] => {
        if (m.size == 1) {
          val entry = m.entrySet().iterator().next()
          (entry.getKey.toString() -> entry.getValue)
        } else {
          (null, m)
        }
      }
      case a: Array[_] => {
        if (a.size == 1) {
          (null, a.head)
        } else {
          (a.head.toString(), a.tail)
        }
      }
      case _ => (null, value)
    }
    // scalastyle:on null
  }
}

object DefaultWriteDataMapper {
  def factory[T]: WriteDataMapperFactory[T, KeyValue] = new WriteDataMapperFactory[T, KeyValue] {
    override def dataMapper(bucketDef: BucketDef) = {
      new DefaultWriteDataMapper[T](bucketDef)
    }
  }
} 
Example 22
Source File: ParserGff3Data.scala    From piflow   with BSD 2-Clause "Simplified" License 5 votes vote down vote up
package cn.piflow.bundle.microorganism.util

import java.util.HashMap

import org.json.JSONObject

class ParserGff3Data {

  def parserAttributes(eachFileStr: String): HashMap[String, String] = {
    val map: HashMap[String, String] = new HashMap[String,String]()
    val eachArr = eachFileStr.split(";")
    for(each <- eachArr){
      try{
        val k: String = each.split("=")(0)
        val v: String = each.split("=")(1)
        map.put(k,v)
      }catch {
        case e : Exception => throw new Exception("File format error")
      }
    }
    map
  }

  def parserGff3(eachLine: String): JSONObject = {
    var doc: JSONObject =new JSONObject()

      val eachArr: Array[String] = eachLine.split("\u0009")
      if(eachArr.size ==9){
        for(x <- (0 until  9)){
          val eachFileStr = eachArr(x)
          if(x == 0){
            doc.put("reference_sequence",eachFileStr)
          }else if(x == 1){
            doc.put("source ",eachFileStr)
          }else if(x == 2){
            doc.put("type",eachFileStr)
          }else if(x == 3){
            doc.put("start_position",eachFileStr)
          }else if(x == 4){
            doc.put("end_position",eachFileStr)
          }else if(x == 5){
            doc.put("score",eachFileStr)
          }else if(x == 6){
            doc.put("strand",eachFileStr)
          }else if(x == 7){
            doc.put("phase",eachFileStr)
          }else if(x == 8){
            var map:HashMap[String, String]=parserAttributes(eachFileStr)
            doc.put("attributes",map)
          }
        }
      }
    return doc
  }
}