@Override
  public int run(String[] args) throws Exception {

    // Construct a local filesystem dataset repository rooted at /tmp/data
    DatasetRepository repo =
        new FileSystemDatasetRepository.Builder()
            .rootDirectory(new URI("/tmp/data"))
            .configuration(getConf())
            .get();

    // Create a dataset of products with the Avro schema in the repository
    DatasetDescriptor descriptor = new DatasetDescriptor.Builder().schema(Product.class).get();
    Dataset products = repo.create("products", descriptor);

    // Get a writer for the dataset and write some products to it
    DatasetWriter<Product> writer = products.getWriter();
    try {
      writer.open();
      String[] names = {"toaster", "teapot", "butter dish"};
      int i = 0;
      for (String name : names) {
        Product product = new Product();
        product.setName(name);
        product.setId(i++);
        writer.write(product);
      }
    } finally {
      writer.close();
    }

    return 0;
  }
Esempio n. 2
0
  @Override
  public int run(String[] args) throws Exception {

    // Construct a local filesystem dataset repository rooted at /tmp/data
    DatasetRepository repo =
        new FileSystemDatasetRepository.Builder()
            .rootDirectory(new URI("/tmp/data"))
            .configuration(getConf())
            .get();

    // Get the events dataset
    Dataset events = repo.get("events");

    // Get a reader for the dataset and read all the events
    DatasetReader<GenericRecord> reader = events.getReader();
    try {
      reader.open();
      while (reader.hasNext()) {
        GenericRecord event = reader.read();
        System.out.println(event);
      }
    } finally {
      reader.close();
    }

    return 0;
  }