use of org.apache.pig.ResourceSchema in project mongo-hadoop by mongodb.
the class JSONPigReplace method substitute.
/*
* Returns result of substituting pig objects in Tuple t into
* initStr
*
* @param Tuple t : Pig tuple containing pig objects
* @param Object s : Schema representing Tuple t
* @param String un : String to represent un-named Schema Fields
*
* @return Array of BasicBSONObjects that contain all replacements for "marked" strings
*/
public BasicBSONObject[] substitute(final Tuple t, final Object s, final String un) throws Exception {
unnamedStr = un;
final ResourceFieldSchema[] fields;
try {
final ResourceSchema schema;
if (s instanceof String) {
schema = new ResourceSchema(Utils.getSchemaFromString((String) s));
} else if (s instanceof Schema) {
schema = new ResourceSchema((Schema) s);
} else if (s instanceof ResourceSchema) {
schema = (ResourceSchema) s;
} else {
throw new IllegalArgumentException("Schema must be represented either by a string or a Schema " + "object, not " + s);
}
fields = schema.getFields();
} catch (Exception e) {
throw new IllegalArgumentException("Invalid Schema Format", e);
}
// Make Tuple t into BSONObject using schema provided and store result in pObj
final BasicDBObjectBuilder builder = BasicDBObjectBuilder.start();
for (int i = 0; i < fields.length; i++) {
writeField(builder, fields[i], t.get(i));
}
// BSONObject that represents Pig Tuple input using Pig Schema
BasicBSONObject pObj = (BasicBSONObject) builder.get();
// fill map of replacement strings to corresponding objects to replace these strings with
fillReplacementMap(pObj);
// Now, replace replacement strings (of form $elem) with corresponding objects in pObj
return replaceAll(initBSONs, reps);
}
use of org.apache.pig.ResourceSchema in project mongo-hadoop by mongodb.
the class MongoInsertStorage method prepareToWrite.
public void prepareToWrite(final RecordWriter writer) throws IOException {
out = writer;
if (out == null) {
throw new IOException("Invalid Record Writer");
}
UDFContext udfc = UDFContext.getUDFContext();
Properties p = udfc.getUDFProperties(getClass(), new String[] { udfcSignature });
String strSchema = p.getProperty(SCHEMA_SIGNATURE);
if (strSchema == null) {
LOG.warn("Could not find schema in UDF context. Interpreting each tuple as containing a single map.");
} else {
try {
// Parse the schema from the string stored in the properties object.
schema = new ResourceSchema(Utils.getSchemaFromString(strSchema));
} catch (Exception e) {
schema = null;
LOG.warn(e.getMessage());
}
if (LOG.isDebugEnabled()) {
LOG.debug("GOT A SCHEMA " + schema + " " + strSchema);
}
}
}
use of org.apache.pig.ResourceSchema in project phoenix by apache.
the class PhoenixPigSchemaUtil method getResourceSchema.
public static ResourceSchema getResourceSchema(final Configuration configuration, Dependencies dependencies) throws IOException {
final ResourceSchema schema = new ResourceSchema();
try {
List<ColumnInfo> columns = null;
final SchemaType schemaType = PhoenixConfigurationUtil.getSchemaType(configuration);
if (SchemaType.QUERY.equals(schemaType)) {
final String sqlQuery = PhoenixConfigurationUtil.getSelectStatement(configuration);
Preconditions.checkNotNull(sqlQuery, "No Sql Query exists within the configuration");
final SqlQueryToColumnInfoFunction function = new SqlQueryToColumnInfoFunction(configuration);
columns = function.apply(sqlQuery);
} else {
columns = dependencies.getSelectColumnMetadataList(configuration);
}
ResourceFieldSchema[] fields = new ResourceFieldSchema[columns.size()];
int i = 0;
for (ColumnInfo cinfo : columns) {
int sqlType = cinfo.getSqlType();
PDataType phoenixDataType = PDataType.fromTypeId(sqlType);
byte pigType = TypeUtil.getPigDataTypeForPhoenixType(phoenixDataType);
ResourceFieldSchema field = new ResourceFieldSchema();
field.setType(pigType).setName(cinfo.getDisplayName());
fields[i++] = field;
}
schema.setFields(fields);
} catch (SQLException sqle) {
LOG.error(String.format("Error: SQLException [%s] ", sqle.getMessage()));
throw new IOException(sqle);
}
return schema;
}
Aggregations