java - 在 Java 中将 ORC 转换为 JSON
问题描述
我正在尝试在单元测试中将输出 ORC 文件转换为 Java 中的 JSON。我一直在阅读他们的单元测试并受到以下启发:
PrintStream origOut = System.out;
String outputFilename = "orc-file-dump.json";
String tmpFileLocationJson = createTempFileJson();
FileOutputStream myOut = new FileOutputStream(tmpFileLocationJson);
// replace stdout and run command
System.setOut(new PrintStream(myOut, true, StandardCharsets.UTF_8.toString()));
FileDump.main(new String[]{"data", tmpFileLocationJson});
System.out.flush();
System.setOut(origOut);
System.out.println("done");
像这样的东西。问题是我不太确定如何将此代码等同于 java utils 利用率:
java -jar orc-tools-1.5.5-uber.jar data output-1595448128191.orc
例如,输出以下 JSON 转储。
{"integerExample":1,"nestedExample":{"sub1":"value1","sub2":42},"dateExample":"2018-01-04"}
所以我想将 ORC 转换为 JSON,以便在我的单元测试中进行交叉引用。
解决方案
好的,我从 Hive 中出售了代码并将输出流覆盖到文件写入器,并将输出重定向到文件中以读回测试。
static void printJsonData(String fileName, PrintStream printStream,
Reader reader) throws IOException, JSONException, org.codehaus.jettison.json.JSONException {
// OutputStreamWriter out = new OutputStreamWriter(printStream, "UTF-8");
BufferedWriter out = new BufferedWriter(new FileWriter(fileName.concat(".json")));
RecordReader rows = reader.rows();
try {
TypeDescription schema = reader.getSchema();
VectorizedRowBatch batch = schema.createRowBatch();
while (rows.nextBatch(batch)) {
for (int r = 0; r < batch.size; ++r) {
JSONWriter writer = new JSONWriter(out);
printRow(writer, batch, schema, r);
out.write("\n");
out.flush();
if (printStream.checkError()) {
throw new IOException("Error encountered when writing to stdout.");
}
}
}
} finally {
rows.close();
}
}
推荐阅读
- typescript - 从数组/对象中解构值并同时转换
- r - “linspace”函数的 Rcpp 代码:增量 1e-5 时向量长度不正确
- c# - 使用 boxcollider2d.bounds.size.x 给了我错误的大小
- python - 如何让我的机器人根据选择的数字说出不同的话?(discord.py)
- python - 教程的 Django 测试未按预期工作
- gridview - 将数据绑定到 GridView 内的 Syncfusion RTE
- node.js - 使用 Angular、Node、Express、Multer 的图像上传问题
- java - 从 Eclipse 2020-06 更新到 2020-09 后,Java 支持消失了
- c - 周期性高内核 CPU 负载?
- python - 数组到 BST 基本情况