2013-03-21 2 views
0

다음 코드를 사용하여 일부 데이터를 시퀀스 파일 형식 파일에 씁니다. 프로그램이 잠시 실행되면, Eclipse 콘솔의 빨간 버튼을 통해 프로그램을 중단합니다. 그러나 hdfs에서 데이터 파일을 검사 할 때 시퀀스 파일의 크기는 0입니다. 또한 'hadoop fs -text filename'명령을 사용하여 파일을 볼 수 없습니다. SequenceFile.Reader를 사용하여 이전에 작성된 파일을 읽을 때 '스레드의 예외 "주"java.io.EOFException'예외가 발생합니다. 이 경우, 어떻게해야합니까? 내 개발 환경은 CentOS 6에서 eclipse3.7 (Windows 7)과 hadoop 클러스터 (hadoop 버전 1.0.3)입니다.SequenceFile 클래스 쓰기 파일 사용

클래스 순서는 스레드 {

private String uri = "hdfs://172.20.11.60:9000"; 
private String filePath = "/user/hadoop/input/"; 
private String fileName = "Sequence-01.seq"; 
public SequenceFile.Writer writer; 
private static int cnt = 0; 

private void init() { 
    Configuration conf = new Configuration(); 
    try { 
     FileSystem fs = FileSystem.get(URI.create(uri), conf); 
     writer = SequenceFile.createWriter(fs, conf, new Path(filePath 
       + fileName), LongWritable.class, Text.class); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

public Sequence() { 
    init(); 
} 

@Override 
public void run(){ 
    while(true){ 
     try { 
      writer.append(new LongWritable(100), new Text("hello,world")); 
      cnt++; 
      if(cnt%100 == 0){ 
       System.out.println("flush current data to file system"); 
       writer.syncFs(); 
      } 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      System.out.println("append data error"); 
      e.printStackTrace(); 
     } 

     try { 
      Thread.sleep(1000); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      System.out.println("thread interupted"); 
      e.printStackTrace(); 
     } 
    } 
} 

}

공용 클래스 TestSequenceFile {

/** 
* @param args 
*/ 
public static void main(String[] args) { 
    // TODO Auto-generated method stub 

    new Sequence().start(); 
} 

}

답변

0

일반적인 조언을 확장 : 프로세스를 중단하지.

솔루션 : 다음 코드는 문제없이 작동하고 있습니다.

import java.io.IOException; 
import java.net.URI; 

import org.apache.hadoop.conf.Configuration; 
import org.apache.hadoop.fs.FileSystem; 
import org.apache.hadoop.fs.Path; 
import org.apache.hadoop.io.IOUtils; 
import org.apache.hadoop.io.IntWritable; 
import org.apache.hadoop.io.SequenceFile; 

import org.apache.hadoop.io.Text; 


public class SequenceFileWriteDemo { 
private static final String[] DATA = { 
"One, two, buckle my shoe", 
"Three, four, shut the door", 
"Five, six, pick up sticks", 
"Seven, eight, lay them straight", 
"Nine, ten, a big fat hen"}; 

public static void main(String[] args) throws IOException { 
//String uri = "/home/Desktop/inputSort.txt"; 
String uri = "hdfs://localhost:9900/out1.seq"; 

Configuration conf = new Configuration(); 
FileSystem fs = FileSystem.get(URI.create(uri), conf); 
Path path = new Path(uri); 
IntWritable key = new IntWritable(); 
Text value = new Text(); 
SequenceFile.Writer writer = null; 



try { 
writer = SequenceFile.createWriter(fs, conf, path, 
    key.getClass(), value.getClass()); 


    for (int i = 0; i < 130; i++) { 
    key.set(100 - i); 
    value.set(DATA[i % DATA.length]); 


    System.out.printf("[%s]\t%s\t%s\n", writer.getLength(), key, value, key.getClass(), value.getClass()); 

    writer.append(key, value); 
    } 
    } finally { 
    IOUtils.closeStream(writer); 
    } 
}} 

시퀀스 파일에 쓰기에 대한 자세한 내용은 Hadoop-The Definitive Guide (O'Reilly 출판물)를 참조하십시오.