RDD分区逻辑

时间:2016-05-22 12:45:32

标签: apache-spark rdd

我正在尝试理解RDD分区逻辑。 RDD在节点之间进行分区,但希望了解此分区逻辑的工作原理。

我有分配了4个核心的VM。我创建了两个RDD,一个来自HDFS,另一个来自并行化操作。

enter image description here

第一次创建了两个分区,但在第二个操作中创建了4个分区。

我检查了没有分配给文件的块 - 它是1块,因为文件非常小但是当我在该文件上创建RDD时,它显示了两个分区。为什么是这样 ?我在某个地方看到分区也依赖于核心,在我的情况下仍然不能满足那个输出。

有人可以帮助理解这一点吗?

1 个答案:

答案 0 :(得分:2)

textFile的完整签名是:

textFile(path: String, minPartitions: Int = defaultMinPartitions): RDD[String]

使用第二个参数minPartitions,您可以设置要获取的最小分区数。如您所见,默认情况下,它设置为defaultMinPartitions,而def defaultMinPartitions: Int = math.min(defaultParallelism, 2) 又定义为:

defaultParalellism

spark.default.parallelism的值配置为min(4, 2)设置,默认情况下,该设置取决于在本地模式下运行Spark时的核心数。在你的情况下这是4,所以你得到 public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) { return findLargestRectangle(inputFrame.rgba()); } private Mat findLargestRectangle(Mat original_image) { Mat imgSource = original_image; hierarchy = new Mat(); //convert the image to black and white Imgproc.cvtColor(imgSource, imgSource, Imgproc.COLOR_BGR2GRAY); //convert the image to black and white does (8 bit) Imgproc.Canny(imgSource, imgSource, 50, 50); //apply gaussian blur to smoothen lines of dots Imgproc.GaussianBlur(imgSource, imgSource, new Size(5, 5), 5); //find the contours List<MatOfPoint> contours = new ArrayList<MatOfPoint>(); Imgproc.findContours(imgSource, contours, hierarchy, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE); hierarchy.release(); double maxArea = -1; int maxAreaIdx = -1; MatOfPoint temp_contour = contours.get(0); //the largest is at the index 0 for starting point MatOfPoint2f approxCurve = new MatOfPoint2f(); Mat largest_contour = contours.get(0); List<MatOfPoint> largest_contours = new ArrayList<MatOfPoint>(); for (int idx = 0; idx < contours.size(); idx++) { temp_contour = contours.get(idx); double contourarea = Imgproc.contourArea(temp_contour); //compare this contour to the previous largest contour found if (contourarea > maxArea) { //check if this contour is a square MatOfPoint2f new_mat = new MatOfPoint2f( temp_contour.toArray() ); int contourSize = (int)temp_contour.total(); Imgproc.approxPolyDP(new_mat, approxCurve, contourSize*0.05, true); if (approxCurve.total() == 4) { maxArea = contourarea; maxAreaIdx = idx; largest_contours.add(temp_contour); largest_contour = temp_contour; } } } MatOfPoint temp_largest = largest_contours.get(largest_contours.size()-1); largest_contours = new ArrayList<MatOfPoint>(); largest_contours.add(temp_largest); Imgproc.cvtColor(imgSource, imgSource, Imgproc.COLOR_BayerBG2RGB); Imgproc.drawContours(imgSource, contours, maxAreaIdx, new Scalar(0, 255, 0), 1); Log.d(TAG, "Largers Contour:" + contours.get(maxAreaIdx).toString()); return imgSource; } ,这就是你获得2个分区的原因。

相关问题