Access HDFS data in ECI

Updated at:

When you run batch jobs with tools such as Hadoop and Spark, you can use Apsara File Storage for HDFS for storage. This topic uses Spark as an example to demonstrate how to upload a file to a Hadoop Distributed File System (HDFS) and access the file from a Spark application.

Prepare data and upload it to HDFS

  1. Activate Apsara File Storage for HDFS. For more information, see Activate Apsara File Storage for HDFS.

  2. Create a file system. For more information, see Create a file system.

  3. (Optional) Create a permission group and set rules. For more information, see Manage permission groups.

  4. Create a mount target. For more information, see Manage mount targets.

  5. Install Apache Hadoop.

    1. Download Apache Hadoop.

      To download Apache Hadoop, see Hadoop Downloads. You must use Apache Hadoop 2.7.2 or later. This topic uses Apache Hadoop 2.7.2 as an example.

    2. Decompress the Apache Hadoop package to a specified folder.

      tar -zxvf hadoop-2.7.2.tar.gz -C /usr/local/
    3. Modify the core-site.xml configuration file.

      vim /usr/local/hadoop-2.7.2/etc/hadoop/core-site.xml

      Modify the core-site.xml configuration file as follows:

      <?xml version="1.0" encoding="UTF-8"?>
      <?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
      <!--
        Licensed under the Apache License, Version 2.0 (the "License");
        you may not use this file except in compliance with the License.
        You may obtain a copy of the License at
          http://www.apache.org/licenses/LICENSE-2.0
        Unless required by applicable law or agreed to in writing, software
        distributed under the License is distributed on an "AS IS" BASIS,
        WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
        See the License for the specific language governing permissions and
        limitations under the License. See accompanying LICENSE file.
      -->
      <!-- Put site-specific property overrides in this file. -->
      <configuration>
          <property>
              <name>fs.defaultFS</name>
              <value>dfs://f-4b1fcae5dv***.cn-hangzhou.dfs.aliyuncs.com:10290</value>
              <!-- Enter your HDFS mount target address. -->
          </property>
          <property>
              <name>fs.dfs.impl</name>
              <value>com.alibaba.dfs.DistributedFileSystem</value>
          </property>
          <property>
              <name>fs.AbstractFileSystem.dfs.impl</name>
              <value>com.alibaba.dfs.DFS</value>
          </property>
          <property>
              <name>io.file.buffer.size</name>
              <value>8388608</value>
          </property>
          <property>
              <name>alidfs.use.buffer.size.setting</name>
              <value>false</value>
              <!-- Keep this disabled. Enabling it reduces the I/O size and affects data throughput. -->
          </property>
          <property>
              <name>dfs.usergroupservice.impl</name>
              <value>com.alibaba.dfs.security.LinuxUserGroupService.class</value>
          </property>
          <property>
              <name>dfs.connection.count</name>
              <value>256</value>
          </property>
      </configuration>	
      Note

      Configure only the items related to HDFS. The modified core-site.xml file is used when you configure the application.

    4. Modify the /etc/profile configuration file and apply the changes.

      vim /etc/profile

      Add the environment variables as follows:

      export HADOOP_HOME=/usr/local/hadoop-2.7.2
      export HADOOP_CLASSPATH=/usr/local/hadoop-2.7.2/etc/hadoop:/usr/local/hadoop-2.7.2/share/hadoop/common/lib/*:/usr/local/hadoop-2.7.2/share/hadoop/common/*:/usr/local/hadoop-2.7.2/share/hadoop/hdfs:/usr/local/hadoop-2.7.2/share/hadoop/hdfs/lib/*:/usr/local/hadoop-2.7.2/share/hadoop/hdfs/*:/usr/local/hadoop-2.7.2/share/hadoop/yarn/lib/*:/usr/local/hadoop-2.7.2/share/hadoop/yarn/*:/usr/local/hadoop-2.7.2/share/hadoop/mapreduce/lib/*:/usr/local/hadoop-2.7.2/share/hadoop/mapreduce/*:/usr/local/hadoop-2.7.2/contrib/capacity-scheduler/*.jar
      export HADOOP_CONF_DIR=/usr/local/hadoop-2.7.2/etc/hadoop

      After you modify the file, run the following command to apply the changes:

      source /etc/profile
  6. Add the Alibaba Cloud HDFS dependency.

    cp aliyun-sdk-dfs-1.0.3.jar  /usr/local/hadoop-2.7.2/share/hadoop/hdfs

    To download the HDFS SDK, see HDFS SDK. You only need to deploy the HDFS Client. You do not need to deploy an HDFS cluster.

  7. Upload data to HDFS.

    1. Create a data catalog.

      $HADOOP_HOME/bin/hadoop fs -mkdir -p /pod/data
    2. Upload a local file to HDFS.

      The following command uses a local book as an example. Replace the file with your own.

      $HADOOP_HOME/bin/hadoop fs -put ./A-Game-of-Thrones.txt /pod/data/A-Game-of-Thrones.txt
    3. Confirm the file information.

      $HADOOP_HOME/bin/hadoop fs -ls /pod/data

Read HDFS data in a Spark application

  1. Develop the application.

    SparkConf conf = new SparkConf().setAppName(WordCount.class.getSimpleName());
    JavaRDD<String> lines = sc.textFile("dfs://f-4b1fcae5dv***.cn-hangzhou.dfs.aliyuncs.com:10290/pod/data/A-Game-of-Thrones.txt", 250);
    ...
    wordsCountResult.saveAsTextFile("dfs://f-4b1fcae5dv***.cn-hangzhou.dfs.aliyuncs.com:10290/pod/data/A-Game-of-Thrones-Result");
    sc.close();   	
  2. Configure HDFS information in the application.

    • Method 1: Use a static configuration file

      Place the core-site.xml file in the resources folder of the application project.

    • Method 2: Dynamically set the configuration when you submit the application

      The following example shows how to set the configuration in Spark when you submit the application:

      hadoopConf:
          # HDFS
          "fs.defaultFS": "dfs://f-4b1fcae5dv***.cn-hangzhou.dfs.aliyuncs.com:10290"
          "fs.dfs.impl": "com.alibaba.dfs.DistributedFileSystem"
          "fs.AbstractFileSystem.dfs.impl": "com.alibaba.dfs.DFS"
  3. Package the JAR file.

    mvn assembly:assembly

    The packaged JAR file must contain all dependencies. The pom.xml file for the application is as follows:

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.aliyun.liumi.spark</groupId>
        <artifactId>SparkExampleJava</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <dependencies>
            <dependency>
                <groupId>org.apache.spark</groupId>
                <artifactId>spark-core_2.12</artifactId>
                <version>2.4.3</version>
            </dependency>
    
            <dependency>
                <groupId>com.aliyun.dfs</groupId>
                <artifactId>aliyun-sdk-dfs</artifactId>
                <version>1.0.3</version>
            </dependency>
    
        </dependencies>
    
        <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <appendAssemblyId>false</appendAssemblyId>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>com.aliyun.liumi.spark.example.WordCount</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>assembly</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        </build>
    </project>
    			
  4. Write the Dockerfile.

    # spark base image
    FROM registry.cn-hangzhou.aliyuncs.com/eci_open/spark:2.4.4
    # The default kubernetes-client version has issues. Use the latest version.
    RUN rm $SPARK_HOME/jars/kubernetes-client-*.jar
    ADD https://repo1.maven.org/maven2/io/fabric8/kubernetes-client/4.4.2/kubernetes-client-4.4.2.jar $SPARK_HOME/jars
    # Copy the local application JAR file
    RUN mkdir -p /opt/spark/jars
    COPY SparkExampleJava-1.0-SNAPSHOT.jar /opt/spark/jars
  5. Build the application image.

    docker build -t registry.cn-beijing.aliyuncs.com/liumi/spark:2.4.4-example -f Dockerfile .	
  6. Push the image to Alibaba Cloud ACR.

    docker push registry.cn-beijing.aliyuncs.com/liumi/spark:2.4.4-example	

After you complete these steps and prepare the Spark application image, you can use the image to deploy the Spark application in a Kubernetes cluster.