Search in sources :

Example 1 with MissingFormatArgumentException

use of java.util.MissingFormatArgumentException in project hive by apache.

the class RegexSerDe method serialize.

@Override
public Writable serialize(Object obj, ObjectInspector objInspector) throws SerDeException {
    if (outputFormatString == null) {
        throw new SerDeException("Cannot write data into table because \"output.format.string\"" + " is not specified in serde properties of the table.");
    }
    // Get all the fields out.
    // NOTE: The correct way to get fields out of the row is to use
    // objInspector.
    // The obj can be a Java ArrayList, or a Java class, or a byte[] or
    // whatever.
    // The only way to access the data inside the obj is through
    // ObjectInspector.
    StructObjectInspector outputRowOI = (StructObjectInspector) objInspector;
    List<? extends StructField> outputFieldRefs = outputRowOI.getAllStructFieldRefs();
    if (outputFieldRefs.size() != numColumns) {
        throw new SerDeException("Cannot serialize the object because there are " + outputFieldRefs.size() + " fields but the table has " + numColumns + " columns.");
    }
    // Get all data out.
    for (int c = 0; c < numColumns; c++) {
        Object field = outputRowOI.getStructFieldData(obj, outputFieldRefs.get(c));
        ObjectInspector fieldOI = outputFieldRefs.get(c).getFieldObjectInspector();
        // The data must be of type String
        StringObjectInspector fieldStringOI = (StringObjectInspector) fieldOI;
        // Convert the field to Java class String, because objects of String type
        // can be
        // stored in String, Text, or some other classes.
        outputFields[c] = fieldStringOI.getPrimitiveJavaObject(field);
    }
    // Format the String
    String outputRowString = null;
    try {
        outputRowString = String.format(outputFormatString, outputFields);
    } catch (MissingFormatArgumentException e) {
        throw new SerDeException("The table contains " + numColumns + " columns, but the outputFormatString is asking for more.", e);
    }
    outputRowText.set(outputRowString);
    return outputRowText;
}
Also used : ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector) StringObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.StringObjectInspector) MissingFormatArgumentException(java.util.MissingFormatArgumentException) StringObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.StringObjectInspector) SerDeException(org.apache.hadoop.hive.serde2.SerDeException) StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector)

Example 2 with MissingFormatArgumentException

use of java.util.MissingFormatArgumentException in project j2objc by google.

the class FormatterTest method test_formatLjava_lang_String$Ljava_lang_Object_ArgIndex.

/**
     * java.util.Formatter#format(String, Object...) for argument index
     */
public void test_formatLjava_lang_String$Ljava_lang_Object_ArgIndex() {
    Formatter formatter = new Formatter(Locale.US);
    formatter.format("%1$s%2$s%3$s%4$s%5$s%6$s%7$s%8$s%9$s%11$s%10$s", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11");
    assertEquals("1234567891110", formatter.toString());
    formatter = new Formatter(Locale.JAPAN);
    formatter.format("%0$s", "hello");
    assertEquals("hello", formatter.toString());
    try {
        formatter = new Formatter(Locale.US);
        formatter.format("%-1$s", "1", "2");
        fail("should throw UnknownFormatConversionException");
    } catch (UnknownFormatConversionException e) {
    // expected
    }
    try {
        formatter = new Formatter(Locale.US);
        formatter.format("%$s", "hello", "2");
        fail("should throw UnknownFormatConversionException");
    } catch (UnknownFormatConversionException e) {
    // expected
    }
    try {
        Formatter f = new Formatter(Locale.US);
        f.format("%", "string");
        fail("should throw UnknownFormatConversionException");
    } catch (UnknownFormatConversionException e) {
    // expected
    }
    formatter = new Formatter(Locale.FRANCE);
    formatter.format("%1$s%2$s%3$s%4$s%5$s%6$s%7$s%8$s%<s%s%s%<s", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11");
    assertEquals("123456788122", formatter.toString());
    formatter = new Formatter(Locale.FRANCE);
    formatter.format("xx%1$s22%2$s%s%<s%5$s%<s&%7$h%2$s%8$s%<s%s%s%<ssuffix", "1", "2", "3", "4", "5", "6", 7, "8", "9", "10", "11");
    assertEquals("xx12221155&7288233suffix", formatter.toString());
    try {
        formatter.format("%<s", "hello");
        fail("should throw MissingFormatArgumentException");
    } catch (MissingFormatArgumentException e) {
    // expected
    }
    formatter = new Formatter(Locale.US);
    try {
        formatter.format("%123$s", "hello");
        fail("should throw MissingFormatArgumentException");
    } catch (MissingFormatArgumentException e) {
    // expected
    }
    formatter = new Formatter(Locale.US);
    try {
        // 2147483648 is the value of Integer.MAX_VALUE + 1
        formatter.format("%2147483648$s", "hello");
        fail("should throw MissingFormatArgumentException");
    } catch (MissingFormatArgumentException e) {
    // expected
    }
    try {
        // 2147483647 is the value of Integer.MAX_VALUE
        formatter.format("%2147483647$s", "hello");
        fail("should throw MissingFormatArgumentException");
    } catch (MissingFormatArgumentException e) {
    // expected
    }
    formatter = new Formatter(Locale.US);
    try {
        formatter.format("%s%s", "hello");
        fail("should throw MissingFormatArgumentException");
    } catch (MissingFormatArgumentException e) {
    // expected
    }
    formatter = new Formatter(Locale.US);
    formatter.format("$100", 100);
    assertEquals("$100", formatter.toString());
    formatter = new Formatter(Locale.UK);
    formatter.format("%01$s", "string");
    assertEquals("string", formatter.toString());
}
Also used : UnknownFormatConversionException(java.util.UnknownFormatConversionException) Formatter(java.util.Formatter) MissingFormatArgumentException(java.util.MissingFormatArgumentException)

Example 3 with MissingFormatArgumentException

use of java.util.MissingFormatArgumentException in project opennms by OpenNMS.

the class SnmpAssetProvisioningAdapter method fetchSnmpAssetString.

private static String fetchSnmpAssetString(final LocationAwareSnmpClient locationAwareSnmpClient, final SnmpAgentConfig agentConfig, final String location, final List<MibObj> mibObjs, final String formatString) {
    final List<String> aliases = new ArrayList<String>();
    final List<SnmpObjId> objs = new ArrayList<SnmpObjId>();
    for (final MibObj mibobj : mibObjs) {
        aliases.add(mibobj.getAlias());
        objs.add(SnmpObjId.get(mibobj.getOid()));
    }
    // Fetch the values from the SNMP agent
    final CompletableFuture<List<SnmpValue>> future = locationAwareSnmpClient.get(agentConfig, objs).withLocation(location).execute();
    List<SnmpValue> values;
    try {
        values = future.get();
    } catch (InterruptedException | ExecutionException e) {
        // Propagate
        throw new RuntimeException(e);
    }
    if (values.size() == aliases.size()) {
        final Properties substitutions = new Properties();
        boolean foundAValue = false;
        for (int i = 0; i < values.size(); i++) {
            // If the value is a NO_SUCH_OBJECT or NO_SUCH_INSTANCE error, then skip it
            if (values.get(i) == null || values.get(i).isError()) {
                // No value for this OID
                continue;
            }
            foundAValue = true;
            // Use trapd's SyntaxToEvent parser so that we format base64
            // and MAC address values appropriately
            Parm parm = SyntaxToEvent.processSyntax(aliases.get(i), values.get(i));
            substitutions.setProperty(aliases.get(i), parm.getValue().getContent());
        }
        if (!foundAValue) {
            LOG.debug("fetchSnmpAssetString: Failed to fetch any SNMP values for system {}", agentConfig);
            throw new MissingFormatArgumentException("fetchSnmpAssetString: Failed to fetch any SNMP values for system " + agentConfig.toString());
        } else {
            LOG.debug("fetchSnmpAssetString: Fetched asset properties from SNMP agent:\n {}", formatPropertiesAsString(substitutions));
        }
        if (objs.size() != substitutions.size()) {
            LOG.warn("fetchSnmpAssetString: Unexpected number of properties returned from SNMP GET:\n {}", formatPropertiesAsString(substitutions));
        }
        return PropertiesUtils.substitute(formatString, substitutions);
    } else {
        LOG.warn("fetchSnmpAssetString: Invalid number of SNMP parameters returned: {} != {}", aliases.size(), values.size());
        throw new MissingFormatArgumentException("fetchSnmpAssetString: Invalid number of SNMP parameters returned: " + values.size() + " != " + aliases.size());
    }
}
Also used : ArrayList(java.util.ArrayList) Parm(org.opennms.netmgt.xml.event.Parm) SnmpObjId(org.opennms.netmgt.snmp.SnmpObjId) MibObj(org.opennms.netmgt.config.snmpAsset.adapter.MibObj) Properties(java.util.Properties) SnmpValue(org.opennms.netmgt.snmp.SnmpValue) MissingFormatArgumentException(java.util.MissingFormatArgumentException) ArrayList(java.util.ArrayList) List(java.util.List) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

MissingFormatArgumentException (java.util.MissingFormatArgumentException)3 ArrayList (java.util.ArrayList)1 Formatter (java.util.Formatter)1 List (java.util.List)1 Properties (java.util.Properties)1 UnknownFormatConversionException (java.util.UnknownFormatConversionException)1 ExecutionException (java.util.concurrent.ExecutionException)1 SerDeException (org.apache.hadoop.hive.serde2.SerDeException)1 ObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)1 StructObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector)1 StringObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.StringObjectInspector)1 MibObj (org.opennms.netmgt.config.snmpAsset.adapter.MibObj)1 SnmpObjId (org.opennms.netmgt.snmp.SnmpObjId)1 SnmpValue (org.opennms.netmgt.snmp.SnmpValue)1 Parm (org.opennms.netmgt.xml.event.Parm)1