Created
May 8, 2017 11:18
-
-
Save xiyoulaoyuanjia/e89e94adffec761b70b4c63ec1692caa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package hadoopDemo; | |
import java.io.File; | |
import org.apache.avro.Protocol; | |
import org.apache.avro.Protocol.Message; | |
import org.apache.avro.generic.GenericData; | |
import org.apache.avro.generic.GenericRecord; | |
import org.apache.avro.ipc.HttpServer; | |
import org.apache.avro.ipc.Server; | |
import org.apache.avro.ipc.generic.GenericResponder; | |
import org.apache.commons.logging.Log; | |
import org.apache.commons.logging.LogFactory; | |
public class AvroHttpServer extends GenericResponder { | |
private static Log log = LogFactory.getLog(AvroHttpServer.class); | |
public AvroHttpServer(Protocol protocol) { | |
super(protocol); | |
} | |
public Object respond(Message message, Object request) throws Exception { | |
GenericRecord req = (GenericRecord) request; | |
GenericRecord reMessage = null; | |
log.info(message); | |
log.info(req); | |
if (message.getName().equals("sayHello")) { | |
Object name = req.get("name"); | |
// do something... | |
//取得返回值的类型 | |
reMessage = new GenericData.Record(super.getLocal().getType("nameMessage")); | |
//直接构造回复 | |
//reMessage.put("name", "Hello, " + name.toString()); | |
reMessage.put("name", "Hello, sb "); | |
log.info(reMessage); | |
} | |
return reMessage; | |
} | |
public static void main(String[] args) throws Exception { | |
int port = 8088; | |
try { | |
Server server = new HttpServer( | |
new AvroHttpServer(Protocol.parse( | |
// new File("helloword.json"))), | |
new File("d://a.avro"))), | |
port); | |
server.start(); | |
server.join(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment