This topic describes how to manage external volumes using the SDK for Java.
Project dependency
Before you can use the SDK to manage external volumes, you must add the following dependency.
<dependency>
<groupId>com.aliyun.odps</groupId>
<artifactId>odps-sdk-core</artifactId>
<version>${Latest Version}</version>
</dependency>Important
In the preceding code, Latest Version is a placeholder. You must replace the placeholder with a specific SDK version number for the project to compile. You can find the released versions of odps-sdk-core in the Maven Central Repository. We recommend that you use the latest version. The version number is in the X.X.X-public format.
Create an external volume
Create
/** * Creates an external volume. * @param builder The parameter configuration of the volume. * @throws OdpsException */ public void create(VolumeBuilder builder) throws OdpsException /** * Creates an external volume. * * @param volumeName The name of the volume. * @param comment * @param type To create a legacy volume, pass {@link Volume}.Type.Old. To create a volume for the new VolumeFS feature, pass {@link Volume} * .Type.New. The VolumeFS feature can be used only after it is enabled for the project. * @throws OdpsException */ public void create(String volumeName, String comment, Volume.Type type) /** * Creates an external volume. * * @param projectName The name of the project to which the destination external volume belongs. * @param volumeName The name of the volume to be created. * @param comment * @param type To create a legacy volume, pass {@link Volume}.Type.Old. To create a volume for the new VolumeFS feature, pass {@link Volume} * .Type.New. The VolumeFS feature can be used only after it is enabled for the project. * @throws OdpsException */ public void create(String projectName, String volumeName, String comment, Volume.Type type) /** * Creates a volume. * * @param projectName The name of the project to which the destination external volume belongs. * @param volumeName The name of the volume to be created. * @param comment * @param type To create a legacy volume, pass {@link Volume}.Type.Old. To create a volume for the new VolumeFS feature, pass {@link Volume} * .Type.New. The VolumeFS feature can be used only after it is enabled for the project. * @param lifecycle The lifecycle. * @throws OdpsException */ public void create(String projectName, String volumeName, String comment, Volume.Type type, Long lifecycle) throws OdpsExceptionExample
/* 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); }
Get the information about a specified external volume
Get
/** * Obtains the information about a specified external volume. * * @param volumeName The name of the volume. * @return */ public Volume get(String volumeName) /** * Obtains the information about a specified external volume. * * @param projectName The name of the project. * @param volumeName The name of the external volume. * @return */ public Volume get(String projectName, String volumeName)Example
/* Obtain the information about an external volume. */ public Volume getVolume() { String projectName = "test_project"; String extVolumeName = "test_ext_1"; Volume volume = odps.volumes().get(projectName, extVolumeName); return volume; }
Get an external volume iterator
Iterator
/** * Obtains an iterator for all volumes in the default project. * * @return A volume iterator. */ public Iterator<Volume> iterator() /** * Obtains a volume iterator. * * @param projectName The name of the project. * @return A volume iterator. */ public Iterator<Volume> iterator(final String projectName) /** * Obtains a volume iterator. * * @param filter The filter condition. * @return A volume iterator. */ public Iterator<Volume> iterator(final VolumeFilter filter) /** * Obtains a volume iterator. * * @param projectName The name of the project. * @param filter The filter condition. * @return A volume iterator. */ public Iterator<Volume> iterator(final String projectName, final VolumeFilter filter)Example
/* Obtain a 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
/** * Deletes an external volume. * * @param volumeName The name of the external volume. * @throws OdpsException */ public void delete(String volumeName) /** * Deletes an external volume. * * @param projectName The project to which the external volume belongs. * @param volumeName The 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); } }