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