Premise: There is a Hadoop cluster, and copy Core-site.xml,hdfs-site.xml,mapred-site.xml,yarn-site.xml to Classpath, you can make src/main/resources
1. Get a getnewapplicationrequest, an instance of Protobuf class Getnewapplicationrequestpbimpl, which will support other serialization methods in the future. The serialization method determines which serializable class the RPC factory produces.
Records.newrecord, is to instantiate a Protobuf class, note that if you write code, the suffix of protobuf has some rules, must be "Pbimpl" end, the package name must be "IMPL.PB"; Otherwise, the corresponding Protobuf class cannot be found
Getnewapplicationrequest request = Records . NewRecord (getnewapplicationrequest. class);
Configuration conf = new configuration ();
2. Get the IP and port where the Resoucemanager resides
Inetsocketaddress rmaddress = conf.getsocketaddr ( yarnconfiguration.rm_address, Yarnconfiguration.default_rm_address, yarnconfiguration.default_rm_port);
3. Create a new YARNRPC instance, which is the default class: Org.apache.hadoop.yarn.ipc.HadoopYarnProtoRPC, and gets
Applicationclientprotocol instance, the default class is Applicationclientprotocolpbclientimpl
Immediately after the call gets a new AppID
//Get Yarn RPCYARNRPC RPC =yarnrpc.create (conf); //get a proxy from yarn RPC, yarn RPC is a factory, can produce some protocolRmclientprotocol =( Applicationclientprotocol) (Rpc.getproxy (Applicationclientprotocol.class, rmaddress, conf)); //get response from yarn RPC proxies with sending the Getnewapplicationrequest//which contains a Protobuf object.Getnewapplicationresponse Newapp =Rmclientprotocol. getnewapplication (Request); System.err.println ("Get a new Application ID:" + newapp.getapplicationid ());
The specific proxy has done how to initialize which classes, but also need further research.
The summary is that currently Hadoop supports PROTOBUF as the default serialization framework, and currently yarn RPC is using the Protobuf factory.
Here is the complete code
PackageCom.jarvis.yarnapp;Importjava.io.IOException;Importjava.net.InetSocketAddress;Importorg.apache.hadoop.conf.Configuration;ImportOrg.apache.hadoop.yarn.api.ApplicationClientProtocol;Importorg.apache.hadoop.yarn.api.protocolrecords.GetNewApplicationRequest;ImportOrg.apache.hadoop.yarn.api.protocolrecords.GetNewApplicationResponse;Importorg.apache.hadoop.yarn.conf.YarnConfiguration;Importorg.apache.hadoop.yarn.exceptions.YarnException;ImportOrg.apache.hadoop.yarn.ipc.YarnRPC;Importorg.apache.hadoop.yarn.util.Records; Public classzjxclient {PrivateApplicationclientprotocol Rmclientprotocol; Public voidRun ()throwsyarnexception, IOException {//get a request for communicate with yarnGetnewapplicationrequest request =Records. NewRecord (getnewapplicationrequest.class); Configuration conf=NewConfiguration (); //get Resouce Manager addressInetsocketaddress rmaddress =conf.getsocketaddr (yarnconfiguration.rm_address, Yarnconfiguration.default_rm_address, Yarnconfiguration.default_rm_port); //Get Yarn RPCYARNRPC RPC =yarnrpc.create (conf); //get a proxy from yarn RPC, yarn RPC is a factory, can produce some protocolRmclientprotocol =( Applicationclientprotocol) (Rpc.getproxy (Applicationclientprotocol.class, rmaddress, conf)); //get response from yarn RPC proxies with sending the Getnewapplicationrequest//which contains a Protobuf object.Getnewapplicationresponse Newapp =Rmclientprotocol. getnewapplication (Request); System.err.println ("Get a new Application ID:" +Newapp.getapplicationid ()); } Public Static voidMain (string[] args)throwsyarnexception, IOException {zjxclient zjxclient=Newzjxclient (); Zjxclient.run (); }}
Get a new application ID from Resource Manager