1. 程式人生 > >大資料學習——hadoop的RPC框架

大資料學習——hadoop的RPC框架

專案結構

 

服務端程式碼 test-hadoop-rpc

pom.xml

<?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.cyf</groupId> <artifactId>test-hadoop-rpc</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>test-hadoop-rpc Maven Webapp</name> <!-- FIXME change it to the project's website
--> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target>
</properties> <dependencies> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-common</artifactId> <version>2.6.4</version> </dependency> </dependencies> </project>
ClientNameNodeProtocal.java
package cn.itcast.rpc.protocal;

/**
 * Created by Administrator on 2019/1/3.
 */
public interface ClientNameNodeProtocal {
    public static final long versionID = 1L;

    public String getMetadata(String path);
}
NameNode.java
package cn.itcast.rpc.client;

import cn.itcast.rpc.protocal.ClientNameNodeProtocal;

/**
 * Created by Administrator on 2019/1/3.
 */
public class NameNode implements ClientNameNodeProtocal {
    @Override
    public String getMetadata(String path) {

        return path + "2 {blk_1,blk_2} {blk_1:mini2,mini3}";
    }
}
ServerPublisher.java

package cn.itcast.rpc.client;


import cn.itcast.rpc.protocal.ClientNameNodeProtocal;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.ipc.RPC;

import java.io.IOException;

/**
 * Created by Administrator on 2019/1/3.
 */
public class ServerPublisher {
    public static void main(String[] args) throws IOException {
        RPC.Builder builder = new RPC.Builder(new Configuration());

        builder.setBindAddress("localhost").setPort(8787).setProtocol(ClientNameNodeProtocal.class).setInstance(new NameNode());
        RPC.Server server = builder.build();
        server.start();
    }
}

 

 

客戶端程式碼

test-hadoop-rpc-client

pom.xml

<?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.cyf</groupId>
  <artifactId>test-hadoop-rpc-client</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>test-hadoop-rpc-client Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-common</artifactId>
      <version>2.6.4</version>
    </dependency>
  </dependencies>

</project>
ClientNameNodeProtocal.java

package cn.itcast.rpc.protocal;

/**
 * Created by Administrator on 2019/1/3.
 */
public interface ClientNameNodeProtocal {
    public static final long versionID = 1L;

    public String getMetadata(String path);
}
HdfsClient.java

package cn.itcast.rpc.client;

import cn.itcast.rpc.protocal.ClientNameNodeProtocal;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.ipc.RPC;

import java.io.IOException;
import java.net.InetSocketAddress;

/**
 * Created by Administrator on 2019/1/3.
 */
public class HdfsClient {
    public static void main(String[] args) throws IOException {

        ClientNameNodeProtocal nameNode = RPC.getProxy(ClientNameNodeProtocal.class, 1L, new InetSocketAddress("localhost", 8787), new Configuration());
        String metaData = nameNode.getMetadata("a.txt");
        System.out.println(metaData);

    }
}