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
79impl OpendalSinkBackend for WebhdfsSink {
80 type Properties = WebhdfsConfig;
81
82 const SINK_NAME: &'static str = WEBHDFS_SINK;
83
84 fn from_btreemap(btree_map: BTreeMap<String, String>) -> Result<Self::Properties> {
85 let config =
86 serde_json::from_value::<WebhdfsConfig>(serde_json::to_value(btree_map).unwrap())
87 .map_err(|e| SinkError::Config(anyhow!(e)))?;
88 if config.r#type != SINK_TYPE_APPEND_ONLY && config.r#type != SINK_TYPE_UPSERT {
89 return Err(SinkError::Config(anyhow!(
90 "`{}` must be {}, or {}",
91 SINK_TYPE_OPTION,
92 SINK_TYPE_APPEND_ONLY,
93 SINK_TYPE_UPSERT
94 )));
95 }
96 Ok(config)
97 }
98
99 fn new_operator(properties: WebhdfsConfig) -> Result<Operator> {
100 FileSink::<WebhdfsSink>::new_webhdfs_sink(properties)
101 }
102
103 fn get_path(properties: Self::Properties) -> String {
104 properties.common.path
105 }
106
107 fn get_engine_type() -> super::opendal_sink::EngineType {
108 super::opendal_sink::EngineType::Webhdfs
109 }
110
111 fn get_batching_strategy(properties: Self::Properties) -> BatchingStrategy {
112 BatchingStrategy {
113 max_row_count: properties.batching_strategy.max_row_count,
114 rollover_seconds: properties.batching_strategy.rollover_seconds,
115 path_partition_prefix: properties.batching_strategy.path_partition_prefix,
116 }
117 }
118}