001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.hadoop.hdfs.client;
019    
020    import java.io.IOException;
021    import java.net.URI;
022    
023    import org.apache.hadoop.classification.InterfaceAudience;
024    import org.apache.hadoop.classification.InterfaceStability;
025    import org.apache.hadoop.conf.Configuration;
026    import org.apache.hadoop.fs.FileSystem;
027    import org.apache.hadoop.fs.Path;
028    import org.apache.hadoop.hdfs.DistributedFileSystem;
029    import org.apache.hadoop.hdfs.protocol.HdfsConstants;
030    import org.apache.hadoop.hdfs.tools.DFSAdmin;
031    
032    /**
033     * The public API for performing administrative functions on HDFS. Those writing
034     * applications against HDFS should prefer this interface to directly accessing
035     * functionality in DistributedFileSystem or DFSClient.
036     * 
037     * Note that this is distinct from the similarly-named {@link DFSAdmin}, which
038     * is a class that provides the functionality for the CLI `hdfs dfsadmin ...'
039     * commands.
040     */
041    @InterfaceAudience.Public
042    @InterfaceStability.Evolving
043    public class HdfsAdmin {
044      
045      private DistributedFileSystem dfs;
046      
047      /**
048       * Create a new HdfsAdmin client.
049       * 
050       * @param uri the unique URI of the HDFS file system to administer
051       * @param conf configuration
052       * @throws IOException in the event the file system could not be created
053       */
054      public HdfsAdmin(URI uri, Configuration conf) throws IOException {
055        FileSystem fs = FileSystem.get(uri, conf);
056        if (!(fs instanceof DistributedFileSystem)) {
057          throw new IllegalArgumentException("'" + uri + "' is not an HDFS URI.");
058        } else {
059          dfs = (DistributedFileSystem)fs;
060        }
061      }
062      
063      /**
064       * Set the namespace quota (count of files, directories, and sym links) for a
065       * directory.
066       * 
067       * @param src the path to set the quota for
068       * @param quota the value to set for the quota
069       * @throws IOException in the event of error
070       */
071      public void setQuota(Path src, long quota) throws IOException {
072        dfs.setQuota(src, quota, HdfsConstants.QUOTA_DONT_SET);
073      }
074      
075      /**
076       * Clear the namespace quota (count of files, directories and sym links) for a
077       * directory.
078       * 
079       * @param src the path to clear the quota of
080       * @throws IOException in the event of error
081       */
082      public void clearQuota(Path src) throws IOException {
083        dfs.setQuota(src, HdfsConstants.QUOTA_RESET, HdfsConstants.QUOTA_DONT_SET);
084      }
085      
086      /**
087       * Set the disk space quota (size of files) for a directory. Note that
088       * directories and sym links do not occupy disk space.
089       * 
090       * @param src the path to set the space quota of
091       * @param spaceQuota the value to set for the space quota
092       * @throws IOException in the event of error
093       */
094      public void setSpaceQuota(Path src, long spaceQuota) throws IOException {
095        dfs.setQuota(src, HdfsConstants.QUOTA_DONT_SET, spaceQuota);
096      }
097      
098      /**
099       * Clear the disk space quota (size of files) for a directory. Note that
100       * directories and sym links do not occupy disk space.
101       * 
102       * @param src the path to clear the space quota of
103       * @throws IOException in the event of error
104       */
105      public void clearSpaceQuota(Path src) throws IOException {
106        dfs.setQuota(src, HdfsConstants.QUOTA_DONT_SET, HdfsConstants.QUOTA_RESET);
107      }
108      
109      /**
110       * Allow snapshot on a directory.
111       * @param path The path of the directory where snapshots will be taken.
112       */
113      public void allowSnapshot(Path path) throws IOException {
114        dfs.allowSnapshot(path);
115      }
116      
117      /**
118       * Disallow snapshot on a directory.
119       * @param path The path of the snapshottable directory.
120       */
121      public void disallowSnapshot(Path path) throws IOException {
122        dfs.disallowSnapshot(path);
123      }
124    }