码迷,mamicode.com
首页 > 其他好文 > 详细

大数据学习——hadoop的RPC框架

时间:2019-01-03 23:10:15      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:protocol   soc   nod   ado   enc   local   config   socket   data   

项目结构

技术分享图片

 

服务端代码 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);

    }
}

 

大数据学习——hadoop的RPC框架

标签:protocol   soc   nod   ado   enc   local   config   socket   data   

原文地址:https://www.cnblogs.com/feifeicui/p/10217404.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!