我有一个目录下驻留在hadoop环境下的多个avro文件,我需要合并所有这些文件,使之成为一个单一的avro文件。例子
/abc-> x.avro y.avro } => a.avro z.avro
文件a.avro将包含所有x,y,z文件的内容,其中x,y,z文件具有相同的模式。我需要创建一个java应用程序。谢谢你的帮助。谢谢。
s3fp2yjn1#
apacheavro提供了一些工具来处理这里的avro文件操作。这些工具包括merging/concat工具,用于将相同的模式avro文件与非保留元数据合并,cattool用于从avro数据文件中提取样本,concat工具用于将输入文件从avro二进制文件转换为json,recoverytool用于从损坏的avro数据文件中恢复数据等(有关所述github url的更多信息)。我已经从github上提到的相同工具中提取了代码,下面是一个java应用程序,它确实解决了您的问题。
Path inPath = new Path("C:\\Users\\vaijnathp\\IdeaProjects\\MSExcel\\vaj"); Path outPath = new Path("getDestinationPath") ; FileSystem fs = FileSystem.get(new Configuration()); FileStatus [] contents contents = fs.listStatus(inPath, new OutputLogFilter()); DataFileWriter<GenericRecord> writer = new DataFileWriter<>(new GenericDatumWriter<>()); Schema schema = null; String inputCodec = null; Map<String, byte[]> metadata = new TreeMap<>(); BufferedOutputStream output = new BufferedOutputStream(new BufferedOutputStream(fs.create(outPath))); for (int i = 0; i < contents.length; i++) { FileStatus folderContent = contents[i]; if (folderContent.isFile() && folderContent.getPath().getName().endsWith(".avro")) { InputStream input = new BufferedInputStream(fs.open(folderContent.getPath())); DataFileStream<GenericRecord> reader = new DataFileStream<>(input, new GenericDatumReader<GenericRecord>()); if (schema == null) { schema = reader.getSchema(); //extract metadata for further check. extractAvroFileMetadata(writer, metadata, reader); inputCodec = reader.getMetaString(DataFileConstants.CODEC); if (inputCodec == null) inputCodec = DataFileConstants.NULL_CODEC; writer.setCodec(CodecFactory.fromString(inputCodec)); writer.create(schema, output); } else { if (!schema.equals(reader.getSchema())) reader.close(); //compare FileMetadata with previously extracted one CompareAvroFileMetadata(metadata, reader, folderContent.getPath().getName()); String thisCodec = reader.getMetaString(DataFileConstants.CODEC); if (thisCodec == null) thisCodec = DataFileConstants.NULL_CODEC; if (!inputCodec.equals(thisCodec)) reader.close(); } writer.appendAllFrom(reader, false); reader.close(); } } writer.close(); }catch (Exception e){ e.printStackTrace(); }
我希望这个代码片段能帮助您创建java应用程序。谢谢。
1条答案
按热度按时间s3fp2yjn1#
apacheavro提供了一些工具来处理这里的avro文件操作。这些工具包括merging/concat工具,用于将相同的模式avro文件与非保留元数据合并,cattool用于从avro数据文件中提取样本,concat工具用于将输入文件从avro二进制文件转换为json,recoverytool用于从损坏的avro数据文件中恢复数据等(有关所述github url的更多信息)。
我已经从github上提到的相同工具中提取了代码,下面是一个java应用程序,它确实解决了您的问题。
我希望这个代码片段能帮助您创建java应用程序。谢谢。