本文整理了Java中org.apache.avro.Protocol
类的一些代码示例,展示了Protocol
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Protocol
类的具体详情如下:
包路径:org.apache.avro.Protocol
类名称:Protocol
[英]A set of messages forming an application protocol.
A protocol consists of:
a name for the protocol;
an optional namespace, further qualifying the name;
a list of types, or named Schema;
a list of errors, or named Schema for exceptions;
a list of named messages, each of which specifies,
request, the parameter schemas;
one-way
or
response, the response schema;
* errors, an optional list of potential error schema names.
[中]构成应用程序协议的一组消息。
协议包括:
*协议的名称;
*可选名称空间,进一步限定名称;
*类型列表或命名模式;
*错误列表,或异常的命名模式;
*命名消息的列表,每个消息指定,
*请求,参数模式;
*其中一个;
*单向的
或
*反应,反应图式;
*错误,潜在错误模式名称的可选列表。
代码示例来源:origin: org.apache.avro/avro
/** Return the protocol for a Java interface. */
public Protocol getProtocol(Class iface) {
try {
Protocol p = (Protocol)(iface.getDeclaredField("PROTOCOL").get(null));
if (!p.getNamespace().equals(iface.getPackage().getName()))
// HACK: protocol mismatches iface. maven shade plugin? try replacing.
p = Protocol.parse(p.toString().replace(p.getNamespace(),
iface.getPackage().getName()));
return p;
} catch (NoSuchFieldException e) {
throw new AvroRuntimeException("Not a Specific protocol: "+iface);
} catch (IllegalAccessException e) {
throw new AvroRuntimeException(e);
}
}
代码示例来源:origin: apache/avro
@Test
public void testEcho() throws IOException {
GenericRecord record =
new GenericData.Record(PROTOCOL.getType("TestRecord"));
record.put("name", new Utf8("foo"));
record.put("kind", new GenericData.EnumSymbol
(PROTOCOL.getType("Kind"), "BAR"));
record.put("hash", new GenericData.Fixed
(PROTOCOL.getType("MD5"),
new byte[]{0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5}));
GenericRecord params =
new GenericData.Record(PROTOCOL.getMessages().get("echo").getRequest());
params.put("record", record);
Object echoed = requestor.request("echo", params);
assertEquals(record, echoed);
}
代码示例来源:origin: apache/avro
private Protocol addStringType(Protocol p) {
if (stringType != StringType.String)
return p;
Protocol newP = new Protocol(p.getName(), p.getDoc(), p.getNamespace());
Map<Schema,Schema> types = new LinkedHashMap<>();
for (Map.Entry<String, Object> a : p.getObjectProps().entrySet()) {
newP.addProp(a.getKey(), a.getValue());
}
// annotate types
Collection<Schema> namedTypes = new LinkedHashSet<>();
for (Schema s : p.getTypes())
namedTypes.add(addStringType(s, types));
newP.setTypes(namedTypes);
// annotate messages
Map<String,Message> newM = newP.getMessages();
for (Message m : p.getMessages().values())
newM.put(m.getName(), m.isOneWay()
? newP.createMessage(m,
addStringType(m.getRequest(), types))
: newP.createMessage(m,
addStringType(m.getRequest(), types),
addStringType(m.getResponse(), types),
addStringType(m.getErrors(), types)));
return newP;
}
代码示例来源:origin: apache/avro
/**
* Constructs a similar Protocol instance with the same {@code name}, {@code doc}, and {@code namespace} as {code p}
* has. It also copies all the {@code props}.
*/
public Protocol(Protocol p) {
this(p.getName(), p.getDoc(), p.getNamespace());
putAll(p);
}
代码示例来源:origin: apache/avro
/**
* Provides a a unique gRPC service name for Avro RPC interface or its subclass Callback
* Interface.
*
* @param iface Avro RPC interface.
* @return unique service name for gRPC.
*/
public static String getServiceName(Class iface) {
Protocol protocol = getProtocol(iface);
return protocol.getNamespace() + "." + protocol.getName();
}
代码示例来源:origin: apache/avro
private static Protocol parse(JsonParser parser) {
try {
Protocol protocol = new Protocol();
protocol.parse((JsonNode)Schema.MAPPER.readTree(parser));
return protocol;
} catch (IOException e) {
throw new SchemaParseException(e);
}
}
代码示例来源:origin: apache/avro
@Test public void testP2() throws Exception {
Schema e1 = ReflectData.get().getSchema(E1.class);
assertEquals(Schema.Type.RECORD, e1.getType());
assertTrue(e1.isError());
Field message = e1.getField("detailMessage");
assertNotNull("field 'detailMessage' should not be null", message);
Schema messageSchema = message.schema();
assertEquals(Schema.Type.UNION, messageSchema.getType());
assertEquals(Schema.Type.NULL, messageSchema.getTypes().get(0).getType());
assertEquals(Schema.Type.STRING, messageSchema.getTypes().get(1).getType());
Protocol p2 = ReflectData.get().getProtocol(P2.class);
Protocol.Message m = p2.getMessages().get("error");
// check error schema is union
Schema response = m.getErrors();
assertEquals(Schema.Type.UNION, response.getType());
assertEquals(Schema.Type.STRING, response.getTypes().get(0).getType());
assertEquals(e1, response.getTypes().get(1));
}
代码示例来源:origin: apache/avro
/** Test that Responder ignores one-way with stateless transport. */
@Test public void testStatelessOneway() throws Exception {
// a version of the Simple protocol that doesn't declare "ack" one-way
Protocol protocol = new Protocol("Simple", "org.apache.avro.test");
Protocol.Message message =
protocol.createMessage("ack", null,
Schema.createRecord(new ArrayList<>()),
Schema.create(Schema.Type.NULL),
Schema.createUnion(new ArrayList<>()));
protocol.getMessages().put("ack", message);
// call a server over a stateless protocol that has a one-way "ack"
GenericRequestor requestor =
new GenericRequestor(protocol, createTransceiver());
requestor.request("ack", new GenericData.Record(message.getRequest()));
// make the request again, to better test handshakes w/ differing protocols
requestor.request("ack", new GenericData.Record(message.getRequest()));
}
代码示例来源:origin: apache/avro
for (Field f : PROTOCOL.getType("TestRecord").getFields())
fields.add(new Field(f.name(), f.schema(), null, null));
fields.add(new Field("extra", Schema.create(Schema.Type.BOOLEAN),
null, true));
Schema record =
Schema.createRecord("TestRecord", null, "org.apache.avro.test", false);
record.setFields(fields);
Protocol protocol = new Protocol("Simple", "org.apache.avro.test");
List<Field> params = new ArrayList<>();
params.add(new Field("record", record, null, null));
protocol.createMessage("echo", null, Schema.createRecord(params),
record,
Schema.createUnion(new ArrayList<>()));
protocol.getMessages().put("echo", message);
Transceiver t
= new SocketTransceiver(new InetSocketAddress(server.getPort()));
try {
GenericRequestor r = new GenericRequestor(protocol, t);
GenericRecord args = new GenericData.Record(message.getRequest());
GenericRecord rec = new GenericData.Record(record);
rec.put("name", new Utf8("foo"));
rec.put("kind", new GenericData.EnumSymbol
(PROTOCOL.getType("Kind"), "BAR"));
rec.put("hash", new GenericData.Fixed
(PROTOCOL.getType("MD5"),
new byte[]{0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5}));
rec.put("extra", Boolean.TRUE);
代码示例来源:origin: jingwei/krati
@Override
public String getProperty(String key) throws Exception {
if(key == null) {
throw new NullPointerException("key");
}
Schema schema = _protocol.getMessages().get(ProtocolConstants.MSG_META).getRequest();
GenericRecord req = new GenericData.Record(schema);
req.put("src", _sourceUtf8);
req.put("opt", ProtocolConstants.OPT_GET_PROPERTY_UTF8);
req.put("key", new Utf8(key));
Utf8 res = (Utf8)send(ProtocolConstants.MSG_META, req);
return res == null ? null : res.toString();
}
代码示例来源:origin: apache/avro
Annotation annotation = annotations[i][j];
if (annotation instanceof AvroSchema) // explicit schema
paramSchema = Schema.parse(((AvroSchema)annotation).value());
else if (annotation instanceof Union) // union
paramSchema = getAnnotatedUnion(((Union)annotation), names);
: paramSchema.getName()+i;
fields.add(new Schema.Field(paramName, paramSchema,
Schema request = Schema.createRecord(fields);
errs.add(getSchema(err, names));
Schema errors = Schema.createUnion(errs);
return protocol.createMessage(method.getName(), null /* doc */, request, response, errors);
代码示例来源:origin: apache/avro
@Test
public void testHello() throws IOException {
GenericRecord params =
new GenericData.Record(PROTOCOL.getMessages().get("hello").getRequest());
params.put("greeting", new Utf8("bob"));
Utf8 response = (Utf8)requestor.request("hello", params);
assertEquals(new Utf8("goodbye"), response);
}
代码示例来源:origin: apache/avro
public Object respond(Message message, Object request)
throws AvroRemoteException {
GenericRecord params = (GenericRecord)request;
if ("hello".equals(message.getName())) {
LOG.info("hello: "+params.get("greeting"));
return new Utf8("goodbye");
}
if ("echo".equals(message.getName())) {
Object record = params.get("record");
LOG.info("echo: "+record);
return record;
}
if ("echoBytes".equals(message.getName())) {
Object data = params.get("data");
LOG.info("echoBytes: "+data);
return data;
}
if ("error".equals(message.getName())) {
if (throwUndeclaredError) throw new RuntimeException("foo");
GenericRecord error =
new GenericData.Record(PROTOCOL.getType("TestError"));
error.put("message", new Utf8("an error"));
throw new AvroRemoteException(error);
}
throw new AvroRuntimeException("unexpected message: "+message.getName());
}
代码示例来源:origin: hibernate/hibernate-search
@Override
public void addIdAsDouble(double id) {
this.idRecord = new GenericData.Record( protocol.getType( "Id" ) );
idRecord.put( "value", id );
}
代码示例来源:origin: commercehub-oss/gradle-avro-plugin
private void processProtoFile(File sourceFile) {
getLogger().info("Processing {}", sourceFile);
try {
Protocol protocol = Protocol.parse(sourceFile);
for (Schema schema : protocol.getTypes()) {
String path = schema.getNamespace().replaceAll(Pattern.quote("."), "/");
File schemaFile = new File(getOutputDir(), path + "/" + schema.getName() + "." + SCHEMA_EXTENSION);
String schemaJson = schema.toString(true);
FileUtils.writeJsonFile(schemaFile, schemaJson);
getLogger().debug("Wrote {}", schemaFile.getPath());
}
} catch (IOException ex) {
throw new GradleException(String.format("Failed to process protocol definition file %s", sourceFile), ex);
}
}
}
代码示例来源:origin: apache/avro
@Test public void testP4() throws Exception {
Protocol p = ReflectData.get().getProtocol(P4.class);
Protocol.Message message = p.getMessages().get("foo");
assertEquals(Schema.Type.INT, message.getResponse().getType());
Field field = message.getRequest().getField("x");
assertEquals(Schema.Type.INT, field.schema().getType());
}
代码示例来源:origin: jingwei/krati
Schema schemaKV = _protocol.getType(ProtocolConstants.TYPE_KeyValue);
Schema schema = _protocol.getMessages().get(ProtocolConstants.MSG_MPUT).getRequest();
GenericRecord req = new GenericData.Record(schema);
GenericArray<GenericRecord> array = new GenericData.Array<GenericRecord>(map.size(), schema.getField("kvList").schema());
GenericRecord item = new GenericData.Record(schemaKV);
K key = e.getKey();
V value = e.getValue();
item.put("key", serializeKey(key));
item.put("value", value == null ? null : serializeValue(value));
array.add(item);
req.put("src", _sourceUtf8);
req.put("kvList", array);
代码示例来源:origin: jingwei/krati
@Override
public boolean delete(Collection<K> keys) throws Exception {
if(keys == null) {
throw new NullPointerException("keys");
}
Schema schema = _protocol.getMessages().get(ProtocolConstants.MSG_MDEL).getRequest();
GenericRecord req = new GenericData.Record(schema);
GenericArray<ByteBuffer> array = new GenericData.Array<ByteBuffer>(keys.size(), schema.getField("keys").schema());
for(K key : keys) {
if(key != null) {
array.add(serializeKey(key));
}
}
if(array.size() == 0) {
return false;
}
req.put("src", _sourceUtf8);
req.put("keys", array);
return (Boolean)send(ProtocolConstants.MSG_MDEL, req);
}
代码示例来源:origin: apache/avro
System.out.println(ReflectData.get().getProtocol(klass).toString(true));
} else {
System.out.println(ReflectData.get().getSchema(klass).toString(true));
代码示例来源:origin: apache/avro
Schema errors = Schema.createUnion(errorSchemata);
if (oneWay && response.getType() != Type.NULL)
{if (true) throw error("One-way message'"+name+"' must return void", token);}
{if (true) return oneWay
? p.createMessage(name, msgDoc, props, request)
: p.createMessage(name, msgDoc, props, request, response, errors);}
throw new Error("Missing return statement in function");
内容来源于网络,如有侵权,请联系作者删除!