risingwave_connector/sink/file_sink/
webhdfs.rs1use std::collections::{BTreeMap, HashMap};
16
17use anyhow::anyhow;
18use opendal::Operator;
19use opendal::layers::LoggingLayer;
20use opendal::services::Webhdfs;
21use serde::Deserialize;
22use serde_with::serde_as;
23use with_options::WithOptions;
24
25use super::opendal_sink::{BatchingStrategy, FileSink};
26use crate::sink::file_sink::opendal_sink::OpendalSinkBackend;
27use crate::sink::{Result, SINK_TYPE_APPEND_ONLY, SINK_TYPE_OPTION, SINK_TYPE_UPSERT, SinkError};
28use crate::source::UnknownFields;
29#[derive(Deserialize, Debug, Clone, WithOptions)]
30pub struct WebhdfsCommon {
31 #[serde(rename = "webhdfs.endpoint")]
32 pub endpoint: String,
33 #[serde(rename = "webhdfs.path")]
35 pub path: String,
36}
37
38#[serde_as]
39#[derive(Clone, Debug, Deserialize, WithOptions)]
40pub struct WebhdfsConfig {
41 #[serde(flatten)]
42 pub common: WebhdfsCommon,
43
44 #[serde(flatten)]
45 pub batching_strategy: BatchingStrategy,
46
47 pub r#type: String, #[serde(flatten)]
50 pub unknown_fields: HashMap<String, String>,
51}
52
53pub const WEBHDFS_SINK: &str = "webhdfs";
54
55impl<S: OpendalSinkBackend> FileSink<S> {
56 pub fn new_webhdfs_sink(config: WebhdfsConfig) -> Result<Operator> {
57 let builder = Webhdfs::default()
59 .endpoint(&config.common.endpoint)
60 .root(&config.common.path);
61
62 let operator: Operator = Operator::new(builder)?
63 .layer(LoggingLayer::default())
64 .finish();
65
66 Ok(operator)
67 }
68}
69
70#[derive(Debug, Clone, Copy, PartialEq, Eq)]
71pub struct WebhdfsSink;
72
73impl UnknownFields for WebhdfsConfig {
74 fn unknown_fields(&self) -> HashMap<String, String> {
75 self.unknown_fields.clone()
76 }
77}
78
79crate::impl_sink_unknown_fields!(WebhdfsConfig);
80
81impl OpendalSinkBackend for WebhdfsSink {
82 type Properties = WebhdfsConfig;
83
84 const SINK_NAME: &'static str = WEBHDFS_SINK;
85
86 fn from_btreemap(btree_map: BTreeMap<String, String>) -> Result<Self::Properties> {
87 let config =
88 serde_json::from_value::<WebhdfsConfig>(serde_json::to_value(btree_map).unwrap())
89 .map_err(|e| SinkError::Config(anyhow!(e)))?;
90 if config.r#type != SINK_TYPE_APPEND_ONLY && config.r#type != SINK_TYPE_UPSERT {
91 return Err(SinkError::Config(anyhow!(
92 "`{}` must be {}, or {}",
93 SINK_TYPE_OPTION,
94 SINK_TYPE_APPEND_ONLY,
95 SINK_TYPE_UPSERT
96 )));
97 }
98 Ok(config)
99 }
100
101 fn new_operator(properties: WebhdfsConfig) -> Result<Operator> {
102 FileSink::<WebhdfsSink>::new_webhdfs_sink(properties)
103 }
104
105 fn get_path(properties: Self::Properties) -> String {
106 properties.common.path
107 }
108
109 fn get_engine_type() -> super::opendal_sink::EngineType {
110 super::opendal_sink::EngineType::Webhdfs
111 }
112
113 fn get_batching_strategy(properties: Self::Properties) -> BatchingStrategy {
114 BatchingStrategy {
115 max_row_count: properties.batching_strategy.max_row_count,
116 rollover_seconds: properties.batching_strategy.rollover_seconds,
117 path_partition_prefix: properties.batching_strategy.path_partition_prefix,
118 }
119 }
120}