大数据学习——hadoop的RPC框架
Posted o_0
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了大数据学习——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); } }
以上是关于大数据学习——hadoop的RPC框架的主要内容,如果未能解决你的问题,请参考以下文章
大数据技术之_03_Hadoop学习_01_入门_大数据概论+从Hadoop框架讨论大数据生态+Hadoop运行环境搭建(开发重点)