use of org.apache.pig.impl.util.UDFContext in project pygmalion by jeromatron.
the class ToCassandraBag method outputSchema.
public Schema outputSchema(Schema input) {
StringBuilder builder = new StringBuilder();
List<Schema.FieldSchema> fields = input.getFields();
for (int i = 0; i < fields.size(); i++) {
builder.append(fields.get(i).alias);
if (i != fields.size() - 1) {
builder.append(OUTPUT_DELIM);
}
}
UDFContext context = UDFContext.getUDFContext();
Properties property = context.getUDFProperties(ToCassandraBag.class);
property.setProperty(getSchemaKey(), builder.toString());
return super.outputSchema(input);
}
use of org.apache.pig.impl.util.UDFContext in project pygmalion by jeromatron.
the class ToCassandraBagTest method test.
@Test
public void test() throws Exception {
ToCassandraBag tcb = new ToCassandraBag();
UDFContext context = UDFContext.getUDFContext();
Properties properties = context.getUDFProperties(ToCassandraBag.class);
Tuple input = new DefaultTuple();
StringBuilder builder = new StringBuilder();
for (int i = 0; i < fields.length; i++) {
builder.append(fields[i]);
input.append("foo" + i);
if (i < fields.length - 1) {
builder.append(',');
}
}
properties.setProperty(ToCassandraBag.UDFCONTEXT_SCHEMA_KEY + ".default_context", builder.toString());
Tuple tuple = tcb.exec(input);
assertNotNull("Tuple is null", tuple);
assertEquals(2, tuple.size());
//first is the key, rest is a set of columns
Object one = tuple.get(0);
assertTrue(one instanceof String);
Object two = tuple.get(1);
assertTrue(two instanceof DataBag);
//Bad input
input = new DefaultTuple();
input.append(null);
input.append("foo");
try {
tcb.exec(input);
assertTrue(false);
} catch (IOException e) {
//expected
}
input = new DefaultTuple();
builder.setLength(0);
for (int i = 0; i < fields.length - 1; i++) {
builder.append(fields[i]);
input.append("foo" + i);
if (i < fields.length - 1) {
builder.append(',');
}
}
properties.setProperty(ToCassandraBag.UDFCONTEXT_SCHEMA_KEY + ".default_context", builder.toString());
input.append("foo extra");
try {
tcb.exec(input);
assertTrue(false);
} catch (IOException e) {
}
}
use of org.apache.pig.impl.util.UDFContext in project brisk by riptano.
the class CassandraStorage method initSchema.
/* Methods to get the column family schema from Cassandra */
private void initSchema() {
UDFContext context = UDFContext.getUDFContext();
Properties property = context.getUDFProperties(CassandraStorage.class);
String schemaContextKey = getSchemaContextKey();
// Only get the schema if we haven't already gotten it
if (!property.containsKey(schemaContextKey)) {
Cassandra.Client client = null;
try {
client = createConnection(ConfigHelper.getInitialAddress(conf), ConfigHelper.getRpcPort(conf), true);
CfDef cfDef = null;
client.set_keyspace(keyspace);
KsDef ksDef = client.describe_keyspace(keyspace);
List<CfDef> defs = ksDef.getCf_defs();
for (CfDef def : defs) {
if (column_family.equalsIgnoreCase(def.getName())) {
cfDef = def;
break;
}
}
property.setProperty(schemaContextKey, cfdefToString(cfDef));
} catch (TException e) {
throw new RuntimeException(e);
} catch (InvalidRequestException e) {
throw new RuntimeException(e);
} catch (NotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
use of org.apache.pig.impl.util.UDFContext in project mongo-hadoop by mongodb.
the class MongoUpdateStorage method checkSchema.
@Override
public void checkSchema(final ResourceSchema s) throws IOException {
schema = s;
UDFContext udfContext = UDFContext.getUDFContext();
Properties p = udfContext.getUDFProperties(getClass(), new String[] { signature });
p.setProperty(SCHEMA_SIGNATURE, schema.toString());
}
use of org.apache.pig.impl.util.UDFContext in project mongo-hadoop by mongodb.
the class MongoUpdateStorage method prepareToWrite.
@Override
public void prepareToWrite(final RecordWriter writer) throws IOException {
//noinspection unchecked
recordWriter = (MongoRecordWriter<?, MongoUpdateWritable>) writer;
LOG.info("Preparing to write to " + recordWriter);
if (recordWriter == null) {
throw new IOException("Invalid Record Writer");
}
UDFContext context = UDFContext.getUDFContext();
Properties p = context.getUDFProperties(getClass(), new String[] { signature });
/*
* In determining the schema to use, the user-defined schema should take
* precedence over the "inferred" schema
*/
if (schemaStr != null) {
try {
schema = new ResourceSchema(Utils.getSchemaFromString(schemaStr));
} catch (Exception e) {
LOG.error(e.getMessage(), e);
}
} else {
String s = p.getProperty(SCHEMA_SIGNATURE);
if (s == null) {
throw new IOException("Could not find schema in UDF context. You'd have to explicitly specify a Schema.");
}
schema = new ResourceSchema(Utils.getSchemaFromString(s));
}
}
Aggregations