本文整理汇总了Java中org.apache.avro.generic.GenericData.Record类的典型用法代码示例。如果您正苦于以下问题:Java Record类的具体用法?Java Record怎么用?Java Record使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Record类属于org.apache.avro.generic.GenericData包,在下文中一共展示了Record类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: readAvroFile
import org.apache.avro.generic.GenericData.Record; //导入依赖的package包/类
/**
* Reads in binary Avro-encoded entities using the schema stored in the file
* and prints them out.
*/
public static void readAvroFile(File file) throws IOException {
GenericDatumReader datum = new GenericDatumReader();
DataFileReader reader = new DataFileReader(file, datum);
GenericData.Record record = new GenericData.Record(reader.getSchema());
while (reader.hasNext()) {
reader.next(record);
System.out.println("Name " + record.get("name") + " on "
+ record.get("Meetup_date") + " attending "
+ record.get("going") + " organized by "
+ record.get("organizer") + " on " + record.get("topics"));
}
reader.close();
}
示例2: testMaps
import org.apache.avro.generic.GenericData.Record; //导入依赖的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))));
}
示例3: testGeneric
import org.apache.avro.generic.GenericData.Record; //导入依赖的package包/类
@Test
public void testGeneric() throws IOException {
Dataset<Record> inputDataset = repo.create("in", new DatasetDescriptor.Builder()
.schema(USER_SCHEMA).build());
Dataset<Record> outputDataset = repo.create("out", new DatasetDescriptor.Builder()
.schema(USER_SCHEMA).build());
// write two files, each of 5 records
writeTestUsers(inputDataset, 5, 0);
writeTestUsers(inputDataset, 5, 5);
Pipeline pipeline = new MRPipeline(TestCrunchDatasets.class);
PCollection<GenericData.Record> data = pipeline.read(
CrunchDatasets.asSource(inputDataset, GenericData.Record.class));
pipeline.write(data, CrunchDatasets.asTarget(outputDataset), Target.WriteMode.APPEND);
pipeline.run();
checkTestUsers(outputDataset, 10);
}
示例4: testGenericParquet
import org.apache.avro.generic.GenericData.Record; //导入依赖的package包/类
@Test
public void testGenericParquet() throws IOException {
Dataset<Record> inputDataset = repo.create("in", new DatasetDescriptor.Builder()
.schema(USER_SCHEMA).format(Formats.PARQUET).build());
Dataset<Record> outputDataset = repo.create("out", new DatasetDescriptor.Builder()
.schema(USER_SCHEMA).format(Formats.PARQUET).build());
// write two files, each of 5 records
writeTestUsers(inputDataset, 5, 0);
writeTestUsers(inputDataset, 5, 5);
Pipeline pipeline = new MRPipeline(TestCrunchDatasets.class);
PCollection<GenericData.Record> data = pipeline.read(
CrunchDatasets.asSource(inputDataset, GenericData.Record.class));
pipeline.write(data, CrunchDatasets.asTarget(outputDataset), Target.WriteMode.APPEND);
pipeline.run();
checkTestUsers(outputDataset, 10);
}
示例5: publish_shouldThrowAnEncodeTransportExceptionForAnInvalidTransportMessage
import org.apache.avro.generic.GenericData.Record; //导入依赖的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);
}
示例6: publish_shouldThrowAnEncodeTransportExceptionForAnInvalidTransportContext
import org.apache.avro.generic.GenericData.Record; //导入依赖的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);
}
示例7: publish_shouldPublishTheMessage
import org.apache.avro.generic.GenericData.Record; //导入依赖的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);
}
示例8: readWithDifferentSchema
import org.apache.avro.generic.GenericData.Record; //导入依赖的package包/类
/**
* Reads in binary Avro-encoded entities using a schema that is different
* from the writer's schema.
*
*/
public static void readWithDifferentSchema(File file, Schema newSchema)
throws IOException {
GenericDatumReader datum = new GenericDatumReader(newSchema);
DataFileReader reader = new DataFileReader(file, datum);
GenericData.Record record = new GenericData.Record(newSchema);
while (reader.hasNext()) {
reader.next(record);
System.out.println("Name " + record.get("name") + " on "
+ record.get("Meetup_date") + " attending "
+ record.get("attendance") + " organized by "
+ record.get("organizer")
+ " at " + record.get("location"));
}
reader.close();
}
示例9: transformAvroArrayToObjectArray
import org.apache.avro.generic.GenericData.Record; //导入依赖的package包/类
private static Object[] transformAvroArrayToObjectArray(Array arr) {
if (arr == null) {
return new Object[0];
}
final Object[] ret = new Object[arr.size()];
final Iterator iterator = arr.iterator();
int i = 0;
while (iterator.hasNext()) {
Object value = iterator.next();
if (value instanceof Record) {
value = ((Record) value).get(0);
}
if (value instanceof Utf8) {
value = ((Utf8) value).toString();
}
ret[i++] = value;
}
return ret;
}
示例10: transformAvroArrayToObjectArray
import org.apache.avro.generic.GenericData.Record; //导入依赖的package包/类
public static Object[] transformAvroArrayToObjectArray(Array arr, FieldSpec spec) {
if (arr == null) {
return new Object[] { getDefaultNullValue(spec) };
}
if (arr.size() == 0) {
return new Object[] { getDefaultNullValue(spec) };
}
final Object[] ret = new Object[arr.size()];
final Iterator iterator = arr.iterator();
int i = 0;
while (iterator.hasNext()) {
Object value = iterator.next();
if (value instanceof Record) {
value = ((Record) value).get(0);
}
if (value instanceof Utf8) {
value = ((Utf8) value).toString();
}
if (value == null) {
value = getDefaultNullValue(spec);
}
ret[i++] = value;
}
return ret;
}
示例11: decode
import org.apache.avro.generic.GenericData.Record; //导入依赖的package包/类
@SuppressWarnings("unchecked")
@Override
public JetstreamEvent decode(byte[] key, byte[] message) {
ByteArrayInputStream stream = new ByteArrayInputStream(message);
BinaryDecoder reusedDecoder = decoderHolder.get();
BinaryDecoder decoder = DecoderFactory.get().directBinaryDecoder(stream, reusedDecoder);
if (reusedDecoder == null) {
decoderHolder.set(decoder);
}
Record object;
try {
object = reader.read(null, decoder);
Map<String, Object> m = (Map<String, Object>) object.get(MAP_FIELD_NAME);
return new JetstreamEvent(m);
} catch (IOException e) {
throw new IllegalArgumentException("Can not read the avro message", e);
}
}
示例12: testDefaultValues
import org.apache.avro.generic.GenericData.Record; //导入依赖的package包/类
@Test
public void testDefaultValues() throws Exception {
Schema schema = Defaults.SCHEMA$;
GenericRecordBuilder builder = new GenericRecordBuilder(schema).set("id", "1234");
Record record1 = builder.build();
String json = "{\"id\": \"1234\"}";
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)));
assertEquals(record2.get("id"), "1234");
assertNull(record2.get("s"));
assertTrue((Boolean) record2.get("b"));
assertEquals(((Record) record2.get("r")).get("f"), "value");
assertEquals(((Record) record2.get("r")).get("l"), 1234l);
}
示例13: testArrays
import org.apache.avro.generic.GenericData.Record; //导入依赖的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)));
}
示例14: testEnums
import org.apache.avro.generic.GenericData.Record; //导入依赖的package包/类
@Test
public void testEnums() throws Exception {
Schema schema = Enums.SCHEMA$;
String avroJson = "{\"enum1\": \"X\", \"enum2\": {\"test.Enum2\": \"A\"}, \"enum3\": {\"null\": null}, \"enum4\": [{\"test.Enum4\": \"SAT\"}, {\"test.Enum4\": \"SUN\"}]}}";
Decoder decoder = DecoderFactory.get().jsonDecoder(schema, avroJson);
GenericDatumReader<Record> reader = new GenericDatumReader<Record>(schema);
Record record1 = reader.read(null, decoder);
String mongoJson = "{\"enum1\": \"X\", \"enum2\": \"A\", \"enum3\": null, \"enum4\": [\"SAT\", \"SUN\"]}}";
BSONObject object = (BSONObject) JSON.parse(mongoJson);
Record record2 = RecordConverter.toRecord(schema, object, getClass().getClassLoader());
assertThat(record2, is(record1));
assertThat(AvroHelper.toSimpleJson(schema, record2), is(AvroHelper.toSimpleJson(schema, record1)));
}
示例15: testUnions
import org.apache.avro.generic.GenericData.Record; //导入依赖的package包/类
@Test
public void testUnions() throws Exception {
Schema schema = Unions.SCHEMA$;
String avroJson = "{\"union1\": {\"int\": 1}, \"union2\": {\"test.Union2\": {\"union21\": {\"long\": 2}}}, \"union3\": {\"array\": [{\"boolean\": true}, {\"boolean\": false}, {\"null\": null}]}, \"union4\": {\"map\": {\"a\": {\"string\": \"A\"}, \"b\": {\"string\": \"B\"}, \"c\": {\"string\": \"C\"}}}, \"union5\": {\"null\": null}, \"union6\": {\"null\": null}}";
Decoder decoder = DecoderFactory.get().jsonDecoder(schema, avroJson);
GenericDatumReader<Record> reader = new GenericDatumReader<Record>(schema);
Record record1 = reader.read(null, decoder);
String mongoJson = "{\"union1\": 1, \"union2\": {\"union21\": 2}, \"union3\": [true, false, null], \"union4\": {\"a\": \"A\", \"b\": \"B\", \"c\": \"C\"}, \"union5\": null, \"union6\": null}";
DBObject object = (DBObject) JSON.parse(mongoJson);
Record record2 = RecordConverter.toRecord(schema, object, getClass().getClassLoader());
assertThat(record2, is(record1));
assertThat(AvroHelper.toSimpleJson(schema, record2), is(AvroHelper.toSimpleJson(schema, record1)));
}