This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dataset.show(10) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dataset = spark.read.format("json").option("multiLine",True).load("sample_data/anscombe.json") | |
dataset.columns |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import findspark | |
findspark.init() | |
from pyspark.sql import SparkSession | |
spark = SparkSession.builder.master("local[*]").getOrCreate() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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> { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { |