This topic describes how to manage external volumes by using the SDK for Java.
Project dependency
Before you manage external volumes by using the SDK for Java, you need to add the following dependency:
<dependency>
<groupId>com.aliyun.odps</groupId>
<artifactId>odps-sdk-core</artifactId>
<version>0.44.0-SNAPSHOT</version>
</dependency>
Create an external volume
Create
/** * Create an external volume. * @param builder Parameter settings of the external volume * @throws OdpsException */ public void create(VolumeBuilder builder) throws OdpsException /** * Create an external volume. * * @param volumeName Name of the external volume * @param comment * @param type Create the original external volume {@link Volume}.Type.Old,Create the external volume for the VolumeFS feature{@link Volume} *. Type.New. The VolumeFS feature can be used only if the feature is enabled for the project. * @throws OdpsException */ public void create(String volumeName, String comment, Volume.Type type) /** * Create an external volume. * * @param projectName Name of the project to which the external volume belongs * @param volumeName Name of the external volume * @param comment * @param type Create the original external volume {@link Volume}.Type.Old,Create the external volume for the VolumeFS feature{@link Volume} *. Type.New. The VolumeFS feature can be used only if the feature is enabled for the project. * @throws OdpsException */ public void create(String projectName, String volumeName, String comment, Volume.Type type) /** * Create an external volume. * * @param projectName Name of the project to which the external volume belongs * @param volumeName Name of the external volume * @param comment * @param type Create the original external volume {@link Volume}.Type.Old,Create the external volume for the VolumeFS feature{@link Volume} *. Type.New. The VolumeFS feature can be used only if the feature is enabled for the project. * @param lifecycle Lifecycle * @throws OdpsException */ public void create(String projectName, String volumeName, String comment, Volume.Type type, Long lifecycle) throws OdpsException
Example
/* Create an external volume. */ public void createExternalVolume() throws Exception { String projectName = "test_project"; String extVolumeName = "test_ext_1"; String externalLocation = "oss://..."; Long lifecycle; String comment; String roleArn; Volumes.VolumeBuilder builder = new Volumes.VolumeBuilder(); builder.project(projectName).volumeName(extVolumeName).type(Volume.Type.EXTERNAL).extLocation(externalLocation); if (lifecycle != null) { builder.lifecycle(lifecycle); } if (comment != null) { builder.comment(comment); } if (roleArn != null) { builder.addProperty(Volumes.EXTERNAL_VOLUME_ROLEARN_KEY, roleArn); } getCurrentOdps().volumes().create(builder); }
Obtain the information about a specified external volume
Get
/** * Obtain the information about a specified external volume. * * @param volumeName Name of the external volume * @return */ public Volume get(String volumeName) /** * Obtain the information about a specified external volume. * * @param projectName Name of the project * @param volumeName Name of the external volume * @return */ public Volume get(String projectName, String volumeName)
Example
/* Obtain the information about a specified external volume. */ public Volume getVolume() { String projectName = "test_project"; String extVolumeName = "test_ext_1"; Volume volume = odps.volumes().get(projectName, extVolumeName); return volume; }
Obtain an external volume iterator
Iterator
/** * Obtain all external volume iterators of the default project. * * @return Volume iterator */ public Iterator<Volume> iterator() /** * Obtain the external volume iterator * * @param projectName Name of the project * @return Volume iterator */ public Iterator<Volume> iterator(final String projectName) /** * Obtain the external volume iterator. * * @param filter Filter condition * @return Volume iterator */ public Iterator<Volume> iterator(final VolumeFilter filter) /** * Obtain the external volume iterator. * * @param projectName Name of the project * @param filter Filter condition * @return Volume iterator */ public Iterator<Volume> iterator(final String projectName, final VolumeFilter filter)
Example
/* Obtain an external volume iterator. */ public Iterator<Volume> getVolumeIterator() { String projectName = "test_project"; String extVolumeName = "test_ext_1"; VolumeFilter volumeFilter = new VolumeFilter(); volumeFilter.setName(extVolumeName); Iterator<Volume> iterator = odps.volumes().iterator(projectName, volumeFilter); return iterator; }
Delete an external volume
Delete
/** * Delete an external volume. * * @param volumeName Name of the external volume * @throws OdpsException */ public void delete(String volumeName) /** * Delete an external volume. * * @param projectName Name of the project to which the external volume belongs * @param volumeName Name of the external volume * @throws OdpsException */ public void delete(String projectName, String volumeName)
Example
/* Delete an external volume. */ public void deleteVolume() { String projectName = "test_project"; String extVolumeName = "test_ext_1"; try { odps.volumes().delete(projectName, extVolumeName); } catch (OdpsException e) { throw new RuntimeException(e); } }