是否可以在不使用Apache Spark的情况下从Scala读取镶木地板文件?
我找到了一个允许我们使用普通scala读写avro文件的项目.
https://github.com/sksamuel/avro4s
但是我找不到使用普通scala程序读取和编写镶木地板文件而不使用Spark的方法?
mon*_*ack 17
使用镶木地板项目是非常简单的,这是Alexey Raga在他的回答中提到的项目.
一些示例代码
val reader = AvroParquetReader.builder[GenericRecord](path).build().asInstanceOf[ParquetReader[GenericRecord]]
// iter is of type Iterator[GenericRecord]
val iter = Iterator.continually(reader.read).takeWhile(_ != null)
// if you want a list then...
val list = iter.toList
Run Code Online (Sandbox Code Playgroud)
这将返回一个标准的Avro GenericRecord,但是如果你想把它变成一个scala案例类,那么你可以在你的问题链接时使用我的Avro4s库,为你做编组.假设您使用的是1.30或更高版本,那么:
case class Bibble(name: String, location: String)
val format = RecordFormat[Bibble]
// then for a given record
val bibble = format.from(record)
Run Code Online (Sandbox Code Playgroud)
我们显然可以在一步中将它与原始迭代器结合起来:
val reader = AvroParquetReader.builder[GenericRecord](path).build().asInstanceOf[ParquetReader[GenericRecord]]
val format = RecordFormat[Bibble]
// iter is now an Iterator[Bibble]
val iter = Iterator.continually(reader.read).takeWhile(_ != null).map(format.from)
// and list is now a List[Bibble]
val list = iter.toList
Run Code Online (Sandbox Code Playgroud)
是的,您不必使用 Spark 来读/写 Parquet。只需直接从 Scala 代码中使用 parquet lib(这就是 Spark 正在做的事情):http ://search.maven.org/#search%7Cga%7C1%7Cparquet