opendal_service_hdfs/
config.rs1use std::fmt::Debug;
19
20use serde::Deserialize;
21use serde::Serialize;
22
23use super::backend::HdfsBuilder;
24
25#[derive(Default, Serialize, Deserialize, Clone, PartialEq, Eq)]
29#[serde(default)]
30#[non_exhaustive]
31pub struct HdfsConfig {
32 pub root: Option<String>,
34 pub name_node: Option<String>,
36 pub kerberos_ticket_cache_path: Option<String>,
38 pub user: Option<String>,
40 #[deprecated(
42 since = "0.57.0",
43 note = "HDFS append capability is enabled by default and this option is no longer needed."
44 )]
45 pub enable_append: bool,
46 pub atomic_write_dir: Option<String>,
48}
49
50impl Debug for HdfsConfig {
51 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
52 f.debug_struct("HdfsConfig")
53 .field("root", &self.root)
54 .field("name_node", &self.name_node)
55 .field(
56 "kerberos_ticket_cache_path",
57 &self.kerberos_ticket_cache_path,
58 )
59 .field("user", &self.user)
60 .field("atomic_write_dir", &self.atomic_write_dir)
61 .finish_non_exhaustive()
62 }
63}
64
65impl opendal_core::Configurator for HdfsConfig {
66 type Builder = HdfsBuilder;
67
68 fn from_uri(uri: &opendal_core::OperatorUri) -> opendal_core::Result<Self> {
69 let mut map = uri.options().clone();
70 if let Some(authority) = uri.authority() {
71 map.insert("name_node".to_string(), format!("hdfs://{authority}"));
72 }
73
74 if let Some(root) = uri.root()
75 && !root.is_empty()
76 {
77 map.insert("root".to_string(), root.to_string());
78 }
79
80 Self::from_iter(map)
81 }
82
83 #[allow(deprecated)]
84 fn into_builder(self) -> Self::Builder {
85 HdfsBuilder { config: self }
86 }
87}
88
89#[cfg(test)]
90mod tests {
91 use super::*;
92 use opendal_core::Configurator;
93 use opendal_core::OperatorUri;
94
95 #[test]
96 fn from_uri_sets_name_node_and_root() {
97 let uri = OperatorUri::new(
98 "hdfs://cluster.local:8020/user/data",
99 Vec::<(String, String)>::new(),
100 )
101 .unwrap();
102
103 let cfg = HdfsConfig::from_uri(&uri).unwrap();
104 assert_eq!(cfg.name_node.as_deref(), Some("hdfs://cluster.local:8020"));
105 assert_eq!(cfg.root.as_deref(), Some("user/data"));
106 }
107
108 #[test]
109 fn from_uri_allows_missing_authority() {
110 let uri = OperatorUri::new("hdfs", Vec::<(String, String)>::new()).unwrap();
111
112 let cfg = HdfsConfig::from_uri(&uri).unwrap();
113 assert!(cfg.name_node.is_none());
114 }
115}