scala

Turning Map(“a” -> 2, “b” -> 1) into seq(“a”,“a”,“b”) using map

拟墨画扇 提交于 2021-01-20 09:43:01
问题 I am trying to turn a Map("a" -> 2, "b" -> 1) into seq("a","a","b") through the map function, Currently I am trying to run the code below giving me the desired result. Is there a smarter way to do this? Possibly a better way through the map function? var multiset : Seq[T] = Seq[T]() var variables : Seq[T] = data.map(x => x._1).toSeq var variableCounts : Seq[Int] = data.map(x => x._2).toSeq for(x <- 0 until variables.length){ for(y <- 0 until variableCounts(x)) multiset = multiset :+ variables

Turning Map(“a” -> 2, “b” -> 1) into seq(“a”,“a”,“b”) using map

这一生的挚爱 提交于 2021-01-20 09:42:11
问题 I am trying to turn a Map("a" -> 2, "b" -> 1) into seq("a","a","b") through the map function, Currently I am trying to run the code below giving me the desired result. Is there a smarter way to do this? Possibly a better way through the map function? var multiset : Seq[T] = Seq[T]() var variables : Seq[T] = data.map(x => x._1).toSeq var variableCounts : Seq[Int] = data.map(x => x._2).toSeq for(x <- 0 until variables.length){ for(y <- 0 until variableCounts(x)) multiset = multiset :+ variables

Turning Map(“a” -> 2, “b” -> 1) into seq(“a”,“a”,“b”) using map

人走茶凉 提交于 2021-01-20 09:41:10
问题 I am trying to turn a Map("a" -> 2, "b" -> 1) into seq("a","a","b") through the map function, Currently I am trying to run the code below giving me the desired result. Is there a smarter way to do this? Possibly a better way through the map function? var multiset : Seq[T] = Seq[T]() var variables : Seq[T] = data.map(x => x._1).toSeq var variableCounts : Seq[Int] = data.map(x => x._2).toSeq for(x <- 0 until variables.length){ for(y <- 0 until variableCounts(x)) multiset = multiset :+ variables

how to deserialize a json string that contains @@ with scala'

一曲冷凌霜 提交于 2021-01-20 08:59:37
问题 As the title already explains, I would like to deserialize a json string that contains a key that starts with @@. With the @@ my standard approach using case classes sadly does not work anymore. val test = """{"@@key": "value"}""" case class Test(@@key: String) // not possible val gson = new GsonBuilder().create() val res = gson.fromJson(test, classOf[Test]) How can work with the @@ withtout preprocessing the input json string? 回答1: The simplest answer is to quote the field name: case class

How do I link variables inside another object in scaladocs?

倾然丶 夕夏残阳落幕 提交于 2021-01-20 04:28:05
问题 To link another class, I can use [[package.Classname]] . Linking functions defined by def works as well, but trying to link variables doesn't work. What I've tried: object Foo { val BAR = 0 } object Example { /** * Does the thing with [[Foo.BAR]] */ def doTheThing(): Unit = { } } I've also tried [[Foo#BAR]] (from another post) instead of [[Foo.BAR]] , which fails as well. What's the proper way to link variables in scaladoc? 回答1: The right way to go is what you have already tried: /** * Does

Kafka底层原理剖析(近万字建议收藏)

走远了吗. 提交于 2021-01-18 23:32:07
Kafka 简介 Apache Kafka 是一个分布式发布-订阅消息系统。是大数据领域消息队列中唯一的王者。最初由 linkedin 公司使用 scala 语言开发,在2010年贡献给了Apache基金会并成为顶级开源项目。至今已有十余年,仍然是大数据领域不可或缺的并且是越来越重要的一个组件。 Kafka 适合离线和在线消息,消息保留在磁盘上,并在集群内复制以防止数据丢失。kafka构建在zookeeper同步服务之上。它与 Flink 和 Spark 有非常好的集成,应用于实时流式数据分析。 Kafka特点: 可靠性:具有副本及容错机制。 可扩展性:kafka无需停机即可扩展节点及节点上线。 持久性:数据存储到磁盘上,持久性保存。 性能:kafka具有高吞吐量。达到TB级的数据,也有非常稳定的性能。 速度快:顺序写入和零拷贝技术使得kafka延迟控制在毫秒级。 Kafka 底层原理 先看下 Kafka 系统的架构 Kafka 架构 kafka支持消息持久化,消费端是主动拉取数据,消费状态和订阅关系由客户端负责维护, 消息消费完后,不会立即删除,会保留历史消息 。因此支持多订阅时,消息只会存储一份就可以。 broker :kafka集群中包含一个或者多个服务实例(节点),这种服务实例被称为broker(一个broker就是一个节点/一个服务器); topic

Java 中的不可变数据结构

拟墨画扇 提交于 2021-01-15 13:36:57
最近,在我主导的几场代码面试中,经常出现不可变数据结构(Immutable Data Structure)相关内容。关于这个主题我个人并不过分教条,不变性通常体现在数据结构中," 除非必要 "否则不会要求代码一定具备不变性。然而,我发现大家对不变性(Immutability)这个概念似乎有一些误解。开发者通常认为加上 `final`,或者在 Kotlin、Scala 中加上 `val` 就足以实现不可变对象。这篇文章会深入讨论不可变引用和不可变数据结构。 1. 不可变数据结构的优点 不可变数据结构有下列显著优点: 没有无效状态(Invalid State) 线程安全 代码易于理解 易于测试 可用作值类型 译注:在计算机编程中包含两种类型,值类型 value type 与引用类型 reference type。值类型表示实际值,引用类型表示对其他值或对象的引用。 2. 没有无效状态 不可变对象只能通过构造函数初始化,并且通过参数限制了输入的有效性,从而确保对象不会包含无效值。例如下面这段代码示例: ```java Address address = new Address(); address.setCity( "Sydney" ); // 由于没有设置 country,address 现在处于无效状态. Address address = new Address( "Sydney"

How to eval code that uses InterfaceStability annotation (that fails with “illegal cyclic reference involving class InterfaceStability”)?

筅森魡賤 提交于 2021-01-15 06:40:47
问题 I want to dynamically generate some kafka stream code when the program is running, but I get an exception when I compile the following code for kafka stream with scala toolbox eval: val toolbox = runtimeMirror(getClass.getClassLoader).mkToolBox() val code = """ |import org.apache.kafka.streams.scala.kstream.KStream |import org.apache.kafka.streams.scala.StreamsBuilder | |class ClassA { | def createStream(): KStream[String, String] = { | import org.apache.kafka.streams.scala

How to eval code that uses InterfaceStability annotation (that fails with “illegal cyclic reference involving class InterfaceStability”)?

一笑奈何 提交于 2021-01-15 06:37:43
问题 I want to dynamically generate some kafka stream code when the program is running, but I get an exception when I compile the following code for kafka stream with scala toolbox eval: val toolbox = runtimeMirror(getClass.getClassLoader).mkToolBox() val code = """ |import org.apache.kafka.streams.scala.kstream.KStream |import org.apache.kafka.streams.scala.StreamsBuilder | |class ClassA { | def createStream(): KStream[String, String] = { | import org.apache.kafka.streams.scala

Gatling初次体验

ぐ巨炮叔叔 提交于 2021-01-14 04:00:41
主要步骤: 1. 利用springboot编写了一个简单的服务jdktest 2.将jdktest利用docker在虚拟机中启动 3.创建一个scala工程,利用gatling提供的DSL编写性能脚本 4.执行并查看报告 1.编写jdktest服务 接口名称:/common/check 1 参数:一个User对象 2 3 格式: json 4 5 响应: 7 年龄小于等于30,结果:{"code":200,"msg":"success","data":{"name":"hello","age":18 }} 8 年龄大于30,结果:{"code":400,"msg":"年龄大于30","data":{"name":"hello","age":50}} 2.部署服务 将jdktest打成jar包,并上传到服务器(我这里是虚拟机,并且已经安装了docker)上,在jar同级目录下创建Dockerfile FROM primetoninc/jdk:1.8 MAINTAINER 3404924705 @qq.com ADD jdktest -0.0.2-SNAPSHOT.jar /usr/local/jdktest/ RUN mkdir /usr/local/jdktest/ log RUN chmod -R 755 /usr/local/ jdktest WORKDIR /usr