2013-12-19 5 views
3

에 Databag 전달 나는 장소에 대한 몇 가지 데이터를로드하는 스크립트가 있습니다. 돼지 UDF 생성자

그래서 나는 그런이 UDF 정의하려고 :

DEFINE GenerateVenues org.gla.anton.udf.main.GenerateVenues(venues); 

을 그리고 여기에 실제 UDF입니다 :

public class GenerateVenues extends EvalFunc<Tuple> { 

    TupleFactory mTupleFactory = TupleFactory.getInstance(); 
    BagFactory mBagFactory = BagFactory.getInstance(); 

    private static final String ALLCHARS = "(.*)"; 
    private ArrayList<String> venues; 

    private String regex; 

    public GenerateVenues(DataBag venuesBag) { 
     Iterator<Tuple> it = venuesBag.iterator(); 
     venues = new ArrayList<String>((int) (venuesBag.size() + 1)); // possible fails!!! 
     String current = ""; 
     regex = ""; 
     while (it.hasNext()){ 
      Tuple t = it.next(); 
      try { 
       current = "(" + ALLCHARS + t.get(0) + ALLCHARS + ")"; 
       venues.add((String) t.get(0)); 
      } catch (ExecException e) { 
       throw new IllegalArgumentException("VenuesRegex: requires tuple with at least one value"); 
      } 
      regex += current + (it.hasNext() ? "|" : ""); 
     } 
    } 

    @Override 
    public Tuple exec(Tuple tuple) throws IOException { 
     // expect one string 
     if (tuple == null || tuple.size() != 2) { 
      throw new IllegalArgumentException(
        "BagTupleExampleUDF: requires two input parameters."); 
     } 
     try { 
      String tweet = (String) tuple.get(0); 
      for (String venue: venues) 
      { 
       if (tweet.matches(ALLCHARS + venue + ALLCHARS)) 
       { 
        Tuple output = mTupleFactory.newTuple(Collections.singletonList(venue)); 
        return output; 
       } 
      } 
      return null; 
     } catch (Exception e) { 
      throw new IOException(
        "BagTupleExampleUDF: caught exception processing input.", e); 
     } 
    } 
} 

스크립트가 단지 (venues); 전에 DEFINE 부분에 오류가 발사되어 실행되는 경우

2013-12-19 04:28:06,072 [main] ERROR org.apache.pig.tools.grunt.Grunt - ERROR 1200: <file script.pig, line 6, column 60> mismatched input 'venues' expecting RIGHT_PAREN 

명백하게 나는 잘못된 것을하고있다. ca n 당신이 무엇이 잘못되었는지 알아내는 것을 도와줍니다. 장소 관계를 매개 변수로 허용 할 수없는 UDF입니까? 또는이 관계가 DataBag (이 public GenerateVenues(DataBag venuesBag)과 같은)으로 표시되지 않습니까? 감사합니다.

추신 : 나는 돼지 버전 0.11.1.1.3.0.0-107을 사용하고 있습니다.

답변

0

UDF 생성자에서 매개 변수로 관계를 사용할 수 없습니다. 인수로만 문자열을 전달할 수 있으며, 실제로 다른 유형 인 경우에는 생성자에서 구문 분석해야합니다.

4

@WinnieNicklaus가 이미 말했듯이 은 UDF 생성자에 문자열을 전달할 수 있습니다.

귀하의 문제에 대한 해결책은 분산 캐시를 사용하고 있으므로 분산 캐시를 통해 사용할 수있는 파일 이름 목록을 반환하려면 public List<String> getCacheFiles()을 재정의해야합니다. 이를 사용하여 파일을 로컬 파일로 읽고 테이블을 빌드 할 수 있습니다.

단점은 돼지가 더 초기화 기능이 없다는 것입니다, 그래서 당신은 전화 후

private void init() { 
    if (!this.initialized) { 
     // read table 
    } 
} 

같은 것을 구현해야한다는 exec에서 첫 번째 일이있다.