Пример #1
0
/*输出数据库中的学生信息*/
int show_student(avro_file_reader_t db,
                 avro_schema_t reader_schema)
{
    int rval;
    avro_datum_t student;

    rval = avro_file_reader_read(db, reader_schema, &student);

    if (rval == 0) {
        int64_t i64;
        int32_t i32;
        char *p;
        avro_datum_t sid_datum, name_datum, dept_datum,
                     phone_datum, age_datum;

        if (avro_record_get(student, "SID", &sid_datum) == 0) {
            avro_int64_get(sid_datum, &i64);
            fprintf(stdout, "%"PRId64"  ", i64);
        }
        if (avro_record_get(student, "Name", &name_datum) == 0) {
            avro_string_get(name_datum, &p);
            fprintf(stdout, "%12s  ", p);
        }
        if (avro_record_get(student, "Dept", &dept_datum) == 0) {
            avro_string_get(dept_datum, &p);
            fprintf(stdout, "%12s  ", p);
        }
        if (avro_record_get(student, "Phone", &phone_datum) == 0) {
            avro_string_get(phone_datum, &p);
            fprintf(stdout, "%12s  ", p);
        }
        if (avro_record_get(student, "Age", &age_datum) == 0) {
            avro_int32_get(age_datum, &i32);
            fprintf(stdout, "%d", i32);
        }
        fprintf(stdout, "\n");

        /*释放记录*/
        avro_datum_decref(student);
    }
    return rval;
}
Пример #2
0
static int test_nested_record(void)
{
    const char  *json =
        "{"
        "  \"type\": \"record\","
        "  \"name\": \"list\","
        "  \"fields\": ["
        "    { \"name\": \"x\", \"type\": \"int\" },"
        "    { \"name\": \"y\", \"type\": \"int\" },"
        "    { \"name\": \"next\", \"type\": [\"null\",\"list\"]}"
        "  ]"
        "}";

    int  rval;

    avro_schema_t schema = NULL;
    avro_schema_error_t error;
    avro_schema_from_json(json, strlen(json), &schema, &error);

    avro_datum_t  head = avro_datum_from_schema(schema);
    avro_record_set_field_value(rval, head, int32, "x", 10);
    avro_record_set_field_value(rval, head, int32, "y", 10);

    avro_datum_t  next = NULL;
    avro_datum_t  tail = NULL;

    avro_record_get(head, "next", &next);
    avro_union_set_discriminant(next, 1, &tail);
    avro_record_set_field_value(rval, tail, int32, "x", 20);
    avro_record_set_field_value(rval, tail, int32, "y", 20);

    avro_record_get(tail, "next", &next);
    avro_union_set_discriminant(next, 0, NULL);

    write_read_check(schema, head, NULL, NULL, "nested record");

    avro_schema_decref(schema);
    avro_datum_decref(head);

    return 0;
}
Пример #3
0
static int
write_record(avro_writer_t writer, const avro_encoding_t * enc,
	     struct avro_record_schema_t *schema, avro_datum_t datum)
{
	int rval;
	long i;
	avro_datum_t field_datum;

	if (schema) {
		for (i = 0; i < schema->fields->num_entries; i++) {
			union {
				st_data_t data;
				struct avro_record_field_t *field;
			} val;
			st_lookup(schema->fields, i, &val.data);
			check(rval,
			      avro_record_get(datum, val.field->name,
					      &field_datum));
			check(rval,
			      write_datum(writer, enc, val.field->type,
					  field_datum));
		}
	} else {
		/* No schema.  Just write the record datum */
		struct avro_record_datum_t *record =
		    avro_datum_to_record(datum);
		for (i = 0; i < record->field_order->num_entries; i++) {
			union {
				st_data_t data;
				char *name;
			} val;
			st_lookup(record->field_order, i, &val.data);
			check(rval,
			      avro_record_get(datum, val.name, &field_datum));
			check(rval,
			      write_datum(writer, enc, NULL, field_datum));
		}
	}
	return 0;
}
Пример #4
0
static int
avro_datum_value_get_by_index(const avro_value_iface_t *iface,
			      const void *vself, size_t index,
			      avro_value_t *child, const char **name)
{
	AVRO_UNUSED(iface);
	const avro_datum_t  self = (const avro_datum_t) vself;
	check_param(EINVAL, self, "datum instance");

	int  rval;
	avro_datum_t  child_datum;

	if (is_avro_array(self)) {
		check(rval, avro_array_get(self, index, &child_datum));
		return avro_datum_as_child_value(child, child_datum);
	}

	if (is_avro_map(self)) {
		const char  *real_key;
		check(rval, avro_map_get_key(self, index, &real_key));
		if (name != NULL) {
			*name = real_key;
		}
		check(rval, avro_map_get(self, real_key, &child_datum));
		return avro_datum_as_child_value(child, child_datum);
	}

	if (is_avro_record(self)) {
		avro_schema_t  schema = avro_datum_get_schema(self);
		const char  *field_name =
		    avro_schema_record_field_name(schema, index);
		if (field_name == NULL) {
			return EINVAL;
		}
		if (name != NULL) {
			*name = field_name;
		}
		check(rval, avro_record_get(self, field_name, &child_datum));
		return avro_datum_as_child_value(child, child_datum);
	}

	avro_set_error("Can only get by index from array, map, or record");
	return EINVAL;
}
Пример #5
0
static int read_data_datum() {
	int rval;
	int records_read = 0;

	avro_file_reader_t reader;
	avro_datum_t datum;

	fprintf(stderr, "\nReading...\n");

	rval = avro_file_reader(filename, &reader);

	if (rval) {
		fprintf(stderr, "Error using 'datum': %s\n", avro_strerror());
		return -1;
	}

	avro_schema_t schema = avro_file_reader_get_writer_schema(reader);

	while ((rval = avro_file_reader_read(reader, schema, &datum)) == 0) {
		avro_datum_t val_datum;
		int32_t val;
		if (avro_record_get(datum, "ab", &val_datum)) {
			fprintf(stderr, "Error getting value: %s\n", avro_strerror());
			return -1;
		}
		avro_int32_get(val_datum, &val);
		fprintf(stderr, "value = %d\n", val);
		records_read++;
		avro_datum_decref(datum);
	}

	avro_schema_decref(schema);
	avro_file_reader_close(reader);

	fprintf(stderr, "read %d records using 'datum'.\n", records_read);

	if (rval != EOF) {
		fprintf(stderr, "Error using 'datum': %s\n", avro_strerror());
		return -1;
	}

	return records_read;
}
Пример #6
0
static int
avro_datum_value_get_by_name(const avro_value_iface_t *iface,
			     const void *vself, const char *name,
			     avro_value_t *child, size_t *index)
{
	AVRO_UNUSED(iface);
	const avro_datum_t  self = (const avro_datum_t) vself;
	check_param(EINVAL, self, "datum instance");

	int  rval;
	avro_datum_t  child_datum;

	if (is_avro_map(self)) {
		if (index != NULL) {
			int  real_index;
			check(rval, avro_map_get_index(self, name, &real_index));
			*index = real_index;
		}

		check(rval, avro_map_get(self, name, &child_datum));
		return avro_datum_as_child_value(child, child_datum);
	}

	if (is_avro_record(self)) {
		if (index != NULL) {
			avro_schema_t  schema = avro_datum_get_schema(self);
			*index = avro_schema_record_field_get_index(schema, name);
		}

		check(rval, avro_record_get(self, name, &child_datum));
		return avro_datum_as_child_value(child, child_datum);
	}

	avro_set_error("Can only get by name from map or record");
	return EINVAL;
}