Skip to content

Instantly share code, notes, and snippets.

View karthik20522's full-sized avatar
๐Ÿ‘Š

Karthik Srinivasan karthik20522

๐Ÿ‘Š
View GitHub Profile
@karthik20522
karthik20522 / reindex_elasticsearch.scala
Last active May 25, 2019 03:23
ReIndexing Elasticsearch in Scala
import org.json4s._
import org.json4s.JsonDSL._
import spray.client.pipelining._
import spray.http._
import spray.httpx._
import spray.httpx.encoding._
import scala.concurrent._
import scala.concurrent.duration._
import akka.actor._
import scala.collection.mutable.ListBuffer
val esReindexActor = system.actorOf(Props(new ElasticsearchReIndexerActor(
"localhost:9200",
"localhost:9200",
"inputIndex",
"outputIndex",
"someType",
doNothing)), name = "esReIndexer")
esReindexActor ! "init"
@karthik20522
karthik20522 / soap.scala
Created May 25, 2019 03:45
Calling SOAP Service in Scala
import spray.client.pipelining._
import akka.actor.{ ActorRefFactory }
import spray.http._
import spray.httpx._
import scala.concurrent.Future
import scala.xml._
class KeywordService(keywordServiceURL: String, implicit val actorRefFactory: ActorRefFactory) {
import actorRefFactory.dispatcher
case class Terms(
termIds: List[Int],
status: Int = 0)
case class DesiredTermDetails(
ancestors: Boolean = false,
category: Boolean = false,
children: Boolean = false,
translations: Boolean = true,
mappingSynonyms: Boolean = false,
import scala.util.parsing.combinator._
import scala.util.parsing.combinator.syntactical._
case class Select(val fields: String*)
case class From(val table: String)
def selectAll: Parser[Select] = "select" ~ "*" ^^^ (Select("*")) //output: Select("*")
def from: Parser[From] = "from" ~> ident ^^ (From(_)) //output: From("users")
@karthik20522
karthik20522 / ScalaParserCombinators.scala
Created May 25, 2019 06:45
select name,age from users
def select: Parser[Select] = "select" ~ repsep(ident, ",") ^^ {
case "select" ~ f => Select(f: _*)
}
//output: Select(List[String]("name", "age"))
@karthik20522
karthik20522 / SCp.scala
Created May 25, 2019 06:46
select count(name) from users
case class Count(val field: String)
def count: Parser[Count] = "select" ~ "count" ~> "(" ~> ident <~ ")" ^^ {
case exp => Count(exp)
}
//output: Count("name")
@karthik20522
karthik20522 / scp2.scala
Created May 25, 2019 06:47
select * from users order by age desc
abstract class Direction
case class Asc(field: String*) extends Direction
case class Desc(field: String*) extends Direction
def order: Parser[Direction] = {
"order" ~> "by" ~> ident ~ ("asc" | "desc") ^^ {
case f ~ "asc" => Asc(f)
case f ~ "desc" => Desc(f)
}
}
@karthik20522
karthik20522 / scp3.scala
Created May 25, 2019 06:48
select * from users order by name, age desc
abstract class Direction
case class Asc(field: String*) extends Direction
case class Desc(field: String*) extends Direction
def order: Parser[Direction] = {
("order" ~> "by" ~> ident ~ ("asc" | "desc") ^^ {
case f ~ "asc" => Asc(f)
case f ~ "desc" => Desc(f)
}) | ("order" ~> "by" ~> repsep(ident, ",") ~ ("asc" | "desc") ^^ {
case f ~ "asc" => Asc(f: _*)
@karthik20522
karthik20522 / scp4.scala
Created May 25, 2019 06:49
select age from users where age>30
def where: Parser[Where] = "where" ~> rep(predicate) ^^ (Where(_: _*))
def predicate = (
ident ~ "=" ~ wholeNumber ^^ { case f ~ "=" ~ i => NumberEquals(f, i.toInt) }
| ident ~ "<" ~ wholeNumber ^^ { case f ~ "<" ~ i => LessThan(f, i.toInt) }
| ident ~ ">" ~ wholeNumber ^^ { case f ~ ">" ~ i => GreaterThan(f, i.toInt) })
//output: GreaterThan("age", 30)