-
-
Save yuj18/ac7bc3e9d83e816fd059bca806c61f1d to your computer and use it in GitHub Desktop.
Import data from GPDB to a GemFire region
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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