Apache pig 按键提取tfidf向量而不破坏文件格式

Apache pig 按键提取tfidf向量而不破坏文件格式,apache-pig,extract,mahout,Apache Pig,Extract,Mahout,我有大约200000个输出格式为seq2sparse的tfidf向量。现在我需要提取500,但不是像split函数那样随机提取。我知道其中500个的密钥,我需要它们的数据格式与seq2sparse中的相同。 当我打开带有200000个条目的sequencefile时,我可以看到这些键是用 org.apache.hadoop.io.Text和带有org.apache.mahout.math.VectorWritable的值 但是当我尝试使用 及 在用于读写它们的Pig拉丁语中,输出的key和va

我有大约200000个输出格式为seq2sparse的tfidf向量。现在我需要提取500,但不是像split函数那样随机提取。我知道其中500个的密钥,我需要它们的数据格式与seq2sparse中的相同。 当我打开带有200000个条目的sequencefile时,我可以看到这些键是用 org.apache.hadoop.io.Text和带有org.apache.mahout.math.VectorWritable的值

但是当我尝试使用

在用于读写它们的Pig拉丁语中,输出的key和value都是org.apache.hadoop.io.Text

我确实需要这种格式的500个条目,因为我想在trainnb和testnb中使用它们


基本上,只要知道我如何做mahout seqdumper的相反操作就足够了。

虽然没有特定的mahout命令,但您可以使用mahout的以下命令编写一个相对简单的实用程序函数:

org.apache.mahout.common.Pair;
org.apache.mahout.common.iterator.sequencefile.SequenceFileIterable;
org.apache.mahout.math.VectorWritable;  
以及:

您可以执行以下操作:

// load up the 500 desired keys with some function
Vector<Text>desiredKeys = getDesiredKeys();
//create a new SequenceFile writer for the 500 Desired Vectors 
SequenceFile.Writer writer =
        SequenceFile.createWriter(fs, conf, output500filePath ,
                                  Text.class,
                                  VectorWritable.class);         
try {
  // create an iterator over the tfidfVector sequence file 
  SequenceFileIterable<Text, VectorWritable>seqFileIterable =
          new SequenceFileIterable<Text, VectorWritable>(
              tfidfVectorPath, true, conf)

  // loop over tfidf sequence file and write out only Pairs with keys
  // contained in the desiredKeys Vector to the output500file 
  for (Pair<Text, VectorWritable> pair : seqFileIterable) {
      if(desiredKeys.contains(pair.getFirst())){
            writer.append(pair.getFirst(),pair.getSecond());
      }
  }
}finally {
  Closeables.close(writer, false);
}
并使用Output500文件的路径作为trainnb的输入。使用vector.contains并不是最有效的方法,但这是一般的想法

// load up the 500 desired keys with some function
Vector<Text>desiredKeys = getDesiredKeys();
//create a new SequenceFile writer for the 500 Desired Vectors 
SequenceFile.Writer writer =
        SequenceFile.createWriter(fs, conf, output500filePath ,
                                  Text.class,
                                  VectorWritable.class);         
try {
  // create an iterator over the tfidfVector sequence file 
  SequenceFileIterable<Text, VectorWritable>seqFileIterable =
          new SequenceFileIterable<Text, VectorWritable>(
              tfidfVectorPath, true, conf)

  // loop over tfidf sequence file and write out only Pairs with keys
  // contained in the desiredKeys Vector to the output500file 
  for (Pair<Text, VectorWritable> pair : seqFileIterable) {
      if(desiredKeys.contains(pair.getFirst())){
            writer.append(pair.getFirst(),pair.getSecond());
      }
  }
}finally {
  Closeables.close(writer, false);
}