| 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 |
import java.util.Random; |
| 24 |
|
| 25 |
import org.apache.avro.file.DataFileWriter; |
| 26 |
import org.apache.avro.io.DatumWriter; |
| 27 |
import org.apache.avro.specific.SpecificDatumWriter; |
| 28 |
|
| 29 |
import example.avro.User; |
| 30 |
|
| 31 |
public class GenerateData { |
| 32 |
public static final String[] COLORS = {"red", "orange", "yellow", "green", "blue", "purple", null}; |
| 33 |
public static final int USERS = 20; |
| 34 |
public static final String PATH = "./input/users.avro"; |
| 35 |
|
| 36 |
public static void main(String[] args) throws IOException { |
| 37 |
// Open data file |
| 38 |
File file = new File(PATH); |
| 39 |
if (file.getParentFile() != null) { |
| 40 |
file.getParentFile().mkdirs(); |
| 41 |
} |
| 42 |
DatumWriter<User> userDatumWriter = new SpecificDatumWriter<User>(User.class); |
| 43 |
DataFileWriter<User> dataFileWriter = new DataFileWriter<User>(userDatumWriter); |
| 44 |
dataFileWriter.create(User.SCHEMA$, file); |
| 45 |
|
| 46 |
// Create random users |
| 47 |
User user; |
| 48 |
Random random = new Random(); |
| 49 |
for (int i = 0; i < USERS; i++) { |
| 50 |
user = new User("user", null, COLORS[random.nextInt(COLORS.length)]); |
| 51 |
dataFileWriter.append(user); |
| 52 |
System.out.println(user); |
| 53 |
} |
| 54 |
|
| 55 |
dataFileWriter.close(); |
| 56 |
} |
| 57 |
} |