Todos os produtos
Search
Central de documentação

MaxCompute:Gerencie volumes externos usando o SDK for Java

Última atualização: Sep 17, 2026

Este tópico descreve como gerenciar volumes externos com o SDK for Java.

Dependência do projeto

Para gerenciar volumes externos com o SDK, adicione a seguinte dependência:

<dependency>
  <groupId>com.aliyun.odps</groupId>
  <artifactId>odps-sdk-core</artifactId>
  <version>${Latest Version}</version>
</dependency>
Importante

No código anterior, Latest Version é um espaço reservado. Substitua-o pelo número de versão específico do SDK para compilar o projeto corretamente. Consulte as versões lançadas do odps-sdk-core no Repositório Central do Maven. Recomendamos usar a versão mais recente. O número da versão segue o formato X.X.X-public.

Crie um volume externo

  • Crie

    /**
    * 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 OdpsException
    
  • Exemplo

    /*
     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);  
    }

Obter informações de um volume externo específico

  • Obtenha

    /**
    * 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)
    
  • Exemplo

    /*
    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;
    }

Obter um iterador de volume externo

  • Iterador

    /**
    * 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)
  • Exemplo

    /*
    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;
    }

Exclua um volume externo

  • Exclua

    /**
    * 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)
  • Exemplo

    /*
    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);
        }
    }