2012-03-17 1 views
13

파일 이름의 일부로 날짜 스탬프가 포함 된 파일 집합에서 데이터를 처리 중입니다. 파일 내의 데이터에는 날짜 스탬프가 없습니다. 파일 이름을 처리하고 스크립트 내의 데이터 구조 중 하나에 추가하고 싶습니다. 돼지 라틴어 (PigStorage의 확장 기능)에서 그렇게 할 수있는 방법이 있습니까? 아니면 Perl 등을 사용하여 모든 파일을 사전에 처리해야합니까?Pig Latin 스크립트에 현재 입력 파일 이름을 통합하려면 어떻게해야합니까?

-- Load two fields from file, then generate a third from the filename 
rawdata = LOAD '/directory/of/files/' USING PigStorage AS (field1:chararray, field2:int, field3:filename); 

-- Reformat the filename into a datestamp 
annotated = FOREACH rawdata GENERATE 
    REGEX_EXTRACT(field3,'*-(20\d{6})-*',1) AS datestamp, 
    field1, field2; 

참고 LOAD 문에서 특별한 "파일 이름"데이터 형식 :

는 나는 다음과 같은 일을 구상. 데이터가로드되면 소스 파일 이름으로 돌아 가기에는 너무 늦기 때문에 거기에서 일어날 필요가있는 것처럼 보입니다.

답변

13

추가 chararray 필드에 파일 이름을했다 PigStorageWithInputPath의 예로서 돼지 위키 : UDF

// Note that there are several versions of Path and FileSplit. These are intended: 
import org.apache.hadoop.fs.Path; 
import org.apache.hadoop.mapreduce.RecordReader; 
import org.apache.hadoop.mapreduce.lib.input.FileSplit; 
import org.apache.pig.backend.hadoop.executionengine.mapReduceLayer.PigSplit; 
import org.apache.pig.builtin.PigStorage; 
import org.apache.pig.data.Tuple; 

public class PigStorageWithInputPath extends PigStorage { 
    Path path = null; 

    @Override 
    public void prepareToRead(RecordReader reader, PigSplit split) { 
     super.prepareToRead(reader, split); 
     path = ((FileSplit)split.getWrappedSplit()).getPath(); 
    } 

    @Override 
    public Tuple getNext() throws IOException { 
     Tuple myTuple = super.getNext(); 
     if (myTuple != null) 
      myTuple.append(path.toString()); 
     return myTuple; 
    } 
} 
1

A = load '/directory/of/files/*' using PigStorageWithInputPath() 
    as (field1:chararray, field2:int, field3:chararray); 

배쉬에서이 작업을 수행 할 수있는 방법과 PigLatin은 How Can I Load Every File In a Folder Using PIG?에서 찾을 수 있습니다.

요즘 내가 뭘하고 있었는지, 그리고 훨씬 더 깨끗한 것으로 보이는 것은 돼지를 돼지에 삽입하는 것입니다. 그 두 가지 사이에 모든 종류의 변수 등을 던져 보겠습니다. 간단한 예입니다 : 당신이 관심이 있다면

#!/path/to/jython.jar          

# explicitly import Pig class                   
from org.apache.pig.scripting import Pig 

# COMPILE: compile method returns a Pig object that represents the pipeline       
P = Pig.compile(
       "a = load '$in'; store a into '$out';") 

input = '/path/to/some/file.txt' 
output = '/path/to/some/output/on/hdfs' 

# BIND and RUN                       
results = P.bind({'in':input, 'out':output}).runSingle() 

if results.isSuccessful() : 
    print 'Pig job succeeded' 
else : 
    raise 'Pig job failed' 

는,이에 대한 소개로 Julien Le Dem's great slides에서보세요. http://pig.apache.org/docs/r0.9.2/cont.pdf에도 많은 설명서가 있습니다.

3

-tagSource는 Pig 0.12.0에서 사용되지 않습니다. 대신에

-tagFile - 각 튜플의 시작 부분에 입력 소스 파일 이름을 추가합니다.
-tagPath - 입력 소스 파일 경로를 각 튜플의 시작 부분에 추가합니다.

A = LOAD '/user/myFile.TXT' using PigStorage(',','-tagPath'); 
DUMP A ; 

는 첫 번째 열 당신에게 전체 파일 경로를 줄 것이다

(hdfs://myserver/user/blo/input/2015.TXT,439,43,05,4,NAVI,PO,P&C,P&CR,UC,40) 

에 refrence : http://pig.apache.org/docs/r0.12.0/api/org/apache/pig/builtin/PigStorage.html