我有 5GB 使用 apache thrift 序列化的数据和一个带有数据格式的 .thrift 文件。我尝试过使用 thriftpy 和官方 thrift 包,但我不知道如何打开文件。
数据是 http://www.iesl.cs.umass.edu/data/wiki-links 的扩展数据集
可以在此处找到数据格式的说明 https://code.google.com/p/wiki-link/wiki/ExpandedDataset
最佳答案
Scala 设置可以在 ThriftSerializerFactory.scala 中找到。文件。由于大多数事物的命名在整个 Thrift 库中都是一致的,因此您或多或少会根据 Scala 示例对 Python 代码进行建模:
package edu.umass.cs.iesl.wikilink.expanded.process
import org.apache.thrift.protocol.TBinaryProtocol
import org.apache.thrift.transport.TIOStreamTransport
import java.io.File
import java.io.BufferedOutputStream
import java.io.FileOutputStream
import java.io.BufferedInputStream
import java.io.FileInputStream
import java.util.zip.{GZIPOutputStream, GZIPInputStream}
object ThriftSerializerFactory {
def getWriter(f: File) = {
val stream = new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(f)), 2048)
val protocol= new TBinaryProtocol(new TIOStreamTransport(stream))
(stream, protocol)
}
def getReader(f: File) = {
val stream = new BufferedInputStream(new GZIPInputStream(new FileInputStream(f)), 2048)
val protocol = new TBinaryProtocol(new TIOStreamTransport(stream))
(stream, protocol)
}
}
您基本上设置了流传输和二进制协议(protocol)。如果您将数据压缩,则必须将 gzip 片段添加到拼图中,但是一旦数据解压缩,就不再需要它了。
WikiLinkItemIterator.scala中的代码展示了如何使用上面的工厂类读取数据文件。
class PerFileWebpageIterator(f: File) extends Iterator[WikiLinkItem] {
var done = false
val (stream, proto) = ThriftSerializerFactory.getReader(f)
private var _next: Option[WikiLinkItem] = getNext()
private def getNext(): Option[WikiLinkItem] = try {
Some(WikiLinkItem.decode(proto))
} catch {case _: TTransportException => {done = true; stream.close(); None}}
def hasNext(): Boolean = !done && (_next != None || {_next = getNext(); _next != None})
def next(): WikiLinkItem = if (hasNext()) _next match {
case Some(wli) => {_next = None; wli}
case None => {throw new Exception("Next on empty iterator.")}
} else throw new Exception("Next on empty iterator.")
}
实现步骤:
- 像上面一样实现 Thrift 协议(protocol)栈工厂(推荐模式,顺便说一句)
- 实例化每条记录的根元素,在我们的例子中是
WikiLinkItem
- 调用
instance.read(proto)
读取一条数据
关于python - 在python中打开apache thrift二进制文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27421449/