Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

scala - Spark sample is too slow

I'm trying to execute a simple random sample with Scala from an existing table, containing around 100e6 records.

import org.apache.spark.sql.SaveMode

val nSamples = 3e5.toInt
val frac = 1e-5
val table = spark.table("db_name.table_name").sample(false, frac).limit(nSamples)
(table
  .write
  .mode(SaveMode.Overwrite)
  .saveAsTable("db_name.new_name")
)

But it is taking too long (~5h by my estimates).

Useful information:

  1. I have ~6 workers. By analyzing the number of partitions of the table I get: 11433.

  2. I'm not sure if the partitions/workers ratio is reasonable.

  3. I'm running Spark 2.1.0 using Scala.

I have tried:

  1. Removing the .limit() part.

  2. Changing frac to 1.0, 0.1, etc.

Question: how can I make it faster?

Best,

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Limit is definitely worth removing, but the real problem is that sampling requires a full data scan. No matter how low is the fraction, the time complexity is still O(N)*.

If you don't require good statistical properties, you can try to limit amount of data you've loaded in the first place by sampling data files first, and then subsampling from the reduced dataset. This might work reasonably well, if data is uniformly distributed.

Otherwise there is not much you can do about it, other than scaling your cluster.


* How do simple random sampling and dataframe SAMPLE function work in Apache Spark (Scala)?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...