Github user CHOIJAEHONG1 commented on the pull request:
https://github.com/apache/spark/pull/7494#issuecomment-128335460
The testcase passed with `Sys.setlocale("LC_ALL", "en_US.UTF-8")`. But, I
had to modify context.R to clear out the UTF-8 indicating bit. It seems to turn
out that the UTF-8 indicating bit in R with `Encoding(x) <- "UTF-8" causes an
error in passing a string to Spark.
I hope the code blow support and preserve UTF-8 encoding in R under
`LC_ALL=C`.
context.R
```
127 # Serialize each slice: obtain a list of raws, or a list of lists
(slices) of
128 # 2-tuples of raws
129 removeUtf8EncodingBit <- function(s) {
130 Encoding(s) <- "bytes"
131 s
132 }
133 slices_ <- rapply(slices, function(x) ifelse(is.character(x),
removeUtf8EncodingBit(x), x), how="list")
134 serializedSlices <- lapply(slices_, serialize, connection = NULL)
135
136 jrdd <- callJStatic("org.apache.spark.api.r.RRDD",
137 "createRDDFromArray", sc, serializedSlices)
138
139 RDD(jrdd, "byte")
140 }
```
deserializer.R
```
readString <- function(con) {
stringLen <- readInt(con)
raw <- readBin(con, raw(), stringLen, endian = "big")
string <- rawToChar(raw)
Encoding(string) <- "UTF-8"
string
}
```
testcase
```
test_that("collect() support Unicode characters", {
convertToUtf8 <- function(s) {
Encoding(s) <- "UTF-8"
s
}
Sys.setlocale("LC_ALL", "en_US.UTF-8")
lines <- c("{\"name\":\"ìë
íì¸ì\"}",
"{\"name\":\"æ¨å¥½\", \"age\":30}",
"{\"name\":\"ããã«ã¡ã¯\", \"age\":19}",
"{\"name\":\"Xin chà o\"}")
jsonPath <- tempfile(pattern="sparkr-test", fileext=".tmp")
writeLines(lines, jsonPath)
df <- read.df(sqlContext, jsonPath, "json")
rdf <- collect(df)
expect_true(is.data.frame(rdf))
expect_equal(rdf$name[1], convertToUtf8("ìë
íì¸ì"))
expect_equal(rdf$name[2], convertToUtf8("æ¨å¥½"))
expect_equal(rdf$name[3], convertToUtf8("ããã«ã¡ã¯"))
expect_equal(rdf$name[4], convertToUtf8("Xin chà o"))
df1 <- createDataFrame(sqlContext, rdf)
expect_equal(collect(where(df1, df1$name == "æ¨å¥½"))$name,
convertToUtf8("æ¨å¥½"))
})
```
---
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 [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]