opendal_core/services/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 pub enable_append: bool,
42 pub atomic_write_dir: Option<String>,
44}
45
46impl Debug for HdfsConfig {
47 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
48 f.debug_struct("HdfsConfig")
49 .field("root", &self.root)
50 .field("name_node", &self.name_node)
51 .field(
52 "kerberos_ticket_cache_path",
53 &self.kerberos_ticket_cache_path,
54 )
55 .field("user", &self.user)
56 .field("enable_append", &self.enable_append)
57 .field("atomic_write_dir", &self.atomic_write_dir)
58 .finish_non_exhaustive()
59 }
60}
61
62impl crate::Configurator for HdfsConfig {
63 type Builder = HdfsBuilder;
64
65 fn from_uri(uri: &crate::types::OperatorUri) -> crate::Result<Self> {
66 let mut map = uri.options().clone();
67 if let Some(authority) = uri.authority() {
68 map.insert("name_node".to_string(), format!("hdfs://{authority}"));
69 }
70
71 if let Some(root) = uri.root() {
72 if !root.is_empty() {
73 map.insert("root".to_string(), root.to_string());
74 }
75 }
76
77 Self::from_iter(map)
78 }
79
80 fn into_builder(self) -> Self::Builder {
81 HdfsBuilder { config: self }
82 }
83}
84
85#[cfg(test)]
86mod tests {
87 use super::*;
88 use crate::Configurator;
89 use crate::types::OperatorUri;
90
91 #[test]
92 fn from_uri_sets_name_node_and_root() {
93 let uri = OperatorUri::new(
94 "hdfs://cluster.local:8020/user/data",
95 Vec::<(String, String)>::new(),
96 )
97 .unwrap();
98
99 let cfg = HdfsConfig::from_uri(&uri).unwrap();
100 assert_eq!(cfg.name_node.as_deref(), Some("hdfs://cluster.local:8020"));
101 assert_eq!(cfg.root.as_deref(), Some("user/data"));
102 }
103
104 #[test]
105 fn from_uri_allows_missing_authority() {
106 let uri = OperatorUri::new("hdfs", Vec::<(String, String)>::new()).unwrap();
107
108 let cfg = HdfsConfig::from_uri(&uri).unwrap();
109 assert!(cfg.name_node.is_none());
110 }
111}