Reading from BigQuery and store data to Google storage (Special Character issue)(从 BigQuery 读取并将数据存储到 Google 存储(特殊字符问题))
问题描述
Reference: Can Google Data flow use existent VM and not temporary created ones?
Code is working, but the issue is that when it saves response from BigQuery to google storage all the Japanese characters are corrupted.
PCollectionTuple QVCollections = rows.apply("FilterEmptyRows", ParDo.of(new FilterEmptyRowDoFn("TransactionId", "TransactionDateTime"))).apply("CreateQVFiles",ParDo.of(new TransactionToQVFilesDoFnJP())
.withOutputTags(BobShare.QVHeaders, TupleTagList.of(BobShare.QVEvents).and(BobShare.QVPayments)));
QVCollections.get(BobShare.QVEvents).apply("WriteQVEvents", TextIO.write().to(storagePath + CSV_OUTPUT_FOLDER + "events_" + timeSuffix).withoutSharding().withHeader(CSV_HEADER_EVENTS).withSuffix(".csv"));
QVCollections.get(BobShare.QVPayments).apply("WriteQVPayments", TextIO.write().to(storagePath + CSV_OUTPUT_FOLDER + "payments_" + timeSuffix).withoutSharding().withHeader(CSV_HEADER_PAYMENTS).withSuffix(".csv"));
QVCollections.get(BobShare.QVHeaders).apply("WriteQVHeaders", TextIO.write().to(storagePath + CSV_OUTPUT_FOLDER + "header_" + timeSuffix).withoutSharding().withHeader(CSV_HEADER_TRANSACTION).withSuffix(".csv"));
Based on what I have found, need to use .withCoder(StringUtf8Coder.of())
In addition, this is what have tried (but working only locally - DirectRunner)
private static void uploadBlob(String project, String bucket, String filename, String localfile) {
String listFromCsv = readCsvFromLocalStorage(localfile);
Storage storage = StorageOptions.newBuilder().setProjectId(project).build().getService();
BlobId blobId = BlobId.of(bucket, filename);
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("application/json").setContentEncoding(UTF_8).build();
try {
storage.create(blobInfo, listFromCsv.getBytes(UTF_8));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
private static String readCsvFromLocalStorage(String fileName) {
StringBuilder builder = new StringBuilder();
Path pathToFile = Paths.get(fileName);
try (BufferedReader br = Files.newBufferedReader(pathToFile,
StandardCharsets.UTF_8)) {
// read the first line from the text file
String line = br.readLine();
// loop until all lines are read
while (line != null) {
builder.append(line).append("
");
line = br.readLine();
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
return builder.toString();
}
private static void deleteLocalFile (String fileName)
{
try {
if (new File(fileName).delete()) {
System.out.println(fileName + " deleted.");
} else {
System.out.println(fileName + " could not be deleted.");
}
} catch (Exception e)
{
System.out.println(fileName + " could not be deleted.");
e.printStackTrace();
}
}
This is how data looks like (corrupted) : JAPANESE CHRACTERS
Any suggestions? Any .... (((
You need to replace
BufferedReader br = Files.newBufferedReader(pathToFile, StandardCharsets.UTF_8))
by
BufferedReader br = Files.newBufferedReader(pathToFile, Charset.forName("UTF-8"))
这篇关于从 BigQuery 读取并将数据存储到 Google 存储(特殊字符问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从 BigQuery 读取并将数据存储到 Google 存储(特殊字符问题)
基础教程推荐
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01