2016-07-28 7 views
5

私はObjective-Cでプログラミングしています。データのシリアル化にはApache Avroを使用しています。C言語のアブロ・アレイ・タイプに任意のデータを設定

マイアブロスキーマはこれです:私のObjective-Cのコードでは

{ 
"name": "School", 
"type":"record", 
"fields":[ 
    { 
    "name":"Employees", 
    "type":["null", {"type": "array", 
        "items":{ 
         "name":"Teacher", 
         "type":"record", 
         "fields":[ 
          {"name":"name", "type":"string"} 
          {"name":"age", "type":"int"} 
         ] 
        } 
        } 
      ], 
    "default":null 
    } 
] 
} 

、私はTeacherオブジェクトの配列を持って、各教師のオブジェクトは、name & ageの値が含まれています。

上記のスキーマでAvroを使用して教師配列データをファイルに書きたいと思います。私は主に、上記のスキーマで定義された配列Employeesにデータを書き込む方法について懸念しています。

はここに私のコードです(私はそれを行うにはCスタイルのコードを使用する必要があり、私はAvro C documentation従ってください):

// I don't show this function, it constructs the a `avro_value_t` based on the schema. No problem here. 
avro_value_t school = [self constructSchoolValueForSchema]; 

// get "Employees" field 
avro_value_t employees; 
avro_value_get_by_name(school, "employees", &employees, 0); 

int idx = 0; 
for (Teacher *teacher in teacherArray) { 
    // get name and age 
    NSString *name = teacher.name; 
    int age = teacher.age; 

    // set value to avro data type. 
    // here 'unionField' is the field of 'Employees', it is a Avro union type which is either null or an array as defined in schema above 
    avro_value_t field, unionField; 
    avro_value_set_branch(&employees, 1, &unionField); 
    // based on documentation, I should use 'avro_value_append' 
    avro_value_append(&employees, name, idx); 
    // I get confused here!!!! 
    // in above line of code, I append 'name' to 'employees', 
    //which looks not correct, 
    // because the 'Employees' array is an array of 'Teacher', not arrary of 'name' 
    // What is the correct way to add teacher to 'employees' ? 

    idx ++; 
} 

私が聞きたい質問は、上記のコードのコメントに実際にあります。

私はそのAvro Cのドキュメントに従っていますが、どうすればteacheremployeesに追加できますか?私の上記のコードでは、各教師のnameemployees配列に追加しました。

答えて

1

あなたのコードには2つの問題があると思いますが、私はAvroに慣れていないので、そのうちの1つを保証することはできません。私はちょうどすぐに私がavro_value_appendを理解し、どのようにリンクされドキュメントを覗くと、ここにある:それ

は新しい要素、すなわち教師と戻り作成しているので、それは返し-参照することにより(第2パラメータで参照を経由して)。私の推測では、その要素を埋めるために他の方法を使う必要がある(つまり、教師の名前を設定するなど)。最後に、この操作を行います。

avro_value_t teacher; 
size_t idx; 
avro_value_append(&employees, &teacher, &idx); // note that idx is also returned by reference and now contains the new elements index 

私はあなたが正しく従業員を設定した場合、ところで、私はそれに見て時間を持っていなかったわかりません。

nameをある点で使用すると2番目の問題が発生します。私はAvroがCの文字列を期待していると仮定しますが、ここではNSStringを使用しています。 getCString:maxLength:encoding:メソッドを使用してAvro内で渡すことができるC文字列を作成するための準備済みのバッファを埋める必要があります。おそらくUTF8Stringを使用することもできますが、そのドキュメントを読んでください:メモリ(memcpy shenanigans)をコピーする必要があります。そうしないと、Avroコンテナはデータを足元から拭き取ります。

関連する問題