本文整理了Java中org.apache.avro.LogicalTypes.uuid()
方法的一些代码示例,展示了LogicalTypes.uuid()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。LogicalTypes.uuid()
方法的具体详情如下:
包路径:org.apache.avro.LogicalTypes
类名称:LogicalTypes
方法名:uuid
暂无
代码示例来源:origin: apache/avro
@Override
public Schema getRecommendedSchema() {
return LogicalTypes.uuid().addToSchema(Schema.create(Schema.Type.STRING));
}
代码示例来源:origin: org.apache.avro/avro
@Override
public Schema getRecommendedSchema() {
return LogicalTypes.uuid().addToSchema(Schema.create(Schema.Type.STRING));
}
代码示例来源:origin: apache/avro
@Test
public void testReadUUID() throws IOException {
Schema uuidSchema = Schema.create(Schema.Type.STRING);
LogicalTypes.uuid().addToSchema(uuidSchema);
UUID u1 = UUID.randomUUID();
UUID u2 = UUID.randomUUID();
List<UUID> expected = Arrays.asList(u1, u2);
File test = write(Schema.create(Schema.Type.STRING),
u1.toString(), u2.toString());
Assert.assertEquals("Should convert Strings to UUIDs",
expected, read(GENERIC.createDatumReader(uuidSchema), test));
}
代码示例来源:origin: apache/avro
@Test
public void testCopyUuid() {
testCopy(LogicalTypes.uuid().addToSchema(Schema.create(Schema.Type.STRING)),
UUID.randomUUID(),
GENERIC);
}
代码示例来源:origin: apache/avro
@Test
public void testCopyUuidRaw() {
testCopy(LogicalTypes.uuid().addToSchema(Schema.create(Schema.Type.STRING)),
UUID.randomUUID().toString(), // use raw type
GenericData.get()); // with no conversions
}
代码示例来源:origin: apache/avro
@Test
public void testWriteUUID() throws IOException {
Schema stringSchema = Schema.create(Schema.Type.STRING);
stringSchema.addProp(GenericData.STRING_PROP, "String");
Schema uuidSchema = Schema.create(Schema.Type.STRING);
LogicalTypes.uuid().addToSchema(uuidSchema);
UUID u1 = UUID.randomUUID();
UUID u2 = UUID.randomUUID();
List<String> expected = Arrays.asList(u1.toString(), u2.toString());
File test = write(GENERIC, uuidSchema, u1, u2);
Assert.assertEquals("Should read UUIDs as Strings",
expected, read(GenericData.get().createDatumReader(stringSchema), test));
}
代码示例来源:origin: apache/avro
@Test
public void testReadUUIDList() throws IOException {
Schema uuidListSchema = SchemaBuilder.record(RecordWithUUIDList.class.getName())
.fields()
.name("uuids").type().array().items().stringType().noDefault()
.endRecord();
uuidListSchema.getField("uuids").schema().addProp(
SpecificData.CLASS_PROP, List.class.getName());
LogicalTypes.uuid().addToSchema(
uuidListSchema.getField("uuids").schema().getElementType());
UUID u1 = UUID.randomUUID();
UUID u2 = UUID.randomUUID();
GenericRecord r = new GenericData.Record(uuidListSchema);
r.put("uuids", Arrays.asList(u1.toString(), u2.toString()));
RecordWithUUIDList expected = new RecordWithUUIDList();
expected.uuids = Arrays.asList(u1, u2);
File test = write(uuidListSchema, r);
Assert.assertEquals("Should convert Strings to UUIDs",
expected, read(REFLECT.createDatumReader(uuidListSchema), test).get(0));
}
代码示例来源:origin: apache/avro
@Test
public void testWriteNullableUUID() throws IOException {
Schema nullableUuidSchema = SchemaBuilder.record(RecordWithUUID.class.getName())
.fields().optionalString("uuid").endRecord();
LogicalTypes.uuid().addToSchema(
nullableUuidSchema.getField("uuid").schema().getTypes().get(1));
UUID u1 = UUID.randomUUID();
UUID u2 = UUID.randomUUID();
RecordWithUUID r1 = new RecordWithUUID();
r1.uuid = u1;
RecordWithUUID r2 = new RecordWithUUID();
r2.uuid = u2;
List<RecordWithStringUUID> expected = Arrays.asList(
new RecordWithStringUUID(), new RecordWithStringUUID());
expected.get(0).uuid = u1.toString();
expected.get(1).uuid = u2.toString();
File test = write(REFLECT, nullableUuidSchema, r1, r2);
// verify that the field's type overrides the logical type
Schema nullableUuidStringSchema = SchemaBuilder
.record(RecordWithStringUUID.class.getName())
.fields().optionalString("uuid").endRecord();
Assert.assertEquals("Should read uuid as String without UUID conversion",
expected,
read(ReflectData.get().createDatumReader(nullableUuidStringSchema), test));
}
代码示例来源:origin: apache/avro
@Test
public void testWriteNullableUUIDReadRequiredString() throws IOException {
Schema nullableUuidSchema = SchemaBuilder.record(RecordWithUUID.class.getName())
.fields().optionalString("uuid").endRecord();
LogicalTypes.uuid().addToSchema(
nullableUuidSchema.getField("uuid").schema().getTypes().get(1));
UUID u1 = UUID.randomUUID();
UUID u2 = UUID.randomUUID();
RecordWithUUID r1 = new RecordWithUUID();
r1.uuid = u1;
RecordWithUUID r2 = new RecordWithUUID();
r2.uuid = u2;
List<RecordWithStringUUID> expected = Arrays.asList(
new RecordWithStringUUID(), new RecordWithStringUUID());
expected.get(0).uuid = u1.toString();
expected.get(1).uuid = u2.toString();
File test = write(REFLECT, nullableUuidSchema, r1, r2);
// verify that the field's type overrides the logical type
Schema uuidStringSchema = SchemaBuilder
.record(RecordWithStringUUID.class.getName())
.fields().requiredString("uuid").endRecord();
Assert.assertEquals("Should read uuid as String without UUID conversion",
expected,
read(REFLECT.createDatumReader(uuidStringSchema), test));
}
代码示例来源:origin: apache/avro
@Test
public void testReadUUIDArray() throws IOException {
Schema uuidArraySchema = SchemaBuilder.record(RecordWithUUIDArray.class.getName())
.fields()
.name("uuids").type().array().items().stringType().noDefault()
.endRecord();
LogicalTypes.uuid().addToSchema(
uuidArraySchema.getField("uuids").schema().getElementType());
UUID u1 = UUID.randomUUID();
UUID u2 = UUID.randomUUID();
GenericRecord r = new GenericData.Record(uuidArraySchema);
r.put("uuids", Arrays.asList(u1.toString(), u2.toString()));
RecordWithUUIDArray expected = new RecordWithUUIDArray();
expected.uuids = new UUID[] {u1, u2};
File test = write(uuidArraySchema, r);
Assert.assertEquals("Should convert Strings to UUIDs",
expected,
read(REFLECT.createDatumReader(uuidArraySchema), test).get(0));
}
代码示例来源:origin: apache/avro
@Test(expected = IllegalArgumentException.class)
public void testReadUUIDMissingLogicalTypeReflect() throws IOException {
String unsafeValue = System.getProperty("avro.disable.unsafe");
try {
// only one FieldAccess can be set per JVM
System.setProperty("avro.disable.unsafe", "true");
Assume.assumeTrue(
ReflectionUtil.getFieldAccess() instanceof FieldAccessReflect);
Schema uuidSchema = SchemaBuilder.record(RecordWithUUID.class.getName())
.fields().requiredString("uuid").endRecord();
LogicalTypes.uuid().addToSchema(uuidSchema.getField("uuid").schema());
UUID u1 = UUID.randomUUID();
RecordWithStringUUID r1 = new RecordWithStringUUID();
r1.uuid = u1.toString();
File test = write(
ReflectData.get().getSchema(RecordWithStringUUID.class), r1);
read(ReflectData.get().createDatumReader(uuidSchema), test).get(0);
} finally {
if (unsafeValue != null) {
System.setProperty("avro.disable.unsafe", unsafeValue);
}
}
}
代码示例来源:origin: apache/avro
@Test
public void testReadUUIDGenericRecord() throws IOException {
Schema uuidSchema = SchemaBuilder.record("RecordWithUUID")
.fields().requiredString("uuid").endRecord();
LogicalTypes.uuid().addToSchema(uuidSchema.getField("uuid").schema());
UUID u1 = UUID.randomUUID();
UUID u2 = UUID.randomUUID();
RecordWithStringUUID r1 = new RecordWithStringUUID();
r1.uuid = u1.toString();
RecordWithStringUUID r2 = new RecordWithStringUUID();
r2.uuid = u2.toString();
List<GenericData.Record> expected = Arrays.asList(
new GenericData.Record(uuidSchema), new GenericData.Record(uuidSchema));
expected.get(0).put("uuid", u1);
expected.get(1).put("uuid", u2);
File test = write(
ReflectData.get().getSchema(RecordWithStringUUID.class), r1, r2);
Assert.assertEquals("Should convert Strings to UUIDs",
expected, read(REFLECT.createDatumReader(uuidSchema), test));
// verify that the field's type overrides the logical type
Schema uuidStringSchema = SchemaBuilder
.record(RecordWithStringUUID.class.getName())
.fields().requiredString("uuid").endRecord();
LogicalTypes.uuid().addToSchema(uuidSchema.getField("uuid").schema());
Assert.assertEquals("Should not convert to UUID if accessor is String",
Arrays.asList(r1, r2),
read(REFLECT.createDatumReader(uuidStringSchema), test));
}
代码示例来源:origin: apache/avro
@Test
public void testWriteNullableUUID() throws IOException {
Schema stringSchema = Schema.create(Schema.Type.STRING);
stringSchema.addProp(GenericData.STRING_PROP, "String");
Schema nullableStringSchema = Schema.createUnion(
Schema.create(Schema.Type.NULL), stringSchema);
Schema uuidSchema = Schema.create(Schema.Type.STRING);
LogicalTypes.uuid().addToSchema(uuidSchema);
Schema nullableUuidSchema = Schema.createUnion(
Schema.create(Schema.Type.NULL), uuidSchema);
UUID u1 = UUID.randomUUID();
UUID u2 = UUID.randomUUID();
List<String> expected = Arrays.asList(u1.toString(), u2.toString());
File test = write(GENERIC, nullableUuidSchema, u1, u2);
Assert.assertEquals("Should read UUIDs as Strings",
expected,
read(GenericData.get().createDatumReader(nullableStringSchema), test));
}
代码示例来源:origin: apache/avro
@Test
public void testWriteUUID() throws IOException {
Schema uuidSchema = SchemaBuilder.record(RecordWithUUID.class.getName())
.fields().requiredString("uuid").endRecord();
LogicalTypes.uuid().addToSchema(uuidSchema.getField("uuid").schema());
UUID u1 = UUID.randomUUID();
UUID u2 = UUID.randomUUID();
RecordWithUUID r1 = new RecordWithUUID();
r1.uuid = u1;
RecordWithUUID r2 = new RecordWithUUID();
r2.uuid = u2;
List<RecordWithStringUUID> expected = Arrays.asList(
new RecordWithStringUUID(), new RecordWithStringUUID());
expected.get(0).uuid = u1.toString();
expected.get(1).uuid = u2.toString();
File test = write(REFLECT, uuidSchema, r1, r2);
// verify that the field's type overrides the logical type
Schema uuidStringSchema = SchemaBuilder
.record(RecordWithStringUUID.class.getName())
.fields().requiredString("uuid").endRecord();
Assert.assertEquals("Should read uuid as String without UUID conversion",
expected,
read(REFLECT.createDatumReader(uuidStringSchema), test));
LogicalTypes.uuid().addToSchema(uuidStringSchema.getField("uuid").schema());
Assert.assertEquals("Should read uuid as String without UUID logical type",
expected,
read(ReflectData.get().createDatumReader(uuidStringSchema), test));
}
代码示例来源:origin: apache/avro
@Test
public void testReadUUIDMissingLogicalTypeUnsafe() throws IOException {
String unsafeValue = System.getProperty("avro.disable.unsafe");
try {
// only one FieldAccess can be set per JVM
System.clearProperty("avro.disable.unsafe");
Assume.assumeTrue(
ReflectionUtil.getFieldAccess() instanceof FieldAccessUnsafe);
Schema uuidSchema = SchemaBuilder.record(RecordWithUUID.class.getName())
.fields().requiredString("uuid").endRecord();
LogicalTypes.uuid().addToSchema(uuidSchema.getField("uuid").schema());
UUID u1 = UUID.randomUUID();
RecordWithStringUUID r1 = new RecordWithStringUUID();
r1.uuid = u1.toString();
File test = write(
ReflectData.get().getSchema(RecordWithStringUUID.class), r1);
RecordWithUUID datum = (RecordWithUUID) read(
ReflectData.get().createDatumReader(uuidSchema), test).get(0);
Object uuid = datum.uuid;
Assert.assertTrue("UUID should be a String (unsafe)",
uuid instanceof String);
} finally {
if (unsafeValue != null) {
System.setProperty("avro.disable.unsafe", unsafeValue);
}
}
}
代码示例来源:origin: apache/avro
@Test
public void testReadUUID() throws IOException {
Schema uuidSchema = SchemaBuilder.record(RecordWithUUID.class.getName())
.fields().requiredString("uuid").endRecord();
LogicalTypes.uuid().addToSchema(uuidSchema.getField("uuid").schema());
UUID u1 = UUID.randomUUID();
UUID u2 = UUID.randomUUID();
RecordWithStringUUID r1 = new RecordWithStringUUID();
r1.uuid = u1.toString();
RecordWithStringUUID r2 = new RecordWithStringUUID();
r2.uuid = u2.toString();
List<RecordWithUUID> expected = Arrays.asList(
new RecordWithUUID(), new RecordWithUUID());
expected.get(0).uuid = u1;
expected.get(1).uuid = u2;
File test = write(
ReflectData.get().getSchema(RecordWithStringUUID.class), r1, r2);
Assert.assertEquals("Should convert Strings to UUIDs",
expected, read(REFLECT.createDatumReader(uuidSchema), test));
// verify that the field's type overrides the logical type
Schema uuidStringSchema = SchemaBuilder
.record(RecordWithStringUUID.class.getName())
.fields().requiredString("uuid").endRecord();
LogicalTypes.uuid().addToSchema(uuidStringSchema.getField("uuid").schema());
Assert.assertEquals("Should not convert to UUID if accessor is String",
Arrays.asList(r1, r2),
read(REFLECT.createDatumReader(uuidStringSchema), test));
}
代码示例来源:origin: apache/avro
@Test
public void testReflectedSchema() {
Schema expected = SchemaBuilder.record(RecordWithUUIDList.class.getName())
.fields()
.name("uuids").type().array().items().stringType().noDefault()
.endRecord();
expected.getField("uuids").schema().addProp(
SpecificData.CLASS_PROP, List.class.getName());
LogicalTypes.uuid().addToSchema(
expected.getField("uuids").schema().getElementType());
Schema actual = REFLECT.getSchema(RecordWithUUIDList.class);
Assert.assertEquals("Should use the UUID logical type", expected, actual);
}
代码示例来源:origin: apache/avro
@Test
public void testWriteUUIDList() throws IOException {
Schema uuidListSchema = SchemaBuilder.record(RecordWithUUIDList.class.getName())
.fields()
.name("uuids").type().array().items().stringType().noDefault()
.endRecord();
uuidListSchema.getField("uuids").schema().addProp(
SpecificData.CLASS_PROP, List.class.getName());
LogicalTypes.uuid().addToSchema(
uuidListSchema.getField("uuids").schema().getElementType());
Schema stringArraySchema = SchemaBuilder.record("RecordWithUUIDArray")
.fields()
.name("uuids").type().array().items().stringType().noDefault()
.endRecord();
stringArraySchema.getField("uuids").schema()
.addProp(SpecificData.CLASS_PROP, List.class.getName());
UUID u1 = UUID.randomUUID();
UUID u2 = UUID.randomUUID();
GenericRecord expected = new GenericData.Record(stringArraySchema);
expected.put("uuids", Arrays.asList(u1.toString(), u2.toString()));
RecordWithUUIDList r = new RecordWithUUIDList();
r.uuids = Arrays.asList(u1, u2);
File test = write(REFLECT, uuidListSchema, r);
Assert.assertEquals("Should read UUIDs as Strings",
expected,
read(REFLECT.createDatumReader(stringArraySchema), test).get(0));
}
代码示例来源:origin: apache/avro
@Test(expected = DataFileWriter.AppendWriteException.class)
public void testWriteUUIDMissingLogicalType() throws IOException {
Schema uuidSchema = SchemaBuilder.record(RecordWithUUID.class.getName())
.fields().requiredString("uuid").endRecord();
LogicalTypes.uuid().addToSchema(uuidSchema.getField("uuid").schema());
UUID u1 = UUID.randomUUID();
UUID u2 = UUID.randomUUID();
RecordWithUUID r1 = new RecordWithUUID();
r1.uuid = u1;
RecordWithUUID r2 = new RecordWithUUID();
r2.uuid = u2;
// write without using REFLECT, which has the logical type
File test = write(uuidSchema, r1, r2);
// verify that the field's type overrides the logical type
Schema uuidStringSchema = SchemaBuilder
.record(RecordWithStringUUID.class.getName())
.fields().requiredString("uuid").endRecord();
// this fails with an AppendWriteException wrapping ClassCastException
// because the UUID isn't converted to a CharSequence expected internally
read(ReflectData.get().createDatumReader(uuidStringSchema), test);
}
代码示例来源:origin: apache/avro
@Test
public void testLogicalTypeEquals() {
LogicalTypes.Decimal decimal90 = LogicalTypes.decimal(9);
LogicalTypes.Decimal decimal80 = LogicalTypes.decimal(8);
LogicalTypes.Decimal decimal92 = LogicalTypes.decimal(9, 2);
assertEqualsTrue("Same decimal", LogicalTypes.decimal(9, 0), decimal90);
assertEqualsTrue("Same decimal", LogicalTypes.decimal(8, 0), decimal80);
assertEqualsTrue("Same decimal", LogicalTypes.decimal(9, 2), decimal92);
assertEqualsFalse("Different logical type", LogicalTypes.uuid(), decimal90);
assertEqualsFalse("Different precision", decimal90, decimal80);
assertEqualsFalse("Different scale", decimal90, decimal92);
}
内容来源于网络,如有侵权,请联系作者删除!