Setting Storage Policies
Function Description
Specify storage policies for a file or folder in HDFS.
Sample Code
- Set the following parameters in the Hdfs-site.xml file in ${HADOOP_HOME}/etc/hadoop/.
<name>dfs.storage.policy.enabled</name> <value>true</value>
- Restart HDFS, as shown in Figure 1.
- Log in to MRS Manager. Choose , and set Type to All.
- Check whether the value of dfs.storage.policy.enabled is true. If it is not, modify the value to true, click Save Configuration, and restart HDFS.
- View the code.
The following provides code snippets. For complete codes, see the HdfsMain class in com.huawei.bigdata.hdfs.examples.
/** * Set storage policies. * @param policyName * The 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; } System.out.println("StoragePolicy:" + bs.getName()); } if (!flag) { policyName = storage[0].getName(); } dfs.setStoragePolicy(destPath, policyName); System.out.println("success to set Storage Policy path " + DEST_PATH); rmdir(); } else{ System.out.println("Storage Policy is only supported in HDFS."); } }
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.