use of org.apache.druid.java.util.common.RE in project druid by druid-io.
the class LookupReferencesManager method startLookup.
@Nullable
private Map.Entry<String, LookupExtractorFactoryContainer> startLookup(LookupBean lookupBean) {
LookupExtractorFactoryContainer container = lookupBean.getContainer();
LOG.info("Starting lookup [%s]:[%s]", lookupBean.getName(), container);
try {
if (container.getLookupExtractorFactory().start()) {
LOG.info("Started lookup [%s]:[%s]", lookupBean.getName(), container);
return new AbstractMap.SimpleImmutableEntry<>(lookupBean.getName(), container);
} else {
LOG.error("Failed to start lookup [%s]:[%s]", lookupBean.getName(), container);
return null;
}
} catch (RuntimeException e) {
throw new RE(e, "Failed to start lookup [%s]:[%s]", lookupBean.getName(), container);
}
}
use of org.apache.druid.java.util.common.RE in project druid by druid-io.
the class BasicDataSourceExt method createConnectionFactory.
@Override
protected ConnectionFactory createConnectionFactory() throws SQLException {
Driver driverToUse = getDriver();
if (driverToUse == null) {
Class<?> driverFromCCL = null;
if (getDriverClassName() != null) {
try {
try {
if (getDriverClassLoader() == null) {
driverFromCCL = Class.forName(getDriverClassName());
} else {
driverFromCCL = Class.forName(getDriverClassName(), true, getDriverClassLoader());
}
} catch (ClassNotFoundException cnfe) {
driverFromCCL = Thread.currentThread().getContextClassLoader().loadClass(getDriverClassName());
}
} catch (Exception t) {
String message = "Cannot load JDBC driver class '" + getDriverClassName() + "'";
LOGGER.error(t, message);
throw new SQLException(message, t);
}
}
try {
if (driverFromCCL == null) {
driverToUse = DriverManager.getDriver(getUrl());
} else {
// Usage of DriverManager is not possible, as it does not
// respect the ContextClassLoader
// N.B. This cast may cause ClassCastException which is handled below
driverToUse = (Driver) driverFromCCL.newInstance();
if (!driverToUse.acceptsURL(getUrl())) {
throw new SQLException("No suitable driver", "08001");
}
}
} catch (Exception t) {
String message = "Cannot create JDBC driver of class '" + (getDriverClassName() != null ? getDriverClassName() : "") + "' for connect URL '" + getUrl() + "'";
LOGGER.error(t, message);
throw new SQLException(message, t);
}
}
if (driverToUse == null) {
throw new RE("Failed to find the DB Driver");
}
final Driver finalDriverToUse = driverToUse;
return () -> {
String user = connectorConfig.getUser();
if (user != null) {
connectionProperties.put("user", user);
} else {
log("DBCP DataSource configured without a 'username'");
}
// Note: This is the main point of this class where we are getting fresh password before setting up
// every new connection.
String password = connectorConfig.getPassword();
if (password != null) {
connectionProperties.put("password", password);
} else {
log("DBCP DataSource configured without a 'password'");
}
return finalDriverToUse.connect(connectorConfig.getConnectURI(), connectionProperties);
};
}
use of org.apache.druid.java.util.common.RE in project druid by druid-io.
the class ResultLevelCachingQueryRunner method deserializeResults.
private Sequence<T> deserializeResults(final byte[] cachedResult, CacheStrategy strategy, String resultSetId) {
if (cachedResult == null) {
log.error("Cached result set is null");
}
final Function<Object, T> pullFromCacheFunction = strategy.pullFromCache(true);
final TypeReference<T> cacheObjectClazz = strategy.getCacheObjectClazz();
// Skip the resultsetID and its length bytes
Sequence<T> cachedSequence = Sequences.simple(() -> {
try {
int resultOffset = Integer.BYTES + resultSetId.length();
return objectMapper.readValues(objectMapper.getFactory().createParser(cachedResult, resultOffset, cachedResult.length - resultOffset), cacheObjectClazz);
} catch (IOException e) {
throw new RE(e, "Failed to retrieve results from cache for query ID [%s]", query.getId());
}
});
return Sequences.map(cachedSequence, pullFromCacheFunction);
}
use of org.apache.druid.java.util.common.RE in project druid by druid-io.
the class GoogleTaskLogs method pushTaskFile.
private void pushTaskFile(final File logFile, final String taskKey) throws IOException {
try (final InputStream fileStream = Files.newInputStream(logFile.toPath())) {
InputStreamContent mediaContent = new InputStreamContent("text/plain", fileStream);
mediaContent.setLength(logFile.length());
try {
RetryUtils.retry((RetryUtils.Task<Void>) () -> {
storage.insert(config.getBucket(), taskKey, mediaContent);
return null;
}, GoogleUtils::isRetryable, 1, 5);
} catch (IOException e) {
throw e;
} catch (Exception e) {
throw new RE(e, "Failed to upload [%s] to [%s]", logFile, taskKey);
}
}
}
use of org.apache.druid.java.util.common.RE in project druid by druid-io.
the class ParquetGroupConverter method convertPrimitiveField.
/**
* Convert a primitive group field to a "ingestion friendly" java object
*
* @return "ingestion ready" java object, or null
*/
@Nullable
private static Object convertPrimitiveField(Group g, int fieldIndex, int index, boolean binaryAsString) {
PrimitiveType pt = (PrimitiveType) g.getType().getFields().get(fieldIndex);
OriginalType ot = pt.getOriginalType();
try {
if (ot != null) {
// convert logical types
switch(ot) {
case DATE:
long ts = g.getInteger(fieldIndex, index) * MILLIS_IN_DAY;
return ts;
case TIME_MICROS:
return g.getLong(fieldIndex, index);
case TIME_MILLIS:
return g.getInteger(fieldIndex, index);
case TIMESTAMP_MICROS:
return TimeUnit.MILLISECONDS.convert(g.getLong(fieldIndex, index), TimeUnit.MICROSECONDS);
case TIMESTAMP_MILLIS:
return g.getLong(fieldIndex, index);
case INTERVAL:
/*
INTERVAL is used for an interval of time. It must annotate a fixed_len_byte_array of length 12.
This array stores three little-endian unsigned integers that represent durations at different
granularities of time. The first stores a number in months, the second stores a number in days,
and the third stores a number in milliseconds. This representation is independent of any particular
timezone or date.
Each component in this representation is independent of the others. For example, there is no
requirement that a large number of days should be expressed as a mix of months and days because there is
not a constant conversion from days to months.
The sort order used for INTERVAL is undefined. When writing data, no min/max statistics should be
saved for this type and if such non-compliant statistics are found during reading, they must be ignored.
*/
Binary intervalVal = g.getBinary(fieldIndex, index);
IntBuffer intBuf = intervalVal.toByteBuffer().order(ByteOrder.LITTLE_ENDIAN).asIntBuffer();
int months = intBuf.get(0);
int days = intBuf.get(1);
int millis = intBuf.get(2);
StringBuilder periodBuilder = new StringBuilder("P");
if (months > 0) {
periodBuilder.append(months).append("M");
}
if (days > 0) {
periodBuilder.append(days).append("D");
}
if (periodBuilder.length() > 1) {
Period p = Period.parse(periodBuilder.toString());
Duration d = p.toStandardDuration().plus(millis);
return d;
} else {
return new Duration(millis);
}
case INT_8:
case INT_16:
case INT_32:
return g.getInteger(fieldIndex, index);
case INT_64:
return g.getLong(fieldIndex, index);
// todo: idk wtd about unsigned
case UINT_8:
case UINT_16:
case UINT_32:
return g.getInteger(fieldIndex, index);
case UINT_64:
return g.getLong(fieldIndex, index);
case DECIMAL:
/*
DECIMAL can be used to annotate the following types:
int32: for 1 <= precision <= 9
int64: for 1 <= precision <= 18; precision < 10 will produce a warning
fixed_len_byte_array: precision is limited by the array size. Length n can
store <= floor(log_10(2^(8*n - 1) - 1)) base-10 digits
binary: precision is not limited, but is required. The minimum number of bytes to store
the unscaled value should be used.
*/
int precision = pt.asPrimitiveType().getDecimalMetadata().getPrecision();
int scale = pt.asPrimitiveType().getDecimalMetadata().getScale();
switch(pt.getPrimitiveTypeName()) {
case INT32:
return new BigDecimal(g.getInteger(fieldIndex, index));
case INT64:
return new BigDecimal(g.getLong(fieldIndex, index));
case FIXED_LEN_BYTE_ARRAY:
case BINARY:
Binary value = g.getBinary(fieldIndex, index);
return convertBinaryToDecimal(value, precision, scale);
default:
throw new RE("Unknown 'DECIMAL' type supplied to primitive conversion: %s (this should never happen)", pt.getPrimitiveTypeName());
}
case UTF8:
case ENUM:
case JSON:
return g.getString(fieldIndex, index);
case LIST:
case MAP:
case MAP_KEY_VALUE:
case BSON:
default:
throw new RE("Non-primitive supplied to primitive conversion: %s (this should never happen)", ot.name());
}
} else {
// fallback to handling the raw primitive type if no logical type mapping
switch(pt.getPrimitiveTypeName()) {
case BOOLEAN:
return g.getBoolean(fieldIndex, index);
case INT32:
return g.getInteger(fieldIndex, index);
case INT64:
return g.getLong(fieldIndex, index);
case FLOAT:
return g.getFloat(fieldIndex, index);
case DOUBLE:
return g.getDouble(fieldIndex, index);
case INT96:
Binary tsBin = g.getInt96(fieldIndex, index);
return convertInt96BinaryToTimestamp(tsBin);
case FIXED_LEN_BYTE_ARRAY:
case BINARY:
Binary bin = g.getBinary(fieldIndex, index);
byte[] bytes = bin.getBytes();
if (binaryAsString) {
return StringUtils.fromUtf8(bytes);
} else {
return bytes;
}
default:
throw new RE("Unknown primitive conversion: %s", pt.getPrimitiveTypeName());
}
}
} catch (Exception ex) {
return null;
}
}
Aggregations