【问题标题】:Inserting a fixed width file in to Hive using scala spark使用 scala spark 将固定宽度的文件插入 Hive
【发布时间】:2018-07-13 10:58:24
【问题描述】:

我有这样的示例文件记录

2018-01-1509.05.540000000000001000000751111EMAIL@AAA.BB.CL

上面的记录来自一个固定长度的文件,我想根据长度进行拆分 当我拆分时,我得到一个如下所示的列表。

ListBuffer(2018-01-15, 09.05.54, 00000000000010000007, 5, 1111, EMAIL@AAA.BB.CL)

到目前为止一切看起来都很好。但我不确定为什么在列表中的每个字段中添加额外的空间(不是第一个字段)。

Example : My data is "09.05.54",But I am getting as" 09.05.54" in the list.

我的拆分逻辑如下所示

val lengths = List("10", "8", "20", "1", "4","15")

// Logic to Split the Line based on the lengths
  def splitLineBasedOnLengths(line: String, lengths: List[String]): ListBuffer[Any] = {
    var splittedLine = line
    var split = new ListBuffer[Any]()
    for (i <- lengths) yield {
      var c = i.toInt
      var fi = splittedLine.take(c)
      split += fi
      splittedLine = splittedLine.drop(c)
    }
    split
  }

上面的代码将 line 和 list[String] 作为输入,它们只是长度,并给出了 listbuffer[Any],它根据长度分割了行。

When we insert into hive because of this issue every column except the first is getting increased by one character

when I use length(COLUMN NAME) it is showing one character extra ie space for every column

谁能帮助我,为什么拆分后每个字段前都有多余的空间?

【问题讨论】:

    标签: scala apache-spark hive apache-spark-sql hiveql


    【解决方案1】:

    这并没有给我空间,而是使用了更惯用的 Scala:

    def splitThis(line: String, lengths: List[String]): List[String] = {
      def loop(l: String, ls: List[Int], acc: Seq[String]): Seq[String] = 
        if (l.isEmpty || ls.isEmpty) acc else loop(l.drop(ls.head), ls.tail, acc :+ 
    l.take(ls.head))
      loop(line, lengths.map(_.toInt), Seq.empty).toList
    }
    

    【讨论】:

      【解决方案2】:

      问题出在您的数据上,请在下方尝试。

      在您的数据中,“,”之间有额外的空格。

        ListBuffer(2018-01-15,09.05.54,00000000000010000007,5,1111,EMAIL@AAA.BB.CL)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-16
        相关资源
        最近更新 更多