| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
* or more contributor license agreements. See the NOTICE file |
| 4 |
* distributed with this work for additional information |
| 5 |
* regarding copyright ownership. The ASF licenses this file |
| 6 |
* to you under the Apache License, Version 2.0 (the |
| 7 |
* "License"); you may not use this file except in compliance |
| 8 |
* with the License. You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* https://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, software |
| 13 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
* See the License for the specific language governing permissions and |
| 16 |
* limitations under the License. |
| 17 |
*/ |
| 18 |
|
| 19 |
package example; |
| 20 |
|
| 21 |
import java.io.File; |
| 22 |
import java.io.IOException; |
| 23 |
|
| 24 |
import org.apache.avro.file.DataFileReader; |
| 25 |
import org.apache.avro.file.DataFileWriter; |
| 26 |
import org.apache.avro.io.DatumReader; |
| 27 |
import org.apache.avro.io.DatumWriter; |
| 28 |
import org.apache.avro.specific.SpecificDatumReader; |
| 29 |
import org.apache.avro.specific.SpecificDatumWriter; |
| 30 |
|
| 31 |
import example.avro.User; |
| 32 |
|
| 33 |
public class SpecificMain { |
| 34 |
public static void main(String[] args) throws IOException { |
| 35 |
User user1 = new User(); |
| 36 |
user1.setName("Alyssa"); |
| 37 |
user1.setFavoriteNumber(256); |
| 38 |
// Leave favorite color null |
| 39 |
|
| 40 |
// Alternate constructor |
| 41 |
User user2 = new User("Ben", 7, "red"); |
| 42 |
|
| 43 |
// Construct via builder |
| 44 |
User user3 = User.newBuilder() |
| 45 |
.setName("Charlie") |
| 46 |
.setFavoriteColor("blue") |
| 47 |
.setFavoriteNumber(null) |
| 48 |
.build(); |
| 49 |
|
| 50 |
// Serialize user1 and user2 to disk |
| 51 |
File file = new File("users.avro"); |
| 52 |
DatumWriter<User> userDatumWriter = new SpecificDatumWriter<User>(User.class); |
| 53 |
DataFileWriter<User> dataFileWriter = new DataFileWriter<User>(userDatumWriter); |
| 54 |
dataFileWriter.create(user1.getSchema(), file); |
| 55 |
dataFileWriter.append(user1); |
| 56 |
dataFileWriter.append(user2); |
| 57 |
dataFileWriter.append(user3); |
| 58 |
dataFileWriter.close(); |
| 59 |
|
| 60 |
// Deserialize Users from disk |
| 61 |
DatumReader<User> userDatumReader = new SpecificDatumReader<User>(User.class); |
| 62 |
User user = null; |
| 63 |
try(DataFileReader<User> dataFileReader = new DataFileReader<User>(file, userDatumReader)){ |
| 64 |
while (dataFileReader.hasNext()) { |
| 65 |
// Reuse user object by passing it to next(). This saves us from |
| 66 |
// allocating and garbage collecting many objects for files with |
| 67 |
// many items. |
| 68 |
user = dataFileReader.next(user); |
| 69 |
System.out.println(user); |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
} |