Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1832 fix load csv option encoding. #1833

Merged
merged 1 commit into from
Sep 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package tech.mlsql.tool

import java.io.{BufferedReader, ByteArrayOutputStream, File, InputStream, InputStreamReader}
import org.apache.commons.io.FileUtils
import org.apache.commons.lang3.StringUtils
import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.fs.{FSDataInputStream, FSDataOutputStream, FileStatus, FileSystem, Path}
import org.apache.hadoop.fs._
import org.apache.hadoop.io.IOUtils
import org.apache.spark.MLSQLSparkUtils
import tech.mlsql.common.utils.Md5
import tech.mlsql.common.utils.path.PathFun

import java.io.{FileSystem => _, _}
import scala.collection.mutable.ArrayBuffer

/**
Expand Down Expand Up @@ -266,31 +265,33 @@ object HDFSOperatorV2 {
}
}

def saveWithoutTopNLines(inPath: String, skipFirstNLines: Int, header: Boolean): String = {
def saveWithoutTopNLines(inPath: String, skipFirstNLines: Int, header: Boolean, encoding: String): String = {
val fs = FileSystem.get(hadoopConfiguration)
val charset = encoding.trim.toLowerCase
val src: Path = new Path(inPath)
var br: BufferedReader = null
var line: String = null
var dos: FSDataOutputStream = null
val pathElements = inPath.split(PathFun.pathSeparator)
val writePathParts = pathElements.take(pathElements.length - 1) :+ String.format("skipFirstNLines_%s_%s", String.valueOf(skipFirstNLines), pathElements(pathElements.length - 1))
val writePathParts = pathElements.take(pathElements.length - 1) :+
s"skipFirstNLines_${skipFirstNLines}_${charset}_${pathElements(pathElements.length - 1)}"
val outPath = writePathParts.mkString(PathFun.pathSeparator)
if (!fileExists(outPath)) {
try {
dos = fs.create(new Path(new java.io.File(outPath).getPath), true)
br = new BufferedReader(new InputStreamReader(fs.open(src)))
br = new BufferedReader(new InputStreamReader(fs.open(src), charset))
line = br.readLine()

var count = 1
while (line != null) {

if (header && count == 1) {
dos.writeBytes(line + "\n")
dos.write((line + "\n").getBytes(charset))
line = br.readLine()
}

if (count >= skipFirstNLines) {
dos.writeBytes(line + "\n")
dos.write((line + "\n").getBytes(charset))
}
count += 1
line = br.readLine()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ class MLSQLCSV(override val uid: String) extends MLSQLBaseFileSource with WowPar
}
case _ => false
}
val encoding = config.config.getOrElse("encoding", "utf-8")
val path = originPath
val newPath = HDFSOperatorV2.saveWithoutTopNLines(path, numsToSkip, header)
val newPath = HDFSOperatorV2.saveWithoutTopNLines(path, numsToSkip, header, encoding)
newPath
}
}
Expand Down