Github user thunterdb commented on a diff in the pull request:

    https://github.com/apache/spark/pull/19439#discussion_r150650409
  
    --- Diff: mllib/src/main/scala/org/apache/spark/ml/image/HadoopUtils.scala 
---
    @@ -0,0 +1,109 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *    http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.spark.ml.image
    +
    +import scala.language.existentials
    +import scala.util.Random
    +
    +import org.apache.commons.io.FilenameUtils
    +import org.apache.hadoop.conf.{Configuration, Configured}
    +import org.apache.hadoop.fs.{Path, PathFilter}
    +import org.apache.hadoop.mapreduce.lib.input.FileInputFormat
    +
    +import org.apache.spark.sql.SparkSession
    +
    +private object RecursiveFlag {
    +  /**
    +   * Sets the spark recursive flag and then restores it.
    +   *
    +   * @param value Value to set
    +   * @param spark Existing spark session
    +   * @param f The function to evaluate after setting the flag
    +   * @return Returns the evaluation result T of the function
    +   */
    +  def withRecursiveFlag[T](value: Boolean, spark: SparkSession)(f: => T): 
T = {
    +    val flagName = FileInputFormat.INPUT_DIR_RECURSIVE
    +    val hadoopConf = spark.sparkContext.hadoopConfiguration
    +    val old = Option(hadoopConf.get(flagName))
    +    hadoopConf.set(flagName, value.toString)
    +    try f finally {
    +      old match {
    +        case Some(v) => hadoopConf.set(flagName, v)
    +        case None => hadoopConf.unset(flagName)
    +      }
    +    }
    +  }
    +}
    +
    +/**
    + * Filter that allows loading a fraction of HDFS files.
    + */
    +private class SamplePathFilter extends Configured with PathFilter {
    --- End diff --
    
    Ok, if we think that duplicate file names are not an issue, then I would 
prefer having using the deterministic hashing-based scheme. I am also happy to 
make this into another PR since this is a pretty small matter.
    
    @imatiach-msft a good hashing function is certainly a concern, and we will 
need to make a trade off between performance and correctness. If we really want 
to make sure that it works as expected, the best is probably to use a 
cryptographic hash, like `SHA-256` (which has strong guarantees of the 
distribution of output values):
    
https://stackoverflow.com/questions/5531455/how-to-hash-some-string-with-sha256-in-java
    We have `murmur3` in the Spark source code, but it is not cryptographic and 
does not come with as strong guarantees. For the sake of performance, we may 
want to use it eventually.
    
    Once you have the digest, which is a byte array, then it can be converted 
to a long first (by taking 8 bytes from the whole digest):
    
https://stackoverflow.com/questions/4485128/how-do-i-convert-long-to-byte-and-back-in-java
    and then you can convert this long to a double and compare it to the 
requested fraction:
    
    ```java
      Long l = new Long(...);
        double d = l.doubleValue();
       bool shouldKeep = d / (2<<31) <= requestedFraction
    ```


---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to