Skip to content

Instantly share code, notes, and snippets.

View YordanGeorgiev's full-sized avatar

Yordan Georgiev YordanGeorgiev

View GitHub Profile
@YordanGeorgiev
YordanGeorgiev / factory-design-pattern.scala
Created March 24, 2018 16:24
[factory-design-pattern-in-scala] how-to use factory design pattern in scala #scala #design-patterns #patterns #factory-design-pattern
// all the readers must extend this trait
trait Reader {
def read ( what: String ): DataFrame
}
object RdrFactory {
// could pass also a custom reader type here ...
def spownReader ( readerType: String , cnf: AppConfig) = {
readerType match {
case "db" => new RdrDb (cnf)
@YordanGeorgiev
YordanGeorgiev / register-spark-custom-udf-and-call-it.scala
Created March 24, 2018 16:09
[register-spark-custom-udf-and-call-it] how-to register custom udf and call it in spark with scala #scala #spark #udf
// register and udf to the spark session
spark.sqlContext.udf.register(
"getRandom64CharStr", () => scala.util.Random.nextString(64))
// or register and UDF accepting params
def getAboutTimeUDF = udf((ts: java.sql.Timestamp, timeFrame: String) => {
import java.util.Calendar
new java.sql.Timestamp(
timeFrame match {
@YordanGeorgiev
YordanGeorgiev / string-keys-to-types.scala
Created March 24, 2018 15:49
[keys-to-types-enumeration-model] how-to model string keys to types with Enumeration in Scala
object InternetProtocolType extends Enumeration {
type InternetProtocolType = Value
val TCP: Value = Value("TCP")
val UDP: Value = Value("UDP")
val SCTP: Value = Value("SCTP")
def getProtocolType(key: String): InternetProtocolType = {
@YordanGeorgiev
YordanGeorgiev / fall-trough-different-match-cases.scala
Created March 24, 2018 10:24
[fall trough different match cases ] how-to fall trough different match cases in scala #scala #match #case
val df: DataFrame = {
filterType match {
case FilterType.SqlLike => filterLikeSql(df)
case FilterType.XlsLike => filterLikeXls(df)
case _ => filterLikeSql(df)
}
}
@YordanGeorgiev
YordanGeorgiev / read-csv-in-spark.scala
Created March 24, 2018 10:12
[read-csv-in-spark] how-to read csv file in spark #spark #scala #file-io
val df = spark.read
.format("csv")
.option("header", "true")
.load(uri)
@YordanGeorgiev
YordanGeorgiev / how-to-print-env-vars-and-sys-props
Created March 22, 2018 13:00
[how-to print the env vars and sys props in scala] how-to print the environmental variables and System properties in scala #scala #env #sys #sysprops
import scala.collection.JavaConversions._
val environmentVars = System.getenv()
for ((k,v) <- environmentVars) println(s"key: $k, value: $v")
val properties = System.getProperties()
for ((k,v) <- properties) println(s"key: $k, value: $v")
@YordanGeorgiev
YordanGeorgiev / zeppelin-rest-api-one-liners
Last active June 8, 2018 10:07
[zeppelin-rest-api-one-liners] how-to run oneliners on zeppelin rest api #curl #one-liners #zeppelin #rest #rest-api
export zeppelin_root_uri='https://company.claud.zepl.com/'
# issue first the call to the login
curl -i --data 'userName=user&password=pwd' -X POST $zeppelin_root_uri/api/login
# check for the
# Set-Cookie: JSESSIONID=b1e15e00-4171-4079-a699-338bf619b0c4; Path=/; HttpOnly
# line in the response header
curl -i -b 'JSESSIONID=b1f15e00-4571-4079-a699-338bf619b0c4; Path=/; HttpOnly' "$zeppelin_root_uri"/api/notebook
@YordanGeorgiev
YordanGeorgiev / chk-that-str-is-a-num.scala
Created March 19, 2018 11:39
[check that string a number in scala] how-to check that a string is a number in scala #scala #utils #nums
import scala.util.Try
object NumCruncher {
def isShort(aString: String): Boolean = Try(aString.toLong).isSuccess
def isInt(aString: String): Boolean = Try(aString.toInt).isSuccess
def isLong(aString: String): Boolean = Try(aString.toLong).isSuccess
def isDouble(aString: String): Boolean = Try(aString.toDouble).isSuccess
def isFloat(aString: String): Boolean = Try(aString.toFloat).isSuccess
@YordanGeorgiev
YordanGeorgiev / find-files-with-ext.pl
Created March 4, 2018 10:34
[find files with extensions] how-to to find files under a dir by file ext regex #perl #files #dir
#
# -----------------------------------------------------------------------------
# read dir recursively , return only the files matching the regex for the
# file extension , example - get all the .pl or .pm files:
# my $arrRefTxtFiles = $objFH->doReadDirGetFilesByExtension ( $dir, 'pl|pm')
# -----------------------------------------------------------------------------
sub doReadDirGetFilesByExtension {
my $self = shift ; # remove this if you are not calling OO style
my $dir = shift ;
my $ext = shift ;
@YordanGeorgiev
YordanGeorgiev / get-current-windows-date-time.cmd
Created February 28, 2018 12:30
[get the current date time on Windows] how-to get the current datetime in iso-8601 format from cmd or Start - Run
:: Start - Run , type:
cmd /c "powershell get-date -format ^"{yyyy-MM-dd HH:mm:ss}^"|clip"
:: click into target media, Ctrl + V to paste the result