Skip to content

Instantly share code, notes, and snippets.

@yuj18
Last active August 9, 2016 12:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuj18/ac7bc3e9d83e816fd059bca806c61f1d to your computer and use it in GitHub Desktop.
Save yuj18/ac7bc3e9d83e816fd059bca806c61f1d to your computer and use it in GitHub Desktop.
Import data from GPDB to a GemFire region
public class LoadFromGPDB extends RegionFunctionAdapter implements Declarable {
private static final Logger log = LogManager.getLogger();
public static final String ID = "LoadFromGPDB";
@Override
public boolean isHA() {
return false;
}
@Override
public String getId() {
return ID;
}
@Override
public void init(Properties props) {
}
/**
* Load data from GPDB to a user-specified GemFire region.
*/
@Override
public void execute(RegionFunctionContext arg0) {
try {
Cache cache = CacheFactory.getAnyInstance();
GpdbService gpdb = GpdbService.getInstance(cache);
Region<Object, ?> region = arg0.getDataSet();
// Clear the region before loading the table from GPDB.
clearRegion(region);
// Load the table from GPDB.
long count = gpdb.importRegion(region);
log.info("Loaded " + count + " rows from GPDB on region " + region.getName());
arg0.getResultSender().lastResult(ID + ": Loaded " + count + " entities on region " + region.getName());
} catch (Exception e) {
e.printStackTrace();
log.error(e.getMessage());
}
}
/**
* Clear a region.
*
* @param region
* the region to be cleared.
*/
public void clearRegion(Region<Object, ?> region) {
Set<Object> keys;
if (PartitionRegionHelper.isPartitionedRegion(region)) {
keys = PartitionRegionHelper.getLocalPrimaryData(region).keySet();
} else {
keys = region.keySet();
}
for (Object key : keys) {
region.remove(key);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment