Updated on 2022-12-08 GMT+08:00

Setting Storage Policies

Function

Specify storage policies for a file or folder in the HDFS.

Example Code

  1. Log in to the FusionInsight Manager portal, and choose Cluster > Name of the desired cluster > Services > HDFS > Configurations > All Configurations.
  2. Check whether the value of dfs.storage.policy.enabled is the default value true. If not, modify the value to true, click Save, and restart HDFS.
  3. Check the code.

    The following code segment is only an example. For details, see the HdfsExample class in com.huawei.bigdata.hdfs.examples.

    /** 
       * set storage policy to path 
       * @param policyName 
       * Policy Name can be accepted: 
       * <li>HOT 
       * <li>WARM 
       * <li>COLD 
       * <li>LAZY_PERSIST 
       * <li>ALL_SSD 
       * <li>ONE_SSD 
       * @throws IOException  
       */ 
    private void setStoragePolicy(String policyName) throws IOException {
        if (fSystem instanceof DistributedFileSystem) {
            DistributedFileSystem dfs = (DistributedFileSystem) fSystem;
            Path destPath = new Path(DEST_PATH);
            Boolean flag = false;
            mkdir();
            BlockStoragePolicySpi[] storage = dfs.getStoragePolicies();
            for (BlockStoragePolicySpi bs : storage) {
                if (bs.getName().equals(policyName)) {
                    flag = true;
                }
                LOG.info("StoragePolicy:" + bs.getName());
            }
            if (!flag) {
                policyName = storage[0].getName();
            }
            dfs.setStoragePolicy(destPath, policyName);
            LOG.info("success to set Storage Policy path " + DEST_PATH);
            rmdir();
        } else {
            LOG.info("SmallFile not support to set Storage Policy !!!");
        }
    }