opendal_core/services/hdfs/
config.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use std::fmt::Debug;
19
20use serde::Deserialize;
21use serde::Serialize;
22
23use super::backend::HdfsBuilder;
24
25/// [Hadoop Distributed File System (HDFS™)](https://hadoop.apache.org/) support.
26///
27/// Config for Hdfs services support.
28#[derive(Default, Serialize, Deserialize, Clone, PartialEq, Eq)]
29#[serde(default)]
30#[non_exhaustive]
31pub struct HdfsConfig {
32    /// work dir of this backend
33    pub root: Option<String>,
34    /// name node of this backend
35    pub name_node: Option<String>,
36    /// kerberos_ticket_cache_path of this backend
37    pub kerberos_ticket_cache_path: Option<String>,
38    /// user of this backend
39    pub user: Option<String>,
40    /// enable the append capacity
41    pub enable_append: bool,
42    /// atomic_write_dir of this backend
43    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}