本文整理汇总了Java中org.apache.avro.generic.GenericRecordBuilder.set方法的典型用法代码示例。如果您正苦于以下问题:Java GenericRecordBuilder.set方法的具体用法?Java GenericRecordBuilder.set怎么用?Java GenericRecordBuilder.set使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.avro.generic.GenericRecordBuilder
的用法示例。
在下文中一共展示了GenericRecordBuilder.set方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testMaps
import org.apache.avro.generic.GenericRecordBuilder; //导入方法依赖的package包/类
@Test
public void testMaps() throws Exception {
Schema schema = Maps.SCHEMA$;
GenericRecordBuilder builder = new GenericRecordBuilder(schema);
builder.set("maps", ImmutableMap.of("key1", ImmutableMap.of("value1", 1, "value2", 2), "key2",
ImmutableMap.of(), "key3", ImmutableMap.of("value3", 3)));
Record record1 = builder.build();
String json = "{\"maps\": {\"key1\": {\"value1\": 1, \"value2\": 2}, \"key2\": {}, \"key3\": {\"value3\": 3}}}";
DBObject object = (DBObject) JSON.parse(json);
Record record2 = RecordConverter.toRecord(schema, object, getClass().getClassLoader());
// Convert into JsonNode before comparison, so the maps equal even if keys are reordered.
assertThat(JSON.parse(AvroHelper.toSimpleJson(schema, record2)), is(JSON.parse(AvroHelper.toSimpleJson(schema, record1))));
}
示例2: publish_shouldThrowAnEncodeTransportExceptionForAnInvalidTransportMessage
import org.apache.avro.generic.GenericRecordBuilder; //导入方法依赖的package包/类
@Test
public void publish_shouldThrowAnEncodeTransportExceptionForAnInvalidTransportMessage() throws Exception {
// expect
exception.expect(EncodeMessageContentException.class);
exception.expectCause(IsInstanceOf.<Throwable>instanceOf(ClassCastException.class));
exception.expectMessage("Failed to publish message: Failed to encode message content");
// given
Context context = mock(Context.class);
when(context.getSchema())
.thenReturn(new Schema.Parser().parse("{\"name\":\"test\",\"type\":\"record\",\"fields\":[]}"));
when(context.getDataBuffer()).thenReturn(ByteBuffer.wrap("{}".getBytes()));
GenericRecordBuilder builder = new GenericRecordBuilder(schema);
builder.set("testString", Boolean.TRUE);
Record record = builder.build();
// when
publisher.publish(context, record);
}
示例3: publish_shouldThrowAnEncodeTransportExceptionForAnInvalidTransportContext
import org.apache.avro.generic.GenericRecordBuilder; //导入方法依赖的package包/类
@Test
public void publish_shouldThrowAnEncodeTransportExceptionForAnInvalidTransportContext() throws Exception {
// expect
exception.expect(EncodeTransportException.class);
exception.expectCause(IsInstanceOf.<Throwable>instanceOf(NullPointerException.class));
exception.expectMessage("Failed to publish message: Failed to encode transport");
// given
Context context = mock(Context.class);
when(context.getSchema())
.thenReturn(new Schema.Parser().parse("{\"name\":\"test\",\"type\":\"record\",\"fields\":[]}"));
when(context.getDataBuffer()).thenReturn(null);
GenericRecordBuilder builder = new GenericRecordBuilder(schema);
builder.set("testString", "testString");
Record record = builder.build();
// when
publisher.publish(context, record);
}
示例4: publish_shouldPublishTheMessage
import org.apache.avro.generic.GenericRecordBuilder; //导入方法依赖的package包/类
@Test
public void publish_shouldPublishTheMessage() throws Exception {
// given
Context context = mock(Context.class);
when(context.getSchema())
.thenReturn(new Schema.Parser().parse("{\"name\":\"test\",\"type\":\"record\",\"fields\":[]}"));
when(context.getDataBuffer()).thenReturn(ByteBuffer.wrap("{}".getBytes()));
GenericRecordBuilder builder = new GenericRecordBuilder(schema);
builder.set("testString", "test");
Record record = builder.build();
// when
publisher.publish(context, record);
}
示例5: writeRowsHelper
import org.apache.avro.generic.GenericRecordBuilder; //导入方法依赖的package包/类
private void writeRowsHelper(List<TableRow> rows, Schema avroSchema,
String destinationPattern, int shard) throws IOException {
String filename = destinationPattern.replace("*", String.format("%012d", shard));
try (WritableByteChannel channel = FileSystems.create(
FileSystems.matchNewResource(filename, false /* isDirectory */), MimeTypes.BINARY);
DataFileWriter<GenericRecord> tableRowWriter =
new DataFileWriter<>(new GenericDatumWriter<GenericRecord>(avroSchema))
.create(avroSchema, Channels.newOutputStream(channel))) {
for (Map<String, Object> record : rows) {
GenericRecordBuilder genericRecordBuilder = new GenericRecordBuilder(avroSchema);
for (Map.Entry<String, Object> field : record.entrySet()) {
genericRecordBuilder.set(field.getKey(), field.getValue());
}
tableRowWriter.append(genericRecordBuilder.build());
}
} catch (IOException e) {
throw new IllegalStateException(
String.format("Could not create destination for extract job %s", filename), e);
}
}
示例6: parse
import org.apache.avro.generic.GenericRecordBuilder; //导入方法依赖的package包/类
/**
* Parse the entity from the body in JSON of the given event.
*
* @param event
* The event to parse.
* @param reuse
* If non-null, this may be reused and returned from this method.
* @return The parsed entity as a GenericRecord.
* @throws EventDeliveryException
* A recoverable error such as an error downloading the schema
* from the URL has occurred.
* @throws NonRecoverableEventException
* A non-recoverable error such as an unparsable schema or
* entity has occurred.
*/
@Override
public GenericRecord parse(Event event, GenericRecord reuse)
throws EventDeliveryException, NonRecoverableEventException {
JsonObject parser = new JsonParser().parse(new String(event.getBody())).getAsJsonObject();
GenericRecordBuilder recordBuilder = new GenericRecordBuilder(datasetSchema);
for (Field field:datasetSchema.getFields()) {
String at_header = field.getProp(FIELD_AT_HEADER_PROPERTY);
if(at_header != null && at_header.equals(Boolean.TRUE.toString())){
recordBuilder.set(field.name(), event.getHeaders().get(field.name()));
}else{
JsonElement element = parser.get(field.name());
recordBuilder.set(field.name(), getElementAsType(field.schema(), element));
}
}
return recordBuilder.build();
}
示例7: testArrays
import org.apache.avro.generic.GenericRecordBuilder; //导入方法依赖的package包/类
@Test
public void testArrays() throws Exception {
Schema schema = Arrays.SCHEMA$;
GenericRecordBuilder builder = new GenericRecordBuilder(schema);
builder.set("arrays", ImmutableList.of(ImmutableList.of(ImmutableList.of(1, 2, 3),
ImmutableList.of()), ImmutableList.of(ImmutableList.of(4), ImmutableList.of()),
ImmutableList.of(ImmutableList.of())));
Record record1 = builder.build();
String json = "{\"arrays\": [[[1, 2, 3], []], [[4], []], [[]]]}";
BSONObject object = (BSONObject) JSON.parse(json);
Record record2 = RecordConverter.toRecord(schema, object, getClass().getClassLoader());
assertThat(record2, is(record1));
assertThat(AvroHelper.toSimpleJson(schema, record2), is(AvroHelper.toSimpleJson(schema, record1)));
}
示例8: testArrays
import org.apache.avro.generic.GenericRecordBuilder; //导入方法依赖的package包/类
@Test
public void testArrays() throws Exception {
Schema schema = Arrays.SCHEMA$;
GenericRecordBuilder builder = new GenericRecordBuilder(schema);
builder.set("arrays", ImmutableList.of(ImmutableList.of(ImmutableList.of(1, 2, 3),
ImmutableList.of()), ImmutableList.of(ImmutableList.of(4), ImmutableList.of()),
ImmutableList.of(ImmutableList.of())));
Record record1 = builder.build();
String json = "{\"arrays\": [[[1, 2, 3], []], [[4], []], [[]]]}";
BSONObject object = (BSONObject) JSON.parse(json);
Record record2 = RecordConverter.toRecord(schema, object, getClass().getClassLoader());
assertThat(record2).isEqualTo(record1);
assertThat(AvroHelper.toJson(schema, record2)).isEqualTo(AvroHelper.toJson(schema, record1));
}
示例9: testMaps
import org.apache.avro.generic.GenericRecordBuilder; //导入方法依赖的package包/类
@Test
public void testMaps() throws Exception {
Schema schema = Maps.SCHEMA$;
GenericRecordBuilder builder = new GenericRecordBuilder(schema);
builder.set("maps", ImmutableMap.of("key1", ImmutableMap.of("value1", 1, "value2", 2), "key2",
ImmutableMap.of(), "key3", ImmutableMap.of("value3", 3)));
Record record1 = builder.build();
String json = "{\"maps\": {\"key1\": {\"value1\": 1, \"value2\": 2}, \"key2\": {}, \"key3\": {\"value3\": 3}}}";
DBObject object = (DBObject) JSON.parse(json);
Record record2 = RecordConverter.toRecord(schema, object, getClass().getClassLoader());
// Convert into JsonNode before comparison, so the maps equal even if keys are reordered.
assertThat((Object) Json.parse(AvroHelper.toJson(schema, record2)))
.isEqualTo(Json.parse(AvroHelper.toJson(schema, record1)));
}
示例10: getPartitionKey
import org.apache.avro.generic.GenericRecordBuilder; //导入方法依赖的package包/类
@SuppressWarnings("deprecation")
private static PartitionKey getPartitionKey(Dataset data, long timestamp) {
// need to build a fake record to get a partition key
final GenericRecordBuilder builder = new GenericRecordBuilder(
data.getDescriptor().getSchema());
builder.set("timestamp", timestamp);
builder.set("level", "INFO");
builder.set("component", "StagingToPersistentSerial");
builder.set("message", "Fake log message");
// access the partition strategy, which produces keys from records
final PartitionStrategy partitioner = data.getDescriptor()
.getPartitionStrategy();
return partitioner.partitionKeyForEntity(builder.build());
}
示例11: writeTestUsers
import org.apache.avro.generic.GenericRecordBuilder; //导入方法依赖的package包/类
public static void writeTestUsers(Dataset<GenericData.Record> ds, int count, int start, String... fields) {
DatasetWriter<GenericData.Record> writer = null;
try {
writer = ds.newWriter();
writer.open();
for (int i = start; i < count + start; i++) {
GenericRecordBuilder recordBuilder = new GenericRecordBuilder(ds.getDescriptor
().getSchema()).set("username", "test-" + i);
for (String field : fields) {
recordBuilder.set(field, field + "-" + i);
}
writer.write(recordBuilder.build());
}
writer.flush();
} finally {
if (writer != null) {
writer.close();
}
}
}
示例12: createGenericRecordWithSchema
import org.apache.avro.generic.GenericRecordBuilder; //导入方法依赖的package包/类
private static GenericRecord createGenericRecordWithSchema(Schema schema, GenericRecord avrObj){
GenericRecordBuilder builder = new GenericRecordBuilder(schema);
for (Schema.Field field : schema.getFields()){
builder.set(field, avrObj.get(field.name()));
}
return builder.build();
}
示例13: toAvroRow
import org.apache.avro.generic.GenericRecordBuilder; //导入方法依赖的package包/类
private GenericRecord toAvroRow(EventModel event) {
GenericRecordBuilder recordBuilder = new GenericRecordBuilder(avroSchema);
EventHeaderModel header = event.getHeader();
recordBuilder.set(EventHDFSTableSchema.EVENT_ID, event.getId().toString());
recordBuilder.set(EventHDFSTableSchema.EVENT_CODE, header.getCode());
recordBuilder.set(EventHDFSTableSchema.EVENT_CREATED_AT_MILLIS, header.getTimeStamp());
recordBuilder.set(EventHDFSTableSchema.EVENT_HEADER_JSON, toJson(header.getHeaders()));
recordBuilder.set(EventHDFSTableSchema.EVENT_PARAMS_JSON, toJson(event.getParams()));
EventType eventType = header.getEventType();
if (eventType != null) {
recordBuilder.set(EventHDFSTableSchema.EVENT_TYPE, eventType.name());
EventDetailModel eventDetail = event.getEventDetail();
if (header.getEventType() == EventType.System) {
SystemEventDetailModel systemEventDetail = eventDetail.getSystemEventDetail();
// System Event
recordBuilder.set(EventHDFSTableSchema.PROCESS_ID, systemEventDetail.getProcessId());
recordBuilder.set(EventHDFSTableSchema.PROCESS_NAME, systemEventDetail.getProcessName());
recordBuilder.set(EventHDFSTableSchema.VM_DETAILS, systemEventDetail.getVmDetail());
recordBuilder.set(EventHDFSTableSchema.CALL_TRACE, systemEventDetail.getGeneratorTrace());
recordBuilder.set(EventHDFSTableSchema.MAC_ID, systemEventDetail.getMacId());
recordBuilder.set(EventHDFSTableSchema.PRODUCT_NAME, systemEventDetail.getProductName());
recordBuilder.set(EventHDFSTableSchema.MODULE_NAME, systemEventDetail.getModuleName());
recordBuilder.set(EventHDFSTableSchema.ACTION, systemEventDetail.getAction());
} else {
UserEventDetailModel userEventDetail = eventDetail.getUserEventDetail();
// User Event
recordBuilder.set(EventHDFSTableSchema.USER_ID, userEventDetail.getUserId());
recordBuilder.set(EventHDFSTableSchema.USER_AGENT, userEventDetail.getUserAgent());
recordBuilder.set(EventHDFSTableSchema.CLIENT_IP_ADDRESS, userEventDetail.getIpAddress());
recordBuilder.set(EventHDFSTableSchema.ACTION_URL, userEventDetail.getActionUrl());
}
}
verifyNull(recordBuilder);
return recordBuilder.build();
}
示例14: verifyNull
import org.apache.avro.generic.GenericRecordBuilder; //导入方法依赖的package包/类
private void verifyNull(GenericRecordBuilder recordBuilder) {
List<Field> fields = avroSchema.getFields();
for (Field field : fields) {
Object value = recordBuilder.get(field);
if (value == null) {
recordBuilder.set(field, "");
}
}
}
示例15: wrapOption
import org.apache.avro.generic.GenericRecordBuilder; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
private Object wrapOption(Schema schema, Object option) {
if (schema.getType() == Schema.Type.BYTES && option instanceof String) {
option = ByteBuffer.wrap(((String) option).getBytes(Charset.defaultCharset()));
} else if (schema.getType() == Schema.Type.FLOAT && option instanceof Double) {
option = ((Double) option).floatValue();
} else if (schema.getType() == Schema.Type.LONG && option instanceof Integer) {
option = ((Integer) option).longValue();
} else if (schema.getType() == Schema.Type.ARRAY && option instanceof Collection) {
option = new GenericData.Array(schema, (Collection) option);
} else if (schema.getType() == Schema.Type.ENUM && option instanceof String) {
option = new GenericData.EnumSymbol(schema, (String) option);
} else if (schema.getType() == Schema.Type.FIXED && option instanceof String) {
option =
new GenericData.Fixed(schema, ((String) option).getBytes(Charset.defaultCharset()));
} else if (schema.getType() == Schema.Type.RECORD && option instanceof Map) {
Map optionMap = (Map) option;
GenericRecordBuilder optionBuilder = new GenericRecordBuilder(schema);
for (Schema.Field field : schema.getFields()) {
if (optionMap.containsKey(field.name())) {
optionBuilder.set(field, optionMap.get(field.name()));
}
}
option = optionBuilder.build();
}
return option;
}