RDDSampleUtils { public static int getSampleNumbers(int numPartitions, long totalNumberOfRecords, int givenSampleNumbers) { if (givenSampleNumbers > 0) { if (givenSampleNumbers > totalNumberOfRecords) { throw new IllegalArgumentException("[GeoSpark] Number of samples " + givenSampleNumbers + " cannot be larger than total records num " + totalNumberOfRecords); } return givenSampleNumbers; } if (totalNumberOfRecords < 2 * numPartitions) { throw new IllegalArgumentException("[GeoSpark] Number of partitions " + numPartitions + " cannot be larger than half of total records num " + totalNumberOfRecords); } if (totalNumberOfRecords < 1000) { return (int) totalNumberOfRecords; } final int minSampleCnt = numPartitions * 2; return (int) Math.max(minSampleCnt, Math.min(totalNumberOfRecords / 100, Integer.MAX_VALUE)); }  static int getSampleNumbers(int numPartitions, long totalNumberOfRecords, int givenSampleNumbers);  }
@Test public void testGetSampleNumbers() { assertEquals(10, RDDSampleUtils.getSampleNumbers(2, 10, -1)); assertEquals(100, RDDSampleUtils.getSampleNumbers(2, 100, -1)); assertEquals(10, RDDSampleUtils.getSampleNumbers(5, 1000, -1)); assertEquals(100, RDDSampleUtils.getSampleNumbers(5, 10000, -1)); assertEquals(100, RDDSampleUtils.getSampleNumbers(5, 10001, -1)); assertEquals(1000, RDDSampleUtils.getSampleNumbers(5, 100011, -1)); assertEquals(99, RDDSampleUtils.getSampleNumbers(6, 100011, 99)); assertEquals(999, RDDSampleUtils.getSampleNumbers(20, 999, -1)); assertEquals(40, RDDSampleUtils.getSampleNumbers(20, 1000, -1)); }