Skip to content

Instantly share code, notes, and snippets.

View MovileGente's full-sized avatar

Movile MovileGente

View GitHub Profile
dataset.show(10)
dataset = spark.read.format("json").option("multiLine",True).load("sample_data/anscombe.json")
dataset.columns
import findspark
findspark.init()
from pyspark.sql import SparkSession
spark = SparkSession.builder.master("local[*]").getOrCreate()
import os
os.environ["JAVA_HOME"] = "/usr/lib/jvm/java-8-openjdk-amd64"
os.environ["SPARK_HOME"] = "/content/spark-2.4.1-bin-hadoop2.7"
!apt-get install openjdk-8-jdk-headless -qq > /dev/null
!wget -q http://www-eu.apache.org/dist/spark/spark-2.4.1/spark-2.4.1-bin-hadoop2.7.tgz
!tar xf spark-2.4.1-bin-hadoop2.7.tgz
!pip install -q findspark
!pip install -q pyspark
suspend fun <T> CompletableFuture<T>.await(): T =
   suspendCoroutine<T> { cont: Continuation<T> ->
       whenComplete { result, exception ->
           if (exception == null) {
               cont.resume(result)
           } else {
               cont.resumeWithException(exception)
           }
       }
// define uma função para criar um future
// exemplo de uso:
// val stringFuture = future { “yay” }
// println(stringFuture.get())
fun <T> future(context: CoroutineContext = CommonPool, block: suspend () -> T): CompletableFuture<T> =
CompletableFutureCoroutine<T>(context).also {
block.startCoroutine(completion = it)
}
class CompletableFutureCoroutine<T>(override val context: CoroutineContext) : CompletableFuture<T>(), Continuation<T> {
sealed class ActorCommand<out T> {
   class REGISTER<T>(val id: Int, val name: String, val response: SendChannel<T>? = null) : ActorCommand<T>()
   class REMOVE<T>(val id: Int, val response: SendChannel<T>? = null) : ActorCommand<T>()
   class QUERY<T>(val response: SendChannel<T>) : ActorCommand<T>()
}
val actorJob = actor<ActorCommand<Any?>> {
   val state = mutableMapOf<Int, String>()
   for (msg in channel) {
       when (msg) {
fun sendMessageToUser(userId: Int, message: String, cont: Continuation) {
   val stateMachine = cont as? CoroutineImpl // verifica se já temos uma máquina de estados instanciada (ou seja, já passamos pelo menos uma vez por essa função)
                      ?: object : CoroutineImpl { // cria uma máquina de estados que chama a própria função
       fun resume(...) {
           sendMessageToUser(userId, message, this) // Hooray, Callback
       }
   }
   switch(stateMachine.label) {
   case 0: // Kotlin usa labels para saber em qual passo estamos
suspend fun queryUser(id: Int): User {
   delay(500) // simulando latência de rede ou outra operação lenta
   return User(id = id, name = "gente.firme", email = "talentos@movile.com", phone = 5511000000000L)
}
suspend fun sendEmail(destination: String, body: String): Boolean {
   println("Sending email to $destination with body: $body")
   delay(1000) // simulando um request/operação lenta
   val couldSentEmail = Random().nextBoolean()
   if (couldSentEmail) {