它是一个线程安全机制吗?(Is it a thread-safe mechanism?)
这个类是线程安全的吗?
class Counter { private ConcurrentMap<String, AtomicLong> map = new ConcurrentHashMap<String, AtomicLong>(); public long add(String name) { if (this.map.get(name) == null) { this.map.putIfAbsent(name, new AtomicLong()); } return this.map.get(name).incrementAndGet(); } }
你怎么看?
Is this class thread-safe?
class Counter { private ConcurrentMap<String, AtomicLong> map = new ConcurrentHashMap<String, AtomicLong>(); public long add(String name) { if (this.map.get(name) == null) { this.map.putIfAbsent(name, new AtomicLong()); } return this.map.get(name).incrementAndGet(); } }
What do you think?
原文:https://stackoverflow.com/questions/10498303
最满意答案
我有类似的问题,我最终使用:
val df_list = ListBuffer[DataFrame]() df_list .toList.reduce((a, b) => a.join(b, a.col(a.schema.head.name) === b.col(b.schema.head.name), "left_outer"))
你可以在Sqoop上做一个免费的sql语句,并加入所有的东西。 或者使用Spark JDBC来完成相同的工作
I had similiar problem and I end up Using :
val df_list = ListBuffer[DataFrame]() df_list .toList.reduce((a, b) => a.join(b, a.col(a.schema.head.name) === b.col(b.schema.head.name), "left_outer"))
You could make a free sql statement on Sqoop and join everything there. Or Use Spark JDBC to do the same job
相关问答
更多-
Dataframe是Dataset的别名。 所以实际上你在代码中使用了一个数据框。 如果要保留具有相同名称的两列,则必须在使用“withColumnRenamed”选项执行连接之前重命名其中一列。 Dataframe is an alias for Dataset. So practically you are using a dataframe in your code. If you want to retain both the columns with the same name, then you ...
-
Json Data的Spark(Spark for Json Data)[2022-04-16]
您可以通过这种方式获取code和qtyAmount 。 import sqlcontext.implicits._ cdm.select( $"businessEntity.element.payGroup.element.reportingPeriod.worker.element.tax.element.code".as("code"), $"businessEntity.element.payGroup.element.reportingPeriod.wor ... -
IIUC你可以这样做: df = df.drop(df.columns.filter(_.startsWith("null"))) IIUC you can do it this way: df = df.drop(df.columns.filter(_.startsWith("null")))
-
第一步是在所有when子句中创建一个包含列名的元组列表。 它可以通过多种方式完成,但如果要使用数据框中的所有列,则可以按如下方式完成(使用示例数据帧): val df = Seq(("1", "2", null, "4", "5", "6"), (null, "2", "3", "4", null, "6"), ("1", "2", "3", "4", null, "6")) .toDF("col1_1", "col1", "col2_1", "col2", "col3_1", "c ...
-
我有类似的问题,我最终使用: val df_list = ListBuffer[DataFrame]() df_list .toList.reduce((a, b) => a.join(b, a.col(a.schema.head.name) === b.col(b.schema.head.name), "left_outer")) 你可以在Sqoop上做一个免费的sql语句,并加入所有的东西。 或者使用Spark JDBC来完成相同的工作 I had similiar problem and I end ...
-
Spark多个数据帧保存(Spark multiple dataframe saves)[2022-08-26]
你应该缓存: val final_df = df10.withColumn(...) val subset_df = final_df.drop(...) final_df.cache() 在第一次行动之前,否则它将执行两次(如你所怀疑)。 You should cache: val final_df = df10.withColumn(...) val subset_df = final_df.drop(...) final_df.cache() before first action other ... -
通过row.names合并会产生奇怪的结果 - 它会创建一个名为Row.names的列,这会使后续合并变得困难。 为了避免这个问题,您可以改为创建一个包含行名称的列(无论如何这通常都是一个更好的主意 - 行名称非常有限并且难以操作)。 用OP中给出的数据来做这件事的一种方法(不是最优方式,为了更好和更简单的方式处理矩形数据,我推荐了解data.table ): Reduce(merge, lapply(l, function(x) data.frame(x, rn = row.names(x)))) Me ...
-
范围连接作为交叉产品执行,并具有后续的过滤步骤。 一个潜在更好的解决方案可能是, 广播可能较小的events表,然后映射persons表:在地图内部,检查连接条件并生成相应的结果。 Range joins are performed as a crossproduct with a subsequent filter step. A potentially better solution could be, to broadcast the potentially smaller events table ...
-
如何使用Spark / Spark SQL将JSON blob压缩为数据框(How do I flatten JSON blobs into a Data Frame using Spark/Spark SQL)[2022-07-26]
不确定您所使用的Spark版本,但请查看此示例: Spark SQL JSON 所以如果你做的事情如下: import org.apache.spark.sql._ val rdd = df2.rdd.map { case Row(j: String) => j } spark.read.json(rdd).show() Spark SQL将做繁重的工作。 Not sure what version of Spark you are on, but have a look at this example: ... -
df.write .format("org.apache.spark.sql.cassandra") .options(Map( "table" -> "t_payu_df", "keyspace" -> "ks_payu")) .save() 这会奏效。 您还可以指定SaveMode(overwrite,append,ErrorIfExists)。 SaveMode示例: df.write .format("org.apache.spark.sql.cassandra") .mode ...