org.codehaus.jackson.JsonToken Java Examples

The following examples show how to use org.codehaus.jackson.JsonToken. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BarFileUtils.java    From io with Apache License 2.0 6 votes vote down vote up
/**
 * barファイルエントリからJSONファイルを読み込む.
 * @param <T> JSONMappedObject
 * @param inStream barファイルエントリのInputStream
 * @param entryName entryName
 * @param clazz clazz
 * @return JSONファイルから読み込んだオブジェクト
 * @throws IOException JSONファイル読み込みエラー
 */
public static <T> T readJsonEntry(
        InputStream inStream, String entryName, Class<T> clazz) throws IOException {
    JsonParser jp = null;
    ObjectMapper mapper = new ObjectMapper();
    JsonFactory f = new JsonFactory();
    jp = f.createJsonParser(inStream);
    JsonToken token = jp.nextToken(); // JSONルート要素("{")
    Pattern formatPattern = Pattern.compile(".*/+(.*)");
    Matcher formatMatcher = formatPattern.matcher(entryName);
    String jsonName = formatMatcher.replaceAll("$1");
    T json = null;
    if (token == JsonToken.START_OBJECT) {
        try {
            json = mapper.readValue(jp, clazz);
        } catch (UnrecognizedPropertyException ex) {
            throw DcCoreException.BarInstall.JSON_FILE_FORMAT_ERROR.params(jsonName);
        }
    } else {
        throw DcCoreException.BarInstall.JSON_FILE_FORMAT_ERROR.params(jsonName);
    }
    return json;
}
 
Example #2
Source File: BackportedJacksonMappingIterator.java    From elasticsearch-hadoop with Apache License 2.0 6 votes vote down vote up
/**
 * Equivalent of {@link #next} but one that may throw checked
 * exceptions from Jackson due to invalid input.
 */
public boolean hasNextValue() throws IOException {
    if (_parser == null) {
        return false;
    }
    JsonToken t = _parser.getCurrentToken();
    if (t == null) { // un-initialized or cleared; find next
        t = _parser.nextToken();
        // If EOF, no more
        if (t == null) {
            _parser.close();
            return false;
        }
        // And similarly if we hit END_ARRAY; except that we won't close parser
        if (t == JsonToken.END_ARRAY) {
            return false;
        }
    }
    return true;
}
 
Example #3
Source File: BackportedJacksonMappingIterator.java    From elasticsearch-hadoop with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
protected BackportedJacksonMappingIterator(JavaType type, JsonParser jp, DeserializationContext ctxt, JsonDeserializer<?> deser) {
    _type = type;
    _parser = jp;
    _context = ctxt;
    _deserializer = (JsonDeserializer<T>) deser;

    /* One more thing: if we are at START_ARRAY (but NOT root-level
     * one!), advance to next token (to allow matching END_ARRAY)
     */
    if (jp != null && jp.getCurrentToken() == JsonToken.START_ARRAY) {
        JsonStreamContext sc = jp.getParsingContext();
        // safest way to skip current token is to clear it (so we'll advance soon)
        if (!sc.inRoot()) {
            jp.clearCurrentToken();
        }
    }
}
 
Example #4
Source File: AbstractJsonRowRecordReader.java    From nifi with Apache License 2.0 6 votes vote down vote up
protected JsonNode getNextJsonNode() throws IOException, MalformedRecordException {
    if (!firstObjectConsumed) {
        firstObjectConsumed = true;
        return firstJsonNode;
    }

    while (true) {
        final JsonToken token = jsonParser.nextToken();
        if (token == null) {
            return null;
        }

        switch (token) {
            case END_OBJECT:
                continue;
            case START_OBJECT:
                return jsonParser.readValueAsTree();
            case END_ARRAY:
            case START_ARRAY:
                continue;

            default:
                throw new MalformedRecordException("Expected to get a JSON Object but got a token of type " + token.name());
        }
    }
}
 
Example #5
Source File: AbstractSiteToSiteReportingTask.java    From nifi with Apache License 2.0 6 votes vote down vote up
private JsonNode getNextJsonNode() throws IOException, MalformedRecordException {
    if (!firstObjectConsumed) {
        firstObjectConsumed = true;
        return firstJsonNode;
    }
    while (true) {
        final JsonToken token = jsonParser.nextToken();
        if (token == null) {
            return null;
        }
        switch (token) {
            case END_OBJECT:
                continue;
            case START_OBJECT:
                return jsonParser.readValueAsTree();
            case END_ARRAY:
            case START_ARRAY:
                return null;
            default:
                throw new MalformedRecordException("Expected to get a JSON Object but got a token of type " + token.name());
        }
    }
}
 
Example #6
Source File: AbstractSiteToSiteReportingTask.java    From nifi with Apache License 2.0 6 votes vote down vote up
public JsonRecordReader(final InputStream in, RecordSchema recordSchema) throws IOException, MalformedRecordException {
    this.recordSchema = recordSchema;
    try {
        jsonParser = new JsonFactory().createJsonParser(in);
        jsonParser.setCodec(new ObjectMapper());
        JsonToken token = jsonParser.nextToken();
        if (token == JsonToken.START_ARRAY) {
            array = true;
            token = jsonParser.nextToken();
        } else {
            array = false;
        }
        if (token == JsonToken.START_OBJECT) {
            firstJsonNode = jsonParser.readValueAsTree();
        } else {
            firstJsonNode = null;
        }
    } catch (final JsonParseException e) {
        throw new MalformedRecordException("Could not parse data as JSON", e);
    }
}
 
Example #7
Source File: RemoteTableJoinExample.java    From samza-hello-samza with Apache License 2.0 6 votes vote down vote up
@Override
public CompletableFuture<Double> getAsync(String symbol) {
  return CompletableFuture.supplyAsync(() -> {
    try {
      URL url = new URL(String.format(URL_TEMPLATE, symbol));
      String response = HttpUtil.read(url, 5000, new ExponentialSleepStrategy());
      JsonParser parser = new JsonFactory().createJsonParser(response);
      while (!parser.isClosed()) {
        if (JsonToken.FIELD_NAME.equals(parser.nextToken()) && "4. close".equalsIgnoreCase(parser.getCurrentName())) {
          return Double.valueOf(parser.nextTextValue());
        }
      }
      return -1d;
    } catch (Exception ex) {
      throw new SamzaException(ex);
    }
  });
}
 
Example #8
Source File: ClientObjectMapper.java    From hraven with Apache License 2.0 6 votes vote down vote up
@Override
public CounterMap deserialize(JsonParser jsonParser,
                              DeserializationContext deserializationContext)
                              throws IOException {
  CounterMap counterMap = new CounterMap();

  JsonToken token;
  while ((token = jsonParser.nextToken()) != JsonToken.END_OBJECT) {
    assertToken(token, JsonToken.FIELD_NAME);
    String group = jsonParser.getCurrentName();

    assertToken(jsonParser.nextToken(), JsonToken.START_OBJECT);
    while ((token = jsonParser.nextToken()) != JsonToken.END_OBJECT) {
      if (token != JsonToken.VALUE_NUMBER_INT) {
        continue; // all deserialized values are ints
      }

      Counter counter =
          new Counter(group, jsonParser.getCurrentName(), jsonParser.getLongValue());
      counterMap.add(counter);
    }
  }
  return counterMap;
}
 
Example #9
Source File: EventResource.java    From io with Apache License 2.0 6 votes vote down vote up
/**
 * リクエストボディを解析してEventオブジェクトを取得する.
 * @param reader Http入力ストリーム
 * @return 解析したEventオブジェクト
 */
protected JSONEvent getRequestBody(final Reader reader) {
    JSONEvent event = null;
    JsonParser jp = null;
    ObjectMapper mapper = new ObjectMapper();
    JsonFactory f = new JsonFactory();
    try {
        jp = f.createJsonParser(reader);
        JsonToken token = jp.nextToken(); // JSONルート要素("{")
        if (token == JsonToken.START_OBJECT) {
            event = mapper.readValue(jp, JSONEvent.class);
        } else {
            throw DcCoreException.Event.JSON_PARSE_ERROR;
        }
    } catch (IOException e) {
        throw DcCoreException.Event.JSON_PARSE_ERROR;
    }
    return event;
}
 
Example #10
Source File: HiveJsonStructReader.java    From incubator-hivemall with Apache License 2.0 6 votes vote down vote up
private Object parseMapKey(JsonParser parser, PrimitiveObjectInspector oi)
        throws SerDeException, IOException {
    JsonToken currentToken = parser.getCurrentToken();
    if (currentToken == null) {
        return null;
    }
    try {
        switch (parser.getCurrentToken()) {
            case FIELD_NAME:
                return getObjectOfCorrespondingPrimitiveType(parser.getText(), oi);
            case VALUE_NULL:
                return null;
            default:
                throw new SerDeException("unexpected token type: " + currentToken);
        }
    } finally {
        parser.nextToken();
    }
}
 
Example #11
Source File: HiveJsonStructReader.java    From incubator-hivemall with Apache License 2.0 6 votes vote down vote up
private Object parsePrimitive(JsonParser parser, PrimitiveObjectInspector oi)
        throws SerDeException, IOException {
    JsonToken currentToken = parser.getCurrentToken();
    if (currentToken == null) {
        return null;
    }
    try {
        switch (parser.getCurrentToken()) {
            case VALUE_FALSE:
            case VALUE_TRUE:
            case VALUE_NUMBER_INT:
            case VALUE_NUMBER_FLOAT:
            case VALUE_STRING:
                return getObjectOfCorrespondingPrimitiveType(parser.getText(), oi);
            case VALUE_NULL:
                return null;
            default:
                throw new SerDeException("unexpected token type: " + currentToken);
        }
    } finally {
        parser.nextToken();
    }
}
 
Example #12
Source File: HiveJsonStructReader.java    From incubator-hivemall with Apache License 2.0 6 votes vote down vote up
private static void skipValue(JsonParser parser) throws JsonParseException, IOException {
    int array = 0;
    int object = 0;
    do {
        JsonToken currentToken = parser.getCurrentToken();
        if (currentToken == JsonToken.START_ARRAY) {
            array++;
        }
        if (currentToken == JsonToken.END_ARRAY) {
            array--;
        }
        if (currentToken == JsonToken.START_OBJECT) {
            object++;
        }
        if (currentToken == JsonToken.END_OBJECT) {
            object--;
        }

        parser.nextToken();

    } while (array > 0 || object > 0);
}
 
Example #13
Source File: JsonSerdeUtils.java    From incubator-hivemall with Apache License 2.0 6 votes vote down vote up
@Nonnull
private static Object parseValue(@Nonnull final JsonParser p)
        throws JsonParseException, IOException {
    final JsonToken t = p.getCurrentToken();
    switch (t) {
        case VALUE_FALSE:
            return Boolean.FALSE;
        case VALUE_TRUE:
            return Boolean.TRUE;
        case VALUE_NULL:
            return null;
        case VALUE_STRING:
            return p.getText();
        case VALUE_NUMBER_FLOAT:
            return p.getDoubleValue();
        case VALUE_NUMBER_INT:
            return p.getIntValue();
        default:
            throw new IOException("Unexpected token: " + t);
    }
}
 
Example #14
Source File: JsonSerdeUtils.java    From incubator-hivemall with Apache License 2.0 6 votes vote down vote up
@Nonnull
private static List<Object> parseArray(@Nonnull final JsonParser p,
        @CheckForNull final List<TypeInfo> columnTypes)
        throws HCatException, IOException, SerDeException {
    Preconditions.checkNotNull(columnTypes, "columnTypes MUST NOT be null",
        SerDeException.class);
    if (columnTypes.size() != 1) {
        throw new IOException("Expected a single array but go " + columnTypes);
    }

    TypeInfo elemType = columnTypes.get(0);
    HCatSchema schema = HCatSchemaUtils.getHCatSchema(elemType);

    HCatFieldSchema listSchema = schema.get(0);
    HCatFieldSchema elemSchema = listSchema.getArrayElementSchema().get(0);

    final List<Object> arr = new ArrayList<Object>();
    while (p.nextToken() != JsonToken.END_ARRAY) {
        arr.add(extractCurrentField(p, elemSchema, true));
    }
    return arr;
}
 
Example #15
Source File: HiveJsonStructReader.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
private Object parseList(JsonParser parser, ListObjectInspector oi)
        throws JsonParseException, IOException, SerDeException {
    List<Object> ret = new ArrayList<>();

    if (parser.getCurrentToken() == JsonToken.VALUE_NULL) {
        parser.nextToken();
        return null;
    }

    if (parser.getCurrentToken() != JsonToken.START_ARRAY) {
        throw new SerDeException("array expected");
    }
    ObjectInspector eOI = oi.getListElementObjectInspector();
    JsonToken currentToken = parser.nextToken();
    try {
        while (currentToken != null && currentToken != JsonToken.END_ARRAY) {
            ret.add(parseDispatcher(parser, eOI));
            currentToken = parser.getCurrentToken();
        }
    } catch (Exception e) {
        throw new SerDeException("array: " + e.getMessage(), e);
    }

    currentToken = parser.nextToken();

    return ret;
}
 
Example #16
Source File: BarFileReadRunner.java    From io with Apache License 2.0 5 votes vote down vote up
/**
 * 10_relations.json, 20_roles.json, 30_extroles.json, 70_$links.json, 10_odatarelations.jsonのバリデートチェック.
 * @param jp Jsonパース
 * @param mapper ObjectMapper
 * @param jsonName ファイル名
 * @throws IOException IOException
 */
protected void registJsonEntityData(JsonParser jp, ObjectMapper mapper, String jsonName) throws IOException {
    JsonToken token;
    token = jp.nextToken();

    // Relations,Roles,ExtRoles,$linksのチェック
    checkMatchFieldName(jp, jsonName);

    token = jp.nextToken();
    // 配列でなければエラー
    if (token != JsonToken.START_ARRAY) {
        throw DcCoreException.BarInstall.JSON_FILE_FORMAT_ERROR.params(jsonName);
    }
    token = jp.nextToken();

    while (jp.hasCurrentToken()) {
        if (token == JsonToken.END_ARRAY) {
            break;
        } else if (token != JsonToken.START_OBJECT) {
            throw DcCoreException.BarInstall.JSON_FILE_FORMAT_ERROR.params(jsonName);
        }

        // 1件登録処理
        JSONMappedObject mappedObject = barFileJsonValidate(jp, mapper, jsonName);
        if (jsonName.equals(RELATION_JSON)) {
            createRelation(mappedObject.getJson());
        } else if (jsonName.equals(ROLE_JSON)) {
            createRole(mappedObject.getJson());
        } else if (jsonName.equals(EXTROLE_JSON)) {
            createExtRole(mappedObject.getJson());
        } else if (jsonName.equals(LINKS_JSON)) {
            createLinks(mappedObject, odataProducer);
        }

        token = jp.nextToken();
    }
}
 
Example #17
Source File: HiveJsonStructReader.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
private Object parseMap(JsonParser parser, MapObjectInspector oi)
        throws IOException, SerDeException {
    if (parser.getCurrentToken() == JsonToken.VALUE_NULL) {
        parser.nextToken();
        return null;
    }

    Map<Object, Object> ret = new LinkedHashMap<>();

    if (parser.getCurrentToken() != JsonToken.START_OBJECT) {
        throw new SerDeException("struct expected");
    }

    if (!(oi.getMapKeyObjectInspector() instanceof PrimitiveObjectInspector)) {
        throw new SerDeException("map key must be a primitive");
    }
    PrimitiveObjectInspector keyOI = (PrimitiveObjectInspector) oi.getMapKeyObjectInspector();
    ObjectInspector valOI = oi.getMapValueObjectInspector();

    JsonToken currentToken = parser.nextToken();
    while (currentToken != null && currentToken != JsonToken.END_OBJECT) {

        if (currentToken != JsonToken.FIELD_NAME) {
            throw new SerDeException("unexpected token: " + currentToken);
        }

        Object key = parseMapKey(parser, keyOI);
        Object val = parseDispatcher(parser, valOI);
        ret.put(key, val);

        currentToken = parser.getCurrentToken();
    }
    if (currentToken != null) {
        parser.nextToken();
    }
    return ret;
}
 
Example #18
Source File: JsonSerdeUtils.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
private static void skipValue(@Nonnull final JsonParser p)
        throws JsonParseException, IOException {
    JsonToken valueToken = p.nextToken();
    if ((valueToken == JsonToken.START_ARRAY) || (valueToken == JsonToken.START_OBJECT)) {
        // if the currently read token is a beginning of an array or object, move stream forward
        // skipping any child tokens till we're at the corresponding END_ARRAY or END_OBJECT token
        p.skipChildren();
    }
}
 
Example #19
Source File: ClientObjectMapper.java    From hraven with Apache License 2.0 5 votes vote down vote up
@Override
public Configuration deserialize(JsonParser jsonParser,
                                 DeserializationContext deserializationContext)
                                 throws IOException {
  Configuration conf = new Configuration();

  JsonToken token;
  while ((token = jsonParser.nextToken()) != JsonToken.END_OBJECT) {
    if (token != JsonToken.VALUE_STRING) { continue; } // all deserialized values are strings
    conf.set(jsonParser.getCurrentName(), jsonParser.getText());
  }

  return conf;
}
 
Example #20
Source File: JsonSerdeUtils.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
private static void populateRecord(@Nonnull final List<Object> r,
        @Nonnull final JsonToken token, @Nonnull final JsonParser p,
        @Nonnull final HCatSchema s) throws IOException {
    if (token != JsonToken.FIELD_NAME) {
        throw new IOException("Field name expected");
    }
    String fieldName = p.getText();
    Integer fpos = s.getPosition(fieldName);
    if (fpos == null) {
        fpos = getPositionFromHiveInternalColumnName(fieldName);
        if (fpos == -1) {
            skipValue(p);
            return; // unknown field, we return. We'll continue from the next field onwards.
        }
        // If we get past this, then the column name did match the hive pattern for an internal
        // column name, such as _col0, etc, so it *MUST* match the schema for the appropriate column.
        // This means people can't use arbitrary column names such as _col0, and expect us to ignore it
        // if we find it.
        if (!fieldName.equalsIgnoreCase(getHiveInternalColumnName(fpos))) {
            throw new IOException("Hive internal column name (" + fieldName
                    + ") and position encoding (" + fpos + ") for the column name are at odds");
        }
        // If we reached here, then we were successful at finding an alternate internal
        // column mapping, and we're about to proceed.
    }
    HCatFieldSchema hcatFieldSchema = s.getFields().get(fpos);
    Object currField = extractCurrentField(p, hcatFieldSchema, false);
    r.set(fpos, currField);
}
 
Example #21
Source File: AbstractJsonRowRecordReader.java    From nifi with Apache License 2.0 5 votes vote down vote up
public AbstractJsonRowRecordReader(final InputStream in, final ComponentLog logger, final String dateFormat, final String timeFormat, final String timestampFormat)
        throws IOException, MalformedRecordException {

    this.logger = logger;

    final DateFormat df = dateFormat == null ? null : DataTypeUtils.getDateFormat(dateFormat);
    final DateFormat tf = timeFormat == null ? null : DataTypeUtils.getDateFormat(timeFormat);
    final DateFormat tsf = timestampFormat == null ? null : DataTypeUtils.getDateFormat(timestampFormat);

    LAZY_DATE_FORMAT = () -> df;
    LAZY_TIME_FORMAT = () -> tf;
    LAZY_TIMESTAMP_FORMAT = () -> tsf;

    try {
        jsonParser = jsonFactory.createJsonParser(in);
        jsonParser.setCodec(codec);

        JsonToken token = jsonParser.nextToken();
        if (token == JsonToken.START_ARRAY) {
            token = jsonParser.nextToken(); // advance to START_OBJECT token
        }

        if (token == JsonToken.START_OBJECT) { // could be END_ARRAY also
            firstJsonNode = jsonParser.readValueAsTree();
        } else {
            firstJsonNode = null;
        }
    } catch (final JsonParseException e) {
        throw new MalformedRecordException("Could not parse data as JSON", e);
    }
}
 
Example #22
Source File: JsonRecordSource.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public JsonNode next() throws IOException {
    while (true) {
        final JsonToken token = jsonParser.nextToken();
        if (token == null) {
            return null;
        }

        if (token == JsonToken.START_OBJECT) {
            return jsonParser.readValueAsTree();
        }
    }
}
 
Example #23
Source File: JacksonJsonParser.java    From elasticsearch-hadoop with Apache License 2.0 5 votes vote down vote up
private Token convertToken(JsonToken token) {
    if (token == null) {
        return null;
    }
    switch (token) {
    case FIELD_NAME:
        return Token.FIELD_NAME;
    case VALUE_FALSE:
    case VALUE_TRUE:
        return Token.VALUE_BOOLEAN;
    case VALUE_STRING:
        return Token.VALUE_STRING;
    case VALUE_NUMBER_INT:
    case VALUE_NUMBER_FLOAT:
        return Token.VALUE_NUMBER;
    case VALUE_NULL:
        return Token.VALUE_NULL;
    case START_OBJECT:
        return Token.START_OBJECT;
    case END_OBJECT:
        return Token.END_OBJECT;
    case START_ARRAY:
        return Token.START_ARRAY;
    case END_ARRAY:
        return Token.END_ARRAY;
    case VALUE_EMBEDDED_OBJECT:
        return Token.VALUE_EMBEDDED_OBJECT;
    case NOT_AVAILABLE:
        throw new UnsupportedOperationException();
    }
    throw new EsHadoopSerializationException("No matching token for json_token [" + token + "]");
}
 
Example #24
Source File: JsonSerdeUtils.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
@Nonnull
private static Object parseObject(@Nonnull final JsonParser p,
        @CheckForNull final List<String> columnNames,
        @CheckForNull final List<TypeInfo> columnTypes)
        throws JsonParseException, IOException, SerDeException {
    Preconditions.checkNotNull(columnNames, "columnNames MUST NOT be null in parseObject",
        SerDeException.class);
    Preconditions.checkNotNull(columnTypes, "columnTypes MUST NOT be null in parseObject",
        SerDeException.class);
    if (columnNames.size() != columnTypes.size()) {
        throw new SerDeException(
            "Size of columnNames and columnTypes does not match. #columnNames="
                    + columnNames.size() + ", #columnTypes=" + columnTypes.size());
    }

    TypeInfo rowTypeInfo = TypeInfoFactory.getStructTypeInfo(columnNames, columnTypes);
    final HCatSchema schema;
    try {
        schema = HCatSchemaUtils.getHCatSchema(rowTypeInfo).get(0).getStructSubSchema();
    } catch (HCatException e) {
        throw new SerDeException(e);
    }

    final List<Object> r = new ArrayList<Object>(Collections.nCopies(columnNames.size(), null));
    JsonToken token;
    while (((token = p.nextToken()) != JsonToken.END_OBJECT) && (token != null)) {
        // iterate through each token, and create appropriate object here.
        populateRecord(r, token, p, schema);
    }

    if (columnTypes.size() == 1) {
        return r.get(0);
    }
    return r;
}
 
Example #25
Source File: ImportAdmins.java    From usergrid with Apache License 2.0 4 votes vote down vote up
private void validateStartArray(JsonToken token) {
    if (token != JsonToken.START_ARRAY) {
        throw new RuntimeException("Token should be START ARRAY but it is:" + token.asString());
    }
}
 
Example #26
Source File: BackendResponse.java    From ReactiveLab with Apache License 2.0 4 votes vote down vote up
private static BackendResponse parseBackendResponse(JsonParser parser) throws IOException {
    try {
        // Sanity check: verify that we got "Json Object":
        if (parser.nextToken() != JsonToken.START_OBJECT) {
            throw new IOException("Expected data to start with an Object");
        }
        long responseKey = 0;
        int delay = 0;
        int numItems = 0;
        int itemSize = 0;
        String[] items = null;
        JsonToken current;

        while (parser.nextToken() != JsonToken.END_OBJECT) {
            String fieldName = parser.getCurrentName();
            // advance
            current = parser.nextToken();
            if (fieldName.equals("responseKey")) {
                responseKey = parser.getLongValue();
            } else if (fieldName.equals("delay")) {
                delay = parser.getIntValue();
            } else if (fieldName.equals("itemSize")) {
                itemSize = parser.getIntValue();
            } else if (fieldName.equals("numItems")) {
                numItems = parser.getIntValue();
            } else if (fieldName.equals("items")) {
                // expect numItems to be populated before hitting this
                if (numItems == 0) {
                    throw new IllegalStateException("Expected numItems > 0");
                }
                items = new String[numItems];
                if (current == JsonToken.START_ARRAY) {
                    int j = 0;
                    // For each of the records in the array
                    while (parser.nextToken() != JsonToken.END_ARRAY) {
                        items[j++] = parser.getText();
                    }
                } else {
                    //                            System.out.println("Error: items should be an array: skipping.");
                    parser.skipChildren();
                }

            }
        }
        return new BackendResponse(responseKey, delay, numItems, itemSize, items);
    } finally {
        parser.close();
    }
}
 
Example #27
Source File: ImportAdmins.java    From usergrid with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
private void importMetadata(String fileName, int writeThreads) throws Exception {

    EntityManager em = emf.getEntityManager( CpNamingUtils.MANAGEMENT_APPLICATION_ID);

    File metadataFile = new File(importDir, fileName);

    logger.info("----- Loading metadata file: " + metadataFile.getAbsolutePath());

    JsonParser jp = getJsonParserForFile(metadataFile);

    JsonToken jsonToken = null; // jp.nextToken();// START_OBJECT this is the outer hashmap

    int depth = 1;

    BlockingQueue<ImportMetadataTask> workQueue = new LinkedBlockingQueue<ImportMetadataTask>();
    startMetadataWorkers(workQueue, writeThreads);

    while (depth > 0) {

        jsonToken = jp.nextToken();

        if (jsonToken == null) {
            logger.info("token is null, breaking");
            break;
        }

        if (jsonToken.equals(JsonToken.START_OBJECT)) {
            depth++;
        } else if (jsonToken.equals(JsonToken.END_OBJECT)) {
            depth--;
        }

        if (jsonToken.equals(JsonToken.FIELD_NAME) && depth == 2) {

            jp.nextToken();
            String entityOwnerId = jp.getCurrentName();

            try {
                EntityRef entityRef = new SimpleEntityRef( "user", UUID.fromString( entityOwnerId ) );
                Map<String, Object> metadata = (Map<String, Object>) jp.readValueAs( Map.class );

                workQueue.put( new ImportMetadataTask( entityRef, metadata ) );
                logger.debug( "Put user {} in metadata queue", entityRef.getUuid() );

            } catch ( Exception e ) {
                logger.debug( "Error with user {}, not putting in metadata queue", entityOwnerId );
            }
        }
    }

    waitForQueueAndMeasure(workQueue, metadataEmptyCount, metadataWorkerThreadMap, "Metadata Load");

    logger.info("----- End of metadata -----");
    jp.close();
}
 
Example #28
Source File: Import.java    From usergrid with Apache License 2.0 4 votes vote down vote up
private void validateStartArray( JsonToken token ) {
    if ( token != JsonToken.START_ARRAY ) {
        throw new RuntimeException( "Token should be START ARRAY but it is:" + token.asString() );
    }
}
 
Example #29
Source File: BackendResponse.java    From WSPerfLab with Apache License 2.0 4 votes vote down vote up
public static BackendResponse parseBackendResponse(JsonParser parser) throws IOException {
    try {
        // Sanity check: verify that we got "Json Object":
        if (parser.nextToken() != JsonToken.START_OBJECT) {
            throw new IOException("Expected data to start with an Object");
        }
        long responseKey = 0;
        int delay = 0;
        int numItems = 0;
        int itemSize = 0;
        String[] items = null;
        JsonToken current;

        while (parser.nextToken() != JsonToken.END_OBJECT) {
            String fieldName = parser.getCurrentName();
            // advance
            current = parser.nextToken();
            if (fieldName.equals("responseKey")) {
                responseKey = parser.getLongValue();
            } else if (fieldName.equals("delay")) {
                delay = parser.getIntValue();
            } else if (fieldName.equals("itemSize")) {
                itemSize = parser.getIntValue();
            } else if (fieldName.equals("numItems")) {
                numItems = parser.getIntValue();
            } else if (fieldName.equals("items")) {
                // expect numItems to be populated before hitting this
                if (numItems == 0) {
                    throw new IllegalStateException("Expected numItems > 0");
                }
                items = new String[numItems];
                if (current == JsonToken.START_ARRAY) {
                    int j = 0;
                    // For each of the records in the array
                    while (parser.nextToken() != JsonToken.END_ARRAY) {
                        items[j++] = parser.getText();
                    }
                } else {
                    //                            System.out.println("Error: items should be an array: skipping.");
                    parser.skipChildren();
                }

            }
        }
        
        return new BackendResponse(responseKey, delay, numItems, itemSize, items);
    } finally {
        parser.close();
    }
}
 
Example #30
Source File: ImportAdmins.java    From usergrid with Apache License 2.0 4 votes vote down vote up
/**
 * Imports admin users.
 *
 * @param fileName Name of admin user data file.
 */
private void importAdminUsers(final String fileName,
                              final int writeThreadCount,
                              final int auditThreadCount) throws Exception {

    int count = 0;

    File adminUsersFile = new File(importDir, fileName);

    logger.info("----- Loading file: " + adminUsersFile.getAbsolutePath());
    JsonParser jp = getJsonParserForFile(adminUsersFile);

    int loopCounter = 0;

    BlockingQueue<Map<String, Object>> workQueue = new LinkedBlockingQueue<Map<String, Object>>();
    BlockingQueue<Map<String, Object>> auditQueue = new LinkedBlockingQueue<Map<String, Object>>();

    startAdminWorkers(workQueue, auditQueue, writeThreadCount);
    startAdminAuditors(auditQueue, auditThreadCount);

    JsonToken token = jp.nextToken();
    validateStartArray(token);

    while (jp.nextValue() != JsonToken.END_ARRAY) {
        loopCounter += 1;

        @SuppressWarnings("unchecked")
        Map<String, Object> entityProps = jp.readValueAs(HashMap.class);
        if (loopCounter % 1000 == 0) {
            logger.debug( "Publishing to queue... counter=" + loopCounter );
        }

        workQueue.add( entityProps );
    }

    waitForQueueAndMeasure(workQueue, writeEmptyCount, adminWriteThreads, "Admin Write");
    waitForQueueAndMeasure(auditQueue, auditEmptyCount, adminAuditThreads, "Admin Audit");

    logger.info("----- End: Imported {} admin users from file {}",
            count, adminUsersFile.getAbsolutePath());

    jp.close();
}