Hadoop RPC instance

Source: Internet
Author: User

 

Datanode needs to send a request to the namenode to receive a string response. Assume that the namenode address is 127.0.0.1: 9000.

 

 

1. Configuration File

Org. Apache. hadoop. conf. configuration; the corresponding configuration file is a core-site.xml. The configuration items are stored as key-value.

<Configuration> <br/> <! -- Namenode --> <br/> <property> <br/> <Name> DFS. namenode. adress </Name> <br/> <value> localhost: 9000 </value> <br/> </property> <br/> <Name> DFS. namenode. handler. count </Name> <br/> <value> 5 </value> <br/> </property> <br/> </configuration> <br/> 

 

2. You need to define a protocol interface to encapsulate requests sent from all clients to the server. The interface must inherit from org. Apache. hadoop. IPC. versionedprotocol. Versionid in the interface is used to identify different protocols.

Package rpctest; <br/> Import Org. apache. hadoop. IPC. versionedprotocol; <br/> Public interface datanode2namenodeprotopencil extends versionedprotocol {<br/> Public static final long versionid = 19l; <br/> Public String getstring (); <br/>}< br/> 

 

Iii. namenode. The caller needs getserver to get an RPC server. The instance of the specified protocol starts the service on the specified address and port.

Package rpctest; <br/> Import Java. io. ioexception; <br/> Import java.net. inetsocketaddress; <br/> Import Org. apache. hadoop. conf. configuration; <br/> Import Org. apache. hadoop. IPC. RPC; <br/> Import Org. apache. hadoop. IPC. server; <br/> Import org.apache.hadoop.net. netutils; <br/> public class namenode implements datanode2namenodeprotopencil {<br/> private configuration conf; <br/> private server rpcserver; <br/> @ Override <br/> Public String getstring () {<br/> return "this is a remote call ...... "; <br/>}< br/> @ override <br/> Public long getprotocolversion (string arg0, long arg1) throws ioexception {<br/> return datanode2namenodeprotopencil. versionid; <br/>}< br/> Public namenode () throws ioexception {<br/> conf = new configuration (); <br/> inetsocketaddress socaddr = netutils. createsocketaddr (Conf. get (<br/> "DFS. Namenode. adress "," "); <br/> int handlercount = Conf. getint ("DFS. namenode. handler. count ", 5); <br/> rpcserver = RPC. getserver (this, socaddr. gethostname (), socaddr <br/>. getport (), handlercount, false, conf); <br/>}< br/> private void start () throws ioexception {<br/> rpcserver. start (); <br/>}</P> <p> private void jion () throws interruptedexception {<br/> rpcserver. join (); // while () Wait (); <br/>}< br/> P Ublic static void main (string [] ARGs) throws exception {<br/> namenode = new namenode (); <br/> If (namenode! = NULL) {<br/> namenode. start (); <br/> namenode. jion (); <br/>}< br/> 

 

4. Get an RPC client through datanode getproxy. The getproxy method uses a dynamic proxy to create a proxy for the specified server. A remote object requires a proxy.

Package rpctest; <br/> Import Java. io. ioexception; <br/> Import java.net. inetsocketaddress; <br/> Import Org. apache. hadoop. conf. configuration; <br/> Import Org. apache. hadoop. IPC. RPC; <br/> Import org.apache.hadoop.net. netutils; <br/> public class datanode {<br/> private configuration conf; <br/> private datanode2namenodeprotocel namenode; <br/> Public datanode () throws ioexception {<br/> conf = new configu Ration (); <br/> // connect to Name node <br/> inetsocketaddress namenodeaddr = netutils. createsocketaddr (Conf. get (<br/> "DFS. namenode. adress "," "); <br/> namenode = (datanode2namenodeprotocer) RPC. waitforproxy (<br/> datanode2namenodeprotoric. class, <br/> datanode2namenodeprotoric. versionid, namenodeaddr, conf); <br/>}< br/> Public String getstring () {<br/> return namenode. getstring (); <br/>}< br/> Publ IC void stop () {<br/> RPC. stopproxy (namenode); <br/>}< br/> Public static void main (string [] ARGs) throws exception {<br/> datanode = new datanode (); <br/> If (datanode! = NULL) <br/> system. out. println (datanode. getstring (); <br/> datanode. stop (); <br/>}< br/> 

5. Run namenode


6. Run datanode

 

This is a remote call ......


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.