padTo 예제이다. 컬렉션의 길이만큼 없는 데이터는 디폴트 값으로 채운다. 


List("a", "b", "c").padTo(5, "-")

res0: List[String] = List(a, b, c, -, -)




리스트의 특정 데이터의 개수를 꼭 채워야 하는 경우가 있다. 로그 저장시 데이터가 없다고 해서 그냥 두기 보다 디폴트 값같은 개념을 둔다고 생각한다.


padTo를 적용한 Log case 클래스 예제이다. 




scala> case class Log(tokens: List[String]) {

     | val z1 = tokens(0)

     | val z2 = tokens(1)

     | val z3 = tokens(2)

     | val z4 = tokens(3)

     | val z5 = tokens(4)

     | }

defined class Log



scala> val l = new Log(List("a", "b", "c").padTo(5, "-"))

l: Log = Log(List(a, b, c, -, -))


scala> l.z1

res4: String = a


scala> l.z2

res5: String = b


scala> l.z3

res6: String = c


scala> l.z4

res7: String = -


scala> l.z5

res8: String = -



Posted by '김용환'
,

[scala] mkString 예제

scala 2017. 3. 13. 19:34


필드 출력을 위해  간단히 \t 단위로 하고 싶을 때 문자열에 \t를 줄 수 있다. 


scala> s"aa\tbb\tcc"

res34: String = aa bb cc



\t를 너무 많이 입력하는 것이 귀찮을 때 mkString을 사용할 수 있다. 



scala> List("aa", "bb", "cc").mkString("\t")

res32: String = aa bb cc



Posted by '김용환'
,


scala/특히 spark에서 작업하다가 혼동되는 게 flatmap이다.


먼저 map을 살펴보면 컬렉션이 Opion 타입이라면 List(Option) 작업을 강제로 진행해야 한다. 


scala> List(Some(1),Some(2),None).map{case Some(x) => x*2  case None => 0 }

res21: List[Int] = List(2, 4, 0)




컬렉션 flatMap을 사용하기만 하면 Option을 모두 정리한다.



scala> List(Some(1),Some(2),None).flatMap(x => x)

res27: List[Int] = List(1, 2)




Posted by '김용환'
,



forall은 술어함수 p에 대해서 전체가 참이면 true를 리턴한다. 아니면 false를 리턴한다.


scala> List(1,2,3,4,5).forall(x => x>2)

res11: Boolean = false



비슷함 함수로는 exists로서 술어라면 하나라도 참이면 true를 리턴하고 아니면 false를 리턴한다.



scala> List(1,2,3,4,5).exists(x => x>2)

res12: Boolean = true




Posted by '김용환'
,




# linkedin

linked 기술 블로그에 따르면 kafka를 중앙 pub/sub 구조의 큐로 잘 사용해 피드 시스템을 처리하고 있다. 재미있는 것은 avro도 사용하고 있다는 점이다. 


https://engineering.linkedin.com/blog/2016/04/kafka-ecosystem-at-linkedin


이미지 : https://content.linkedin.com/content/dam/engineering/site-assets/images/blog/posts/2016/04/KafkaEcosystem1.jpg




#uber


카프카 데이터 피드를 사용해 분당 수백 번의 승차 정보를 로그 데이터로 저장한 후, 해당 데이터를 Amazon S3에 대량 로드한다. 로컬 데이터 센터의 변경 데이터 로그를 스트리밍한다. json 데이터를 수집한 후 spark-hadoop(paquet)를  사용한다.


https://www.datanami.com/2015/10/05/how-uber-uses-spark-and-hadoop-to-optimize-customer-experience/









#  twitter


하루 50억 세션을 실시간으로 처리하려면 카프카를 스트림 처리 인프라로 사용해야 한다.


https://blog.twitter.com/2015/handling-five-billion-sessions-a-day-in-real-time






# netflix


카프카는 실시간 모니터링 및 이벤트 처리를 위한 넷플릭스의 데이터 파이프 라인의 백본이다.


http://techblog.netflix.com/2013/12/announcing-suro-backbone-of-netflixs.html



# spotify


https://www.meetup.com/ko-KR/stockholm-hug/events/121628932/?eventId=121628932



# yahoo


https://yahooeng.tumblr.com/post/109994930921/kafka-yahoo

Posted by '김용환'
,



특정 로그 파일(birthday.log)을 logstash를 통해 kafka로 보내려고 한다.


이미 logstash, kafka가 설치되어 있다고 가정한다.





# logstash 설정


input {
file {
type => "birthday_log"
path => [
"/home/www/googleplus/birthday.log"
]
}
}

output {
kafka {
codec => plain { format => "%{host} %{message}" }
topic_id => "%{type}"
request_required_acks => 1
broker_list => "server-ip:9092"
}
}



# kafka에서 보기


$ /usr/local/kafka/bin/kafka-console-consumer.sh --topic birthday_log --zookeeper zk-server-ip:2181


Posted by '김용환'
,



Akka에서 ActorSystem을 정의할 때, 영어 대소문자, 숫자와 -,_만 된다.(-와 _는 앞에 있으면 안된다)



Exception 나는 경우

val system = ActorSystem("MonitoringTest ")
val system = ActorSystem("-MonitoringTest-")


Exception 내용이다. 


java.lang.IllegalArgumentException: invalid ActorSystem name [MonitoringTest ], must contain only word characters (i.e. [a-zA-Z0-9] plus non-leading '-' or '_')




문제가 발생하지 않는 경우의 예

val system = ActorSystem("MonitoringTest")


val system = ActorSystem("MonitoringTest1-")



Posted by '김용환'
,