0

클라우드 기능에서 데이터 흐름 템플릿으로 전달되는 속성 값을 가져 오려고합니다. 전달되는 값이 래퍼이므로 컴파일 중에 오류가 발생하며 .get() 메서드를 사용하면 오류가 발생합니다. 이 오류와 함께 An exception occurred while executing the Java class. null: InvocationTargetException: Not called from a runtime context.ValueProvider 문제

public interface MyOptions extends DataflowPipelineOptions { 
... 
@Description("schema of csv file") 
ValueProvider<String> getHeader(); 
void setHeader(ValueProvider<String> header); 
... 
} 

public static void main(String[] args) throws IOException { 
... 
    List<String> sideInputColumns = Arrays.asList(options.getHeader().get().split(",")); 
... 
    //ultimately use the getHeaders as side inputs 
    PCollection<String> input = p.apply(Create.of(sideInputColumns)); 
    final PCollectionView<List<String>> finalColumnView = input.apply(View.asList()); 
} 

은 어떻게 ValueProvider 유형의 값을 추출 할 수 있습니까?

+0

코드를 공유하십시오. –

답변

1

파이프 라인 구성 중에 ValueProvider 값을 사용할 수 없습니다. 따라서 항상 동일한 구조를 가지도록 파이프 라인을 구성하고 ValueProvider을 직렬화해야합니다. 런타임시 파이프 라인 내의 개별 변환은 값을 검사하여 작동 방법을 결정할 수 있습니다.

예제에 따라 다음과 같이해야 할 수도 있습니다. 그것은 하나의 요소를 만든 다음 헤더 확장을 런타임에 평가되는 DoFn을 사용

public static class HeaderDoFn extends DoFn<String, String> { 
    private final ValueProvider<String> header; 
    public HeaderDoFn(ValueProvider<String> header) { 
    this.header = header; 
    } 

    @ProcessElement 
    public void processElement(ProcessContext c) { 
    // Ignore input element -- there should be exactly one 
    for (String column : this.header().get().split(",")) { 
     c.output(column); 
    } 
    } 
} 

public static void main(String[] args) throws IOException { 
    PCollection<String> input = p 
    .apply(Create.of("one")) // create a single element 
    .apply(ParDo.of(new DoFn<String, String>() { 
     @ProcessElement 
     public void processElement(ProcessContext c) { 
     } 
    }); 

    // Note that the order of this list is not guaranteed. 
    final PCollectionView<List<String>> finalColumnView = 
    input.apply(View.asList());   
} 

또 다른 옵션은 옵션에서 ValueProvider<List<String>>을 만들 NestedValueProvider을 사용하고, 통과 될 것이라고 ValueProvider<List<String>>받는 사람을 사이드 입력을 사용하는 것보다 DoFn이 필요합니다.

+0

값 공급자가있는 템플릿을 만드는 데 google 사이트에 갔는데 구조를 구현하는 데 문제가 있습니다. MyFn에 옵션을 전달하면 'package.this'가 정적 컨텍스트에서 참조가 될 수 없다는 오류가 발생합니다. '[link] (https://cloud.google.com/dataflow/docs/templates/creating-templates). 내가 도대체 ​​뭘 잘못하고있는 겁니까? – Vlad

+0

유스 케이스에 대한 두 가지 가능성을 제공하는 답변을 업데이트했습니다. –

+0

감사합니다. @Ben! 코드의 도움을 받아 값을 추출 할 수있었습니다. 난 단지 내가 필요로하는 것을 위해 그것을 사용할 필요가있다. – Vlad