使用scala spark将固定宽度的文件插入Hive

问题描述 投票:0回答:2

我有像这样的样本文件记录

[email protected]

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

ListBuffer(2018-01-15, 09.05.54, 00000000000010000007, 5, 1111, [email protected])

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

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
  }

上面的代码采用行和列表[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
2个回答
0
投票

这不会给我空间并使用更多惯用的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
}

0
投票

问题与你的数据有关,请尝试以下。

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

  ListBuffer(2018-01-15,09.05.54,00000000000010000007,5,1111,[email protected])
© www.soinside.com 2019 - 2024. All rights reserved.