【问题标题】:Writing hadoop sequence file编写hadoop序列文件
【发布时间】:2015-02-20 06:55:20
【问题描述】:

我有一个文本文件,其中的数据以以下(键、值)格式写入:

1,34
5,67
8,88

文件放在本地文件系统中。

我想将它转换成一个 hadoop 序列文件,再次在本地文件系统上,以便在 mahout 中使用它。序列文件应该包含所有记录。例如,对于记录 1,1 是键,34 是值。其他记录也是如此。

我是 Java 新手。我将不胜感激。

谢谢。

【问题讨论】:

标签: hadoop mahout


【解决方案1】:

好吧,我确实找到了一种方法。代码如下:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.SequenceFile;
import org.apache.hadoop.io.Text;

public class CreateSequenceFile {
    public static void main(String[] argsx) throws FileNotFoundException, IOException 
      {
       String myfile = "/home/ashokharnal/keyvalue.txt";
       String outputseqfile =  "/home/ashokharnal/part-0000";
       Path path = new Path(outputseqfile);

       //open input file
       BufferedReader br = new BufferedReader(new FileReader(myfile));
       //create Sequence Writer
       Configuration conf = new Configuration();        
       FileSystem fs = FileSystem.get(conf);
       SequenceFile.Writer writer = new SequenceFile.Writer(fs,conf,path,LongWritable.class,Text.class);
       LongWritable key ; 
       Text value ;
       String line = br.readLine();
       String field_delimiter = ",";
       String[] temp;
       while (line != null) {
          try
           {
               temp = line.split(field_delimiter);
               key = new LongWritable(Integer.valueOf(temp[0]))  ;
               value = new Text(temp[1].toString());
               writer.append(key,value);    
               System.out.println("Appended to sequence file key " + key.toString() + " and value " + value.toString());
               line = br.readLine();    
           }
           catch(Exception ex)
           {
              ex.printStackTrace();
           }
      }        
    writer.close();
}
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-30
    • 2013-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多