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

    https://github.com/apache/spark/pull/1658#discussion_r16207161
  
    --- Diff: 
core/src/main/scala/org/apache/spark/input/FixedLengthBinaryInputFormat.scala 
---
    @@ -0,0 +1,95 @@
    +/*
    + * 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.input
    +
    +import org.apache.hadoop.fs.Path
    +import org.apache.hadoop.io.{BytesWritable, LongWritable}
    +import org.apache.hadoop.mapreduce.lib.input.FileInputFormat
    +import org.apache.hadoop.mapreduce.{InputSplit, JobContext, RecordReader, 
TaskAttemptContext}
    +
    +/**
    + * Custom Input Format for reading and splitting flat binary files that 
contain records,
    + * each of which are a fixed size in bytes. The fixed record size is 
specified through
    + * a parameter recordLength in the Hadoop configuration.
    + */
    +
    +object FixedLengthBinaryInputFormat {
    +
    +  /**
    +   * This function retrieves the recordLength by checking the 
configuration parameter
    +   *
    +   */
    +  def getRecordLength(context: JobContext): Int = {
    +
    +    // retrieve record length from configuration
    +    context.getConfiguration.get("recordLength").toInt
    +  }
    +
    +}
    +
    +class FixedLengthBinaryInputFormat extends FileInputFormat[LongWritable, 
BytesWritable] {
    +
    +
    +  /**
    +   * Override of isSplitable to ensure initial computation of the record 
length
    +   */
    +  override def isSplitable(context: JobContext, filename: Path): Boolean = 
{
    +
    +    if (recordLength == -1) {
    +      recordLength = FixedLengthBinaryInputFormat.getRecordLength(context)
    +    }
    +    if (recordLength <= 0) {
    +      println("record length is less than 0, file cannot be split")
    +      false
    +    } else {
    +      true
    +    }
    +
    +  }
    +
    +  /**
    +   * This input format overrides computeSplitSize() to make sure that each 
split
    +   * only contains full records. Each InputSplit passed to 
FixedLengthBinaryRecordReader
    +   * will start at the first byte of a record, and the last byte will the 
last byte of a record.
    +   */
    +  override def computeSplitSize(blockSize: Long, minSize: Long, maxSize: 
Long): Long = {
    +
    +    val defaultSize = super.computeSplitSize(blockSize, minSize, maxSize)
    +
    +    // If the default size is less than the length of a record, make it 
equal to it
    +    // Otherwise, make sure the split size is as close to possible as the 
default size,
    +    // but still contains a complete set of records, with the first record
    +    // starting at the first byte in the split and the last record ending 
with the last byte
    +
    +    defaultSize match {
    +      case x if x < recordLength => recordLength.toLong
    +      case _ => (Math.floor(defaultSize / recordLength) * 
recordLength).toLong
    +    }
    --- End diff --
    
    Probably clearer to write this as
    ```
    if (defaultSize < recordLength) {
      recordLenght.toLong
    } else {
      (Math.floor(defaultSize / recordLength) * recordLength).toLong
    }
    ```


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

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

Reply via email to