Skip to main content

opendal_service_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    /// Deprecated: HDFS append capability is enabled by default.
41    #[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    /// atomic_write_dir of this backend
47    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}