Search in sources :

Example 21 with UDFContext

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);
}
Also used : UDFContext(org.apache.pig.impl.util.UDFContext) Properties(java.util.Properties)

Example 22 with UDFContext

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) {
    }
}
Also used : DataBag(org.apache.pig.data.DataBag) ToCassandraBag(org.pygmalion.udf.ToCassandraBag) DefaultTuple(org.apache.pig.data.DefaultTuple) UDFContext(org.apache.pig.impl.util.UDFContext) IOException(java.io.IOException) Properties(java.util.Properties) DefaultTuple(org.apache.pig.data.DefaultTuple) Tuple(org.apache.pig.data.Tuple) Test(org.junit.Test)

Example 23 with UDFContext

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);
        }
    }
}
Also used : TException(org.apache.thrift.TException) UDFContext(org.apache.pig.impl.util.UDFContext) IOException(java.io.IOException)

Example 24 with UDFContext

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());
}
Also used : UDFContext(org.apache.pig.impl.util.UDFContext) Properties(java.util.Properties)

Example 25 with UDFContext

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));
    }
}
Also used : ResourceSchema(org.apache.pig.ResourceSchema) MongoUpdateWritable(com.mongodb.hadoop.io.MongoUpdateWritable) UDFContext(org.apache.pig.impl.util.UDFContext) IOException(java.io.IOException) Properties(java.util.Properties) IOException(java.io.IOException)

Aggregations

UDFContext (org.apache.pig.impl.util.UDFContext)28 Properties (java.util.Properties)23 IOException (java.io.IOException)14 ResourceSchema (org.apache.pig.ResourceSchema)4 Configuration (org.apache.hadoop.conf.Configuration)3 DataBag (org.apache.pig.data.DataBag)3 Tuple (org.apache.pig.data.Tuple)3 InterruptedException (java.lang.InterruptedException)2 Path (org.apache.hadoop.fs.Path)2 HCatException (org.apache.hive.hcatalog.common.HCatException)2 HCatSchema (org.apache.hive.hcatalog.data.schema.HCatSchema)2 TException (org.apache.thrift.TException)2 JsonParseException (org.codehaus.jackson.JsonParseException)2 JsonMappingException (org.codehaus.jackson.map.JsonMappingException)2 MongoUpdateWritable (com.mongodb.hadoop.io.MongoUpdateWritable)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 SQLException (java.sql.SQLException)1 ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1