Search in sources :

Example 1 with JString

use of org.apache.asterix.external.library.java.JObjects.JString in project asterixdb by apache.

the class AddHashTagsFunction method evaluate.

@Override
public void evaluate(IFunctionHelper functionHelper) throws Exception {
    list.clear();
    JRecord inputRecord = (JRecord) functionHelper.getArgument(0);
    JString text = (JString) inputRecord.getValueByName(Datatypes.Tweet.MESSAGE);
    JDouble latitude = (JDouble) inputRecord.getValueByName(Datatypes.Tweet.LATITUDE);
    JDouble longitude = (JDouble) inputRecord.getValueByName(Datatypes.Tweet.LONGITUDE);
    if (latitude != null && longitude != null) {
        location.setValue(latitude.getValue(), longitude.getValue());
    } else {
        location.setValue(0, 0);
    }
    String[] tokens = text.getValue().split(" ");
    for (String tk : tokens) {
        if (tk.startsWith("#")) {
            JString newField = (JString) functionHelper.getObject(JTypeTag.STRING);
            newField.setValue(tk);
            list.add(newField);
        }
    }
    JRecord outputRecord = (JRecord) functionHelper.getResultObject();
    outputRecord.setField(Datatypes.Tweet.ID, inputRecord.getValueByName(Datatypes.Tweet.ID));
    JRecord userRecord = (JRecord) inputRecord.getValueByName(Datatypes.Tweet.USER);
    outputRecord.setField(Datatypes.ProcessedTweet.USER_NAME, userRecord.getValueByName(Datatypes.Tweet.SCREEN_NAME));
    outputRecord.setField(Datatypes.ProcessedTweet.LOCATION, location);
    outputRecord.setField(Datatypes.Tweet.CREATED_AT, inputRecord.getValueByName(Datatypes.Tweet.CREATED_AT));
    outputRecord.setField(Datatypes.Tweet.MESSAGE, text);
    outputRecord.setField(Datatypes.ProcessedTweet.TOPICS, list);
    inputRecord.addField(Datatypes.ProcessedTweet.TOPICS, list);
    functionHelper.setResult(outputRecord);
}
Also used : JDouble(org.apache.asterix.external.library.java.JObjects.JDouble) JRecord(org.apache.asterix.external.library.java.JObjects.JRecord) JString(org.apache.asterix.external.library.java.JObjects.JString) JString(org.apache.asterix.external.library.java.JObjects.JString)

Example 2 with JString

use of org.apache.asterix.external.library.java.JObjects.JString in project asterixdb by apache.

the class AddHashTagsInPlaceFunction method evaluate.

@Override
public void evaluate(IFunctionHelper functionHelper) throws Exception {
    list.clear();
    JRecord inputRecord = (JRecord) functionHelper.getArgument(0);
    JString text = (JString) inputRecord.getValueByName(Datatypes.Tweet.MESSAGE);
    String[] tokens = text.getValue().split(" ");
    for (String tk : tokens) {
        if (tk.startsWith("#")) {
            JString newField = (JString) functionHelper.getObject(JTypeTag.STRING);
            newField.setValue(tk);
            list.add(newField);
        }
    }
    inputRecord.addField(Datatypes.ProcessedTweet.TOPICS, list);
    functionHelper.setResult(inputRecord);
}
Also used : JRecord(org.apache.asterix.external.library.java.JObjects.JRecord) JString(org.apache.asterix.external.library.java.JObjects.JString) JString(org.apache.asterix.external.library.java.JObjects.JString)

Example 3 with JString

use of org.apache.asterix.external.library.java.JObjects.JString in project asterixdb by apache.

the class AllTypesFunction method evaluate.

@Override
public void evaluate(IFunctionHelper functionHelper) throws Exception {
    newFieldList.clear();
    JRecord inputRecord = (JRecord) functionHelper.getArgument(0);
    JInt id = (JInt) inputRecord.getValueByName("id");
    JString name = (JString) inputRecord.getValueByName("name");
    JFloat age = (JFloat) inputRecord.getValueByName("age");
    JDouble salary = (JDouble) inputRecord.getValueByName("salary");
    JBoolean married = (JBoolean) inputRecord.getValueByName("married");
    JUnorderedList interest = (JUnorderedList) inputRecord.getValueByName("interests");
    JOrderedList children = (JOrderedList) inputRecord.getValueByName("children");
    JRecord address = (JRecord) inputRecord.getValueByName("address");
    JDate dob = (JDate) inputRecord.getValueByName("dob");
    JTime time = (JTime) inputRecord.getValueByName("time");
    JDateTime dateTime = (JDateTime) inputRecord.getValueByName("datetime");
    JDuration duration = (JDuration) inputRecord.getValueByName("duration");
    JPoint location2d = (JPoint) inputRecord.getValueByName("location2d");
    JPoint3D location3d = (JPoint3D) inputRecord.getValueByName("location3d");
    JLine line = (JLine) inputRecord.getValueByName("line");
    JPolygon polygon = (JPolygon) inputRecord.getValueByName("polygon");
    JCircle circle = (JCircle) inputRecord.getValueByName("circle");
    JRecord result = (JRecord) functionHelper.getResultObject();
    result.setField("id", id);
    result.setField("name", name);
    result.setField("age", age);
    result.setField("salary", salary);
    result.setField("married", married);
    result.setField("interests", interest);
    result.setField("children", children);
    JInt zipCode = (JInt) functionHelper.getObject(JTypeTag.INT);
    zipCode.setValue(92841);
    address.addField("Zipcode", zipCode);
    result.setField("address", address);
    result.setField("dob", dob);
    result.setField("time", time);
    result.setField("datetime", dateTime);
    result.setField("duration", duration);
    result.setField("location2d", location2d);
    result.setField("location3d", location3d);
    result.setField("line", line);
    result.setField("polygon", polygon);
    result.setField("circle", circle);
    JString newFieldString = (JString) functionHelper.getObject(JTypeTag.STRING);
    newFieldString.setValue("processed");
    result.addField("status", newFieldString);
    /*
         * JString element = (JString)
         * functionHelper.getObject(JTypeTag.STRING); element.setValue("raman");
         * newFieldList.add(element); result.addField("mylist", newFieldList);
         */
    JString newFieldString2 = (JString) functionHelper.getObject(JTypeTag.STRING);
    newFieldString2.setValue("this is working");
    result.addField("working", newFieldString);
    functionHelper.setResult(result);
}
Also used : JFloat(org.apache.asterix.external.library.java.JObjects.JFloat) JRecord(org.apache.asterix.external.library.java.JObjects.JRecord) JPoint(org.apache.asterix.external.library.java.JObjects.JPoint) JCircle(org.apache.asterix.external.library.java.JObjects.JCircle) JInt(org.apache.asterix.external.library.java.JObjects.JInt) JLine(org.apache.asterix.external.library.java.JObjects.JLine) JDate(org.apache.asterix.external.library.java.JObjects.JDate) JTime(org.apache.asterix.external.library.java.JObjects.JTime) JPolygon(org.apache.asterix.external.library.java.JObjects.JPolygon) JUnorderedList(org.apache.asterix.external.library.java.JObjects.JUnorderedList) JDouble(org.apache.asterix.external.library.java.JObjects.JDouble) JBoolean(org.apache.asterix.external.library.java.JObjects.JBoolean) JDateTime(org.apache.asterix.external.library.java.JObjects.JDateTime) JDuration(org.apache.asterix.external.library.java.JObjects.JDuration) JOrderedList(org.apache.asterix.external.library.java.JObjects.JOrderedList) JPoint3D(org.apache.asterix.external.library.java.JObjects.JPoint3D) JString(org.apache.asterix.external.library.java.JObjects.JString)

Example 4 with JString

use of org.apache.asterix.external.library.java.JObjects.JString in project asterixdb by apache.

the class CapitalFinderFunction method evaluate.

@Override
public void evaluate(IFunctionHelper functionHelper) throws Exception {
    JString country = ((JString) functionHelper.getArgument(0));
    JRecord record = (JRecord) functionHelper.getResultObject();
    String capitalCity = capitalList.getProperty(country.getValue(), NOT_FOUND);
    capital.setValue(capitalCity);
    record.setField("country", country);
    record.setField("capital", capital);
    functionHelper.setResult(record);
}
Also used : JRecord(org.apache.asterix.external.library.java.JObjects.JRecord) JString(org.apache.asterix.external.library.java.JObjects.JString) JString(org.apache.asterix.external.library.java.JObjects.JString)

Example 5 with JString

use of org.apache.asterix.external.library.java.JObjects.JString in project asterixdb by apache.

the class JObjectUtil method getJType.

public static IJObject getJType(ATypeTag typeTag, IAType type, ByteArrayAccessibleDataInputStream dis, IObjectPool<IJObject, IAType> objectPool) throws IOException {
    IJObject jObject;
    switch(typeTag) {
        case INTEGER:
            {
                int v = dis.readInt();
                jObject = objectPool.allocate(BuiltinType.AINT32);
                ((JInt) jObject).setValue(v);
                break;
            }
        case FLOAT:
            {
                float v = dis.readFloat();
                jObject = objectPool.allocate(BuiltinType.AFLOAT);
                ((JFloat) jObject).setValue(v);
                break;
            }
        case DOUBLE:
            {
                double value = dis.readDouble();
                jObject = objectPool.allocate(BuiltinType.ADOUBLE);
                ((JDouble) jObject).setValue(value);
                break;
            }
        case STRING:
            {
                String v = dis.readUTF();
                jObject = objectPool.allocate(BuiltinType.ASTRING);
                ((JString) jObject).setValue(v);
                break;
            }
        case BOOLEAN:
            jObject = objectPool.allocate(BuiltinType.ABOOLEAN);
            ((JBoolean) jObject).setValue(dis.readBoolean());
            break;
        case DATE:
            {
                int d = dis.readInt();
                jObject = objectPool.allocate(BuiltinType.ADATE);
                ((JDate) jObject).setValue(d);
                break;
            }
        case DATETIME:
            {
                jObject = objectPool.allocate(BuiltinType.ADATETIME);
                long value = dis.readLong();
                ((JDateTime) jObject).setValue(value);
                break;
            }
        case DURATION:
            {
                jObject = objectPool.allocate(BuiltinType.ADURATION);
                int months = dis.readInt();
                long msecs = dis.readLong();
                ((JDuration) jObject).setValue(months, msecs);
                break;
            }
        case TIME:
            {
                jObject = objectPool.allocate(BuiltinType.ATIME);
                int time = dis.readInt();
                ((JTime) jObject).setValue(time);
                break;
            }
        case INTERVAL:
            {
                jObject = objectPool.allocate(BuiltinType.AINTERVAL);
                long start = dis.readLong();
                long end = dis.readLong();
                byte intervalType = dis.readByte();
                ((JInterval) jObject).setValue(start, end, intervalType);
                break;
            }
        case CIRCLE:
            {
                jObject = objectPool.allocate(BuiltinType.ACIRCLE);
                double x = dis.readDouble();
                double y = dis.readDouble();
                double radius = dis.readDouble();
                JPoint jpoint = (JPoint) objectPool.allocate(BuiltinType.APOINT);
                jpoint.setValue(x, y);
                ((JCircle) jObject).setValue(jpoint, radius);
                break;
            }
        case POINT:
            {
                jObject = objectPool.allocate(BuiltinType.APOINT);
                double x = dis.readDouble();
                double y = dis.readDouble();
                ((JPoint) jObject).setValue(x, y);
                break;
            }
        case POINT3D:
            {
                jObject = objectPool.allocate(BuiltinType.APOINT3D);
                double x = dis.readDouble();
                double y = dis.readDouble();
                double z = dis.readDouble();
                ((JPoint3D) jObject).setValue(x, y, z);
                break;
            }
        case LINE:
            {
                jObject = objectPool.allocate(BuiltinType.ALINE);
                double x1 = dis.readDouble();
                double y1 = dis.readDouble();
                double x2 = dis.readDouble();
                double y2 = dis.readDouble();
                JPoint jpoint1 = (JPoint) objectPool.allocate(BuiltinType.APOINT);
                jpoint1.setValue(x1, y1);
                JPoint jpoint2 = (JPoint) objectPool.allocate(BuiltinType.APOINT);
                jpoint2.setValue(x2, y2);
                ((JLine) jObject).setValue(jpoint1, jpoint2);
                break;
            }
        case POLYGON:
            {
                jObject = objectPool.allocate(BuiltinType.APOLYGON);
                short numberOfPoints = dis.readShort();
                List<JPoint> points = new ArrayList<JPoint>();
                for (int i = 0; i < numberOfPoints; i++) {
                    JPoint p1 = (JPoint) objectPool.allocate(BuiltinType.APOINT);
                    p1.setValue(dis.readDouble(), dis.readDouble());
                    points.add(p1);
                }
                ((JPolygon) jObject).setValue(points.toArray(new APoint[] {}));
                break;
            }
        case RECTANGLE:
            {
                jObject = objectPool.allocate(BuiltinType.ARECTANGLE);
                double x1 = dis.readDouble();
                double y1 = dis.readDouble();
                double x2 = dis.readDouble();
                double y2 = dis.readDouble();
                JPoint jpoint1 = (JPoint) objectPool.allocate(BuiltinType.APOINT);
                jpoint1.setValue(x1, y1);
                JPoint jpoint2 = (JPoint) objectPool.allocate(BuiltinType.APOINT);
                jpoint2.setValue(x2, y2);
                ((JRectangle) jObject).setValue(jpoint1, jpoint2);
                break;
            }
        case MULTISET:
            {
                AUnorderedListType listType = (AUnorderedListType) type;
                IAType elementType = listType.getItemType();
                jObject = objectPool.allocate(listType);
                boolean fixedSize = false;
                ATypeTag tag = EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(dis.readByte());
                switch(tag) {
                    case STRING:
                    case OBJECT:
                    case ARRAY:
                    case MULTISET:
                    case ANY:
                        fixedSize = false;
                        break;
                    default:
                        fixedSize = true;
                        break;
                }
                // list size
                dis.readInt();
                int numberOfitems;
                numberOfitems = dis.readInt();
                if (numberOfitems <= 0) {
                    break;
                }
                if (!fixedSize) {
                    for (int i = 0; i < numberOfitems; i++) {
                        dis.readInt();
                    }
                }
                for (int i = 0; i < numberOfitems; i++) {
                    IJObject v = getJType(elementType.getTypeTag(), elementType, dis, objectPool);
                    ((JUnorderedList) jObject).add(v);
                }
                break;
            }
        case ARRAY:
            {
                AOrderedListType listType = (AOrderedListType) type;
                IAType elementType = listType.getItemType();
                jObject = objectPool.allocate(listType);
                boolean fixedSize = false;
                ATypeTag tag = EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(dis.readByte());
                switch(tag) {
                    case STRING:
                    case OBJECT:
                    case ARRAY:
                    case MULTISET:
                    case ANY:
                        fixedSize = false;
                        break;
                    default:
                        fixedSize = true;
                        break;
                }
                // list size
                dis.readInt();
                int numberOfitems;
                numberOfitems = dis.readInt();
                if (numberOfitems <= 0) {
                    break;
                }
                if (!fixedSize) {
                    for (int i = 0; i < numberOfitems; i++) {
                        dis.readInt();
                    }
                }
                for (int i = 0; i < numberOfitems; i++) {
                    IJObject v = getJType(elementType.getTypeTag(), elementType, dis, objectPool);
                    ((JOrderedList) jObject).add(v);
                }
                break;
            }
        case OBJECT:
            ARecordType recordType = (ARecordType) type;
            int numberOfSchemaFields = recordType.getFieldTypes().length;
            byte[] recordBits = dis.getInputStream().getArray();
            boolean isExpanded = false;
            dis.getInputStream();
            int[] fieldOffsets = new int[numberOfSchemaFields];
            IJObject[] closedFields = new IJObject[numberOfSchemaFields];
            // reading length is not required.
            dis.skip(4);
            if (recordType.isOpen()) {
                isExpanded = dis.readBoolean();
                if (isExpanded) {
                    dis.readInt();
                } else {
                }
            } else {
            }
            if (numberOfSchemaFields > 0) {
                dis.readInt();
                int nullBitMapOffset = 0;
                boolean hasOptionalFields = NonTaggedFormatUtil.hasOptionalField(recordType);
                if (hasOptionalFields) {
                    nullBitMapOffset = dis.getInputStream().getPosition();
                    dis.getInputStream();
                } else {
                    dis.getInputStream();
                }
                for (int i = 0; i < numberOfSchemaFields; i++) {
                    fieldOffsets[i] = dis.readInt();
                }
                for (int fieldNumber = 0; fieldNumber < numberOfSchemaFields; fieldNumber++) {
                    if (hasOptionalFields) {
                        byte b1 = recordBits[nullBitMapOffset + fieldNumber / 8];
                        int p = 1 << (7 - (fieldNumber % 8));
                        if ((b1 & p) == 0) {
                            continue;
                        }
                    }
                    IAType[] fieldTypes = recordType.getFieldTypes();
                    ATypeTag fieldValueTypeTag = null;
                    IAType fieldType = fieldTypes[fieldNumber];
                    if (fieldTypes[fieldNumber].getTypeTag() == ATypeTag.UNION) {
                        if (((AUnionType) fieldTypes[fieldNumber]).isUnknownableType()) {
                            fieldType = ((AUnionType) fieldTypes[fieldNumber]).getActualType();
                            fieldValueTypeTag = fieldType.getTypeTag();
                        }
                    } else {
                        fieldValueTypeTag = fieldTypes[fieldNumber].getTypeTag();
                    }
                    closedFields[fieldNumber] = getJType(fieldValueTypeTag, fieldType, dis, objectPool);
                }
            }
            if (isExpanded) {
                int numberOfOpenFields = dis.readInt();
                String[] fieldNames = new String[numberOfOpenFields];
                IAType[] fieldTypes = new IAType[numberOfOpenFields];
                IJObject[] openFields = new IJObject[numberOfOpenFields];
                for (int i = 0; i < numberOfOpenFields; i++) {
                    dis.readInt();
                    dis.readInt();
                }
                for (int i = 0; i < numberOfOpenFields; i++) {
                    fieldNames[i] = AStringSerializerDeserializer.INSTANCE.deserialize(dis).getStringValue();
                    ATypeTag openFieldTypeTag = SerializerDeserializerUtil.deserializeTag(dis);
                    openFields[i] = getJType(openFieldTypeTag, null, dis, objectPool);
                    fieldTypes[i] = openFields[i].getIAObject().getType();
                }
                ARecordType openPartRecType = new ARecordType(null, fieldNames, fieldTypes, true);
                if (numberOfSchemaFields > 0) {
                    ARecordType mergedRecordType = mergeRecordTypes(recordType, openPartRecType);
                    IJObject[] mergedFields = mergeFields(closedFields, openFields);
                    jObject = objectPool.allocate(recordType);
                    return new JRecord(mergedRecordType, mergedFields);
                } else {
                    return new JRecord(recordType, openFields);
                }
            } else {
                return new JRecord(recordType, closedFields);
            }
        default:
            throw new RuntimeDataException(ErrorCode.LIBRARY_JOBJECT_UTIL_ILLEGAL_ARGU_TYPE, typeTag);
    }
    return jObject;
}
Also used : JRecord(org.apache.asterix.external.library.java.JObjects.JRecord) JPoint(org.apache.asterix.external.library.java.JObjects.JPoint) AUnionType(org.apache.asterix.om.types.AUnionType) AOrderedListType(org.apache.asterix.om.types.AOrderedListType) IJObject(org.apache.asterix.external.api.IJObject) JString(org.apache.asterix.external.library.java.JObjects.JString) AUnorderedListType(org.apache.asterix.om.types.AUnorderedListType) JPoint(org.apache.asterix.external.library.java.JObjects.JPoint) APoint(org.apache.asterix.om.base.APoint) JBoolean(org.apache.asterix.external.library.java.JObjects.JBoolean) ATypeTag(org.apache.asterix.om.types.ATypeTag) JUnorderedList(org.apache.asterix.external.library.java.JObjects.JUnorderedList) ArrayList(java.util.ArrayList) JOrderedList(org.apache.asterix.external.library.java.JObjects.JOrderedList) List(java.util.List) ARecordType(org.apache.asterix.om.types.ARecordType) IAType(org.apache.asterix.om.types.IAType) RuntimeDataException(org.apache.asterix.common.exceptions.RuntimeDataException)

Aggregations

JRecord (org.apache.asterix.external.library.java.JObjects.JRecord)8 JString (org.apache.asterix.external.library.java.JObjects.JString)8 JBoolean (org.apache.asterix.external.library.java.JObjects.JBoolean)3 JDouble (org.apache.asterix.external.library.java.JObjects.JDouble)3 JInt (org.apache.asterix.external.library.java.JObjects.JInt)3 JOrderedList (org.apache.asterix.external.library.java.JObjects.JOrderedList)3 JPoint (org.apache.asterix.external.library.java.JObjects.JPoint)3 JUnorderedList (org.apache.asterix.external.library.java.JObjects.JUnorderedList)3 IJObject (org.apache.asterix.external.api.IJObject)2 JCircle (org.apache.asterix.external.library.java.JObjects.JCircle)2 JDate (org.apache.asterix.external.library.java.JObjects.JDate)2 JDateTime (org.apache.asterix.external.library.java.JObjects.JDateTime)2 JDuration (org.apache.asterix.external.library.java.JObjects.JDuration)2 JFloat (org.apache.asterix.external.library.java.JObjects.JFloat)2 JLine (org.apache.asterix.external.library.java.JObjects.JLine)2 JPoint3D (org.apache.asterix.external.library.java.JObjects.JPoint3D)2 JPolygon (org.apache.asterix.external.library.java.JObjects.JPolygon)2 JTime (org.apache.asterix.external.library.java.JObjects.JTime)2 AOrderedListType (org.apache.asterix.om.types.AOrderedListType)2 ARecordType (org.apache.asterix.om.types.ARecordType)2