use of com.baidu.hugegraph.computer.core.common.exception.ComputerException in project hugegraph-computer by hugegraph.
the class EtcdClient method get.
/**
* Returns the value to which the specified key is mapped. If no
* key exists, wait at most timeout milliseconds. Or throw
* ComputerException if timeout
* @param key the key whose associated value is to be returned.
* @param timeout the max time in milliseconds to wait.
* @return the specified value in byte array to which the specified key is
* mapped.
*/
public byte[] get(String key, long timeout, long logInterval) {
E.checkArgumentNotNull(key, "The key can't be null");
E.checkArgument(timeout > 0L, "The timeout must be > 0, but got: %s", timeout);
E.checkArgument(logInterval > 0L, "The logInterval must be > 0, but got: %s", logInterval);
ByteSequence keySeq = ByteSequence.from(key, ENCODING);
try {
GetResponse response = this.kv.get(keySeq).get();
if (response.getCount() > 0) {
List<KeyValue> kvs = response.getKvs();
return kvs.get(0).getValue().getBytes();
} else {
long revision = response.getHeader().getRevision();
return this.waitAndGetFromPutEvent(keySeq, revision, timeout, logInterval);
}
} catch (InterruptedException e) {
throw new ComputerException("Interrupted while getting with key='%s'", e, key);
} catch (ExecutionException e) {
throw new ComputerException("Error while getting with key='%s'", e, key);
}
}
use of com.baidu.hugegraph.computer.core.common.exception.ComputerException in project hugegraph-computer by hugegraph.
the class EtcdClient method getWithPrefix.
/**
* Get the expected count of values of keys with the specified prefix.
* Throws ComputerException if there are no enough object.
*/
public List<byte[]> getWithPrefix(String prefix, int count) {
E.checkArgumentNotNull(prefix, "The prefix can't be null");
E.checkArgument(count >= 0, "The count must be >= 0, but got: %s", count);
try {
ByteSequence prefixSeq = ByteSequence.from(prefix, ENCODING);
GetOption getOption = GetOption.newBuilder().withPrefix(prefixSeq).withLimit(count).withSortOrder(SortOrder.ASCEND).build();
GetResponse response = this.kv.get(prefixSeq, getOption).get();
if (response.getCount() == count) {
return getResponseValues(response);
} else {
throw new ComputerException("Expect %s elements, only find %s elements with " + "prefix='%s'", count, response.getCount(), prefix);
}
} catch (InterruptedException e) {
throw new ComputerException("Interrupted while getting with prefix='%s', count=%s", e, prefix, count);
} catch (ExecutionException e) {
throw new ComputerException("Error while getting with prefix='%s', count=%s", e, prefix, count);
}
}
use of com.baidu.hugegraph.computer.core.common.exception.ComputerException in project hugegraph-computer by hugegraph.
the class EtcdClient method deleteWithPrefix.
/**
* @return the number of keys deleted
*/
public long deleteWithPrefix(String prefix) {
E.checkArgumentNotNull(prefix, "The prefix can't be null");
ByteSequence prefixSeq = ByteSequence.from(prefix, ENCODING);
DeleteOption deleteOption = DeleteOption.newBuilder().withPrefix(prefixSeq).build();
try {
DeleteResponse response = this.client.getKVClient().delete(prefixSeq, deleteOption).get();
return response.getDeleted();
} catch (InterruptedException e) {
throw new ComputerException("Interrupted while deleting with prefix '%s'", e, prefix);
} catch (ExecutionException e) {
throw new ComputerException("ExecutionException is thrown while deleting with " + "prefix '%s'", e, prefix);
}
}
use of com.baidu.hugegraph.computer.core.common.exception.ComputerException in project hugegraph-computer by hugegraph.
the class EtcdClient method put.
/**
* Put the key value mapping to the map, if the map previously contained a
* mapping for the key, the old value is replaced by the specified value.
* @param key key with which the specified value is to be associated
* @param value value to be associated with the specified key
*/
public void put(String key, byte[] value) {
E.checkArgument(key != null, "The key can't be null.");
E.checkArgument(value != null, "The value can't be null.");
try {
this.kv.put(ByteSequence.from(key, ENCODING), ByteSequence.from(value)).get();
} catch (InterruptedException e) {
throw new ComputerException("Interrupted while putting with key='%s'", e, key);
} catch (ExecutionException e) {
throw new ComputerException("Error while putting with key='%s'", e, key);
}
}
use of com.baidu.hugegraph.computer.core.common.exception.ComputerException in project hugegraph-computer by hugegraph.
the class HdfsOutput method init.
@Override
public void init(Config config, int partition) {
super.init(config, partition);
try {
Configuration configuration = new Configuration();
Short replication = config.get(ComputerOptions.OUTPUT_HDFS_REPLICATION);
configuration.set(REPLICATION_KEY, String.valueOf(replication));
this.fs = HdfsOutput.openHDFS(config, configuration);
this.delimiter = config.get(ComputerOptions.OUTPUT_HDFS_DELIMITER);
String dir = config.get(ComputerOptions.OUTPUT_HDFS_DIR);
String jobId = config.get(ComputerOptions.JOB_ID);
Path hdfsPath = buildPath(dir, jobId, partition);
this.fileOutputStream = this.fs.create(hdfsPath, true);
} catch (IOException | InterruptedException e) {
throw new ComputerException("Failed to init hdfs output on " + "partition [%s]", e, partition);
}
}
Aggregations