Skip to content

Instantly share code, notes, and snippets.

@jayunit100
Created February 25, 2015 21:56
Show Gist options
  • Save jayunit100/ddd4abb801dbbb8152d1 to your computer and use it in GitHub Desktop.
Save jayunit100/ddd4abb801dbbb8152d1 to your computer and use it in GitHub Desktop.
public Iterator<Transaction> iterateData() throws Throwable {
final InputData inputData = new DataLoader().loadData();
final SeedFactory seedFactory = new SeedFactory(seed);
System.out.println("Generating stores...");
final ArrayList<Store> stores = new ArrayList<Store>();
final StoreGenerator storeGenerator = new StoreGenerator(inputData, seedFactory);
for (int i = 0; i < nStores; i++) {
Store store = storeGenerator.generate();
stores.add(store);
}
final List<Customer> customers = Lists.newArrayList();
final CustomerGenerator custGen = new CustomerGenerator(inputData, stores, seedFactory);
for (int i = 0; i < nCustomers; i++) {
Customer customer = custGen.generate();
customers.add(customer);
}
Long nextSeed = seedFactory.getNextSeed();
Iterator<Transaction> transactions = new Iterator<Transaction>() {
long index = 0;
Iterator<Customer> iter = customers.iterator();
@Override
public void remove() {
throw new RuntimeException("unsupported");
}
@Override
public boolean hasNext() {
if(index%1000==0){
System.out.println("Transaction: " + index);
}
return true;
}
@Override
public Transaction next() {
try {
index++;
Iterator<Customer> custIter = customers.iterator();
Collection<ProductCategory> products = inputData.getProductCategories();
Customer cust = custIter.next();
//Create a new purchasing profile.
PurchasingProfileGenerator profileGen = new PurchasingProfileGenerator(products, seedFactory);
PurchasingProfile profile = profileGen.generate();
TransactionGenerator transGen = new TransactionGenerator(cust, profile, stores, products, seedFactory);
List<Transaction> transactions = Lists.newArrayList();
return transGen.generate();
} catch (Throwable t) {
t.printStackTrace();
throw new RuntimeException("See above failure. failed creating next transaction due to " + t.getMessage() + " " + t);
}
}
};
/**
* Return the iterator.
*/
return transactions;
}
@rnowling
Copy link

Awesome start to a streaming driver. :)

For correctness, you should only create one PurchasingProfile and one TransactionGenerator per customer. Maybe you should create a List as an instance member and populate it with one TransactionGenerator per customer?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment