Skip to content

Instantly share code, notes, and snippets.

View satendrakumar's full-sized avatar
:octocat:

Satendra Kumar satendrakumar

:octocat:
View GitHub Profile
@satendrakumar
satendrakumar / JavaNullWithScala.scala
Last active April 15, 2017 04:32
Handling Java null with Scala
object play {
println("Welcome to the Scala worksheet")
case class User(id:Int, name:String)
def getUser:User = null
def processUser(u:User):String= u.name
Option(getUser)
Option(getUser).fold("Sky")(u=>processUser(u))
@satendrakumar
satendrakumar / kafka.md
Last active August 29, 2015 14:21 — forked from ashrithr/kafka.md

Introduction to Kafka

Kafka acts as a kind of write-ahead log (WAL) that records messages to a persistent store (disk) and allows subscribers to read and apply these changes to their own stores in a system appropriate time-frame.

Terminology:

  • Producers send messages to brokers
  • Consumers read messages from brokers
  • Messages are sent to a topic
package slicks.docs.dao
import scala.slick.driver.PostgresDriver.simple._
import scala.slick.driver._
trait Profile {
val profile: JdbcProfile
}
@satendrakumar
satendrakumar / kafka-consumer
Last active May 31, 2016 18:08
Read data from kafka cluster.
//Worker code
import org.apache.kafka.clients.consumer.ConsumerRecord;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;
public class Worker implements Callable<Boolean> {
ConsumerRecord record;
@satendrakumar
satendrakumar / GenericTypeConverter.scala
Created March 23, 2017 12:44
Generic Type Converter
object GenericTypeConverter extends App{
def convert[T: ClassTag](value:Any): Option[T] = {
val ct = implicitly[ClassTag[T]]
value match {
case ct(x) => Some(x)
case _ => None
}
}
Prelude> let forall :: (a -> Bool) -> [a] -> Bool ; forall f []= True; forall f (x:xs) = f x && forall f xs
Prelude> forall even [2,4,6]
True
Prelude> forall even [2,4,5]
False
Prelude> let forall :: (a -> Bool) -> [a] -> Bool ; forall f []= True; forall f (x:xs) = if (f x) then forall f xs else False
Prelude> forall even [2,4,5]
False
Prelude> forall even [2,4,6]
True
@satendrakumar
satendrakumar / JsonUtility.scala
Last active February 12, 2019 06:32
Scala Utility class for parsing and writing Json(Write once and use everywhere )
import org.json4s._
import org.json4s.native.{JsonMethods, Serialization}
object JsonUtility {
implicit val formats = DefaultFormats
def write[T <: AnyRef](value: T): String = Serialization.write(value)
@satendrakumar
satendrakumar / Logging.scala
Last active April 15, 2017 04:36
Cleaner way to use Logback logger
import org.slf4j.{Logger, LoggerFactory}
trait Logging {
protected val logger: Logger = LoggerFactory.getLogger(this.getClass())
protected def debug(message: String): Unit = logger.debug(message)
protected def debug(message: String, exception: Throwable): Unit = logger.debug(message, exception)
@satendrakumar
satendrakumar / CSVParser.scala
Last active May 13, 2017 11:12
Parse CSV using univocity parser
import com.univocity.parsers.csv.{CsvParser, CsvParserSettings}
class CSVParser(delimiter: Char = ',') {
private val parser = {
val settings = new CsvParserSettings
val format = settings.getFormat
format.setLineSeparator("\n")
format.setDelimiter(delimiter)
@satendrakumar
satendrakumar / logback.xml
Last active April 15, 2017 05:42
logback xml with rolling appender
<configuration>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%highlight([%level]) - [%thread] - [%date] - [%logger] %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<prudent>true</prudent>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>logs/etl-%d{yyyy-MM-dd}.log</fileNamePattern>