|
17 | 17 |
|
18 | 18 | package feast.ingestion.transform; |
19 | 19 |
|
| 20 | +import static com.google.common.base.Preconditions.checkArgument; |
20 | 21 | import static feast.ingestion.util.JsonUtil.convertJsonStringToMap; |
21 | 22 |
|
22 | 23 | import com.google.inject.Inject; |
23 | 24 | import feast.ingestion.model.Specs; |
24 | 25 | import feast.ingestion.options.ImportJobOptions; |
25 | 26 | import feast.ingestion.transform.FeatureIO.Write; |
26 | | -import feast.ingestion.transform.fn.LoggerDoFn; |
27 | 27 | import feast.specs.StorageSpecProto.StorageSpec; |
28 | 28 | import feast.storage.ErrorsStore; |
29 | 29 | import feast.storage.noop.NoOpIO; |
30 | 30 | import feast.types.FeatureRowExtendedProto.FeatureRowExtended; |
31 | 31 | import java.util.List; |
32 | 32 | import lombok.extern.slf4j.Slf4j; |
33 | | -import org.apache.beam.sdk.transforms.ParDo; |
34 | 33 | import org.apache.beam.sdk.values.PCollection; |
35 | 34 | import org.apache.beam.sdk.values.PDone; |
36 | | -import org.slf4j.event.Level; |
| 35 | +import org.apache.hadoop.hbase.util.Strings; |
37 | 36 |
|
38 | 37 | @Slf4j |
39 | 38 | public class ErrorsStoreTransform extends FeatureIO.Write { |
40 | 39 |
|
41 | | - public static final String ERRORS_STORE_STDERR = "stderr"; |
42 | | - public static final String ERRORS_STORE_STDOUT = "stdout"; |
43 | | - public static final String ERRORS_STORE_JSON = "file.json"; |
44 | | - |
45 | 40 | private String errorsStoreType; |
46 | 41 | private StorageSpec errorsStoreSpec; |
47 | | - private ErrorsStore errorsStore; |
48 | 42 | private Specs specs; |
| 43 | + private List<ErrorsStore> errorsStores; |
49 | 44 |
|
50 | 45 | @Inject |
51 | 46 | public ErrorsStoreTransform( |
52 | 47 | ImportJobOptions options, Specs specs, List<ErrorsStore> errorsStores) { |
53 | 48 | this.specs = specs; |
| 49 | + this.errorsStores = errorsStores; |
54 | 50 | this.errorsStoreType = options.getErrorsStoreType(); |
55 | 51 |
|
56 | | - for (ErrorsStore errorsStore : errorsStores) { |
57 | | - if (errorsStore.getType().equals(errorsStoreType)) { |
58 | | - this.errorsStore = errorsStore; |
59 | | - } |
| 52 | + if (!Strings.isEmpty(errorsStoreType)) { |
| 53 | + this.errorsStoreSpec = |
| 54 | + StorageSpec.newBuilder() |
| 55 | + .setType(errorsStoreType) |
| 56 | + .putAllOptions(convertJsonStringToMap(options.getErrorsStoreOptions())) |
| 57 | + .build(); |
60 | 58 | } |
61 | | - |
62 | | - this.errorsStoreSpec = |
63 | | - StorageSpec.newBuilder() |
64 | | - .setType(errorsStoreType) |
65 | | - .putAllOptions(convertJsonStringToMap(options.getErrorsStoreOptions())) |
66 | | - .build(); |
67 | 59 | } |
68 | 60 |
|
69 | 61 | @Override |
70 | 62 | public PDone expand(PCollection<FeatureRowExtended> input) { |
71 | | - switch (errorsStoreType) { |
72 | | - case ERRORS_STORE_STDOUT: |
73 | | - input.apply("Log errors to STDOUT", ParDo.of(new LoggerDoFn(Level.INFO))); |
74 | | - break; |
75 | | - case ERRORS_STORE_STDERR: |
76 | | - input.apply("Log errors to STDERR", ParDo.of(new LoggerDoFn(Level.ERROR))); |
77 | | - break; |
78 | | - default: |
79 | | - if (errorsStore == null) { |
80 | | - log.warn("No valid errors store specified, errors will be discarded"); |
81 | | - return input.apply(new NoOpIO.Write()); |
82 | | - } |
83 | | - Write write = errorsStore.create(this.errorsStoreSpec, specs); |
84 | | - return input.apply(write); |
| 63 | + Write write; |
| 64 | + if (Strings.isEmpty(errorsStoreType)) { |
| 65 | + write = new NoOpIO.Write(); |
| 66 | + } else { |
| 67 | + write = getErrorStore().create(this.errorsStoreSpec, specs); |
85 | 68 | } |
| 69 | + input.apply("errors to " + String.valueOf(errorsStoreType), write); |
86 | 70 | return PDone.in(input.getPipeline()); |
87 | 71 | } |
| 72 | + |
| 73 | + ErrorsStore getErrorStore() { |
| 74 | + checkArgument(!errorsStoreType.isEmpty(), "Errors store type not provided"); |
| 75 | + for (ErrorsStore errorsStore : errorsStores) { |
| 76 | + if (errorsStore.getType().equals(errorsStoreType)) { |
| 77 | + return errorsStore; |
| 78 | + } |
| 79 | + } |
| 80 | + throw new IllegalArgumentException("Errors store type not found"); |
| 81 | + } |
88 | 82 | } |
0 commit comments