List of usage examples for com.google.gson.stream JsonReader beginArray
public void beginArray() throws IOException
From source file:co.cask.cdap.client.StreamClient.java
License:Apache License
/** * Reads events from a stream/*from w w w . j a v a2 s. c o m*/ * * @param streamId ID of the stream * @param start Timestamp in milliseconds or now-xs format to start reading event from (inclusive) * @param end Timestamp in milliseconds or now-xs format for the last event to read (exclusive) * @param limit Maximum number of events to read * @param callback Callback to invoke for each stream event read. If the callback function returns {@code false} * upon invocation, it will stops the reading * @throws IOException If fails to read from stream * @throws StreamNotFoundException If the given stream does not exists */ public void getEvents(Id.Stream streamId, String start, String end, int limit, Function<? super StreamEvent, Boolean> callback) throws IOException, StreamNotFoundException, UnauthenticatedException { long startTime = TimeMathParser.parseTime(start, TimeUnit.MILLISECONDS); long endTime = TimeMathParser.parseTime(end, TimeUnit.MILLISECONDS); URL url = config.resolveNamespacedURLV3(streamId.getNamespace(), String .format("streams/%s/events?start=%d&end=%d&limit=%d", streamId.getId(), startTime, endTime, limit)); HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); AccessToken accessToken = config.getAccessToken(); if (accessToken != null) { urlConn.setRequestProperty(HttpHeaders.AUTHORIZATION, accessToken.getTokenType() + " " + accessToken.getValue()); } if (urlConn instanceof HttpsURLConnection && !config.isVerifySSLCert()) { try { HttpRequests.disableCertCheck((HttpsURLConnection) urlConn); } catch (Exception e) { // TODO: Log "Got exception while disabling SSL certificate check for request.getURL()" } } try { if (urlConn.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED) { throw new UnauthenticatedException("Unauthorized status code received from the server."); } if (urlConn.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) { throw new StreamNotFoundException(streamId); } if (urlConn.getResponseCode() == HttpURLConnection.HTTP_NO_CONTENT) { return; } // The response is an array of stream event object InputStream inputStream = urlConn.getInputStream(); JsonReader jsonReader = new JsonReader(new InputStreamReader(inputStream, Charsets.UTF_8)); jsonReader.beginArray(); while (jsonReader.peek() != JsonToken.END_ARRAY) { Boolean result = callback.apply(GSON.<StreamEvent>fromJson(jsonReader, StreamEvent.class)); if (result == null || !result) { break; } } drain(inputStream); // No need to close reader, the urlConn.disconnect in finally will close all underlying streams } finally { urlConn.disconnect(); } }
From source file:co.cask.cdap.format.StructuredRecordStringConverter.java
License:Apache License
private static byte[] readBytes(JsonReader reader) throws IOException { ByteArrayOutputStream os = new ByteArrayOutputStream(128); reader.beginArray(); while (reader.peek() != JsonToken.END_ARRAY) { os.write(reader.nextInt());/*from w w w. j a va 2s. c o m*/ } reader.endArray(); return os.toByteArray(); }
From source file:co.cask.cdap.format.StructuredRecordStringConverter.java
License:Apache License
private static List<Object> readArray(JsonReader reader, Schema elementSchema) throws IOException { List<Object> result = new ArrayList<>(); reader.beginArray(); while (reader.peek() != JsonToken.END_ARRAY) { result.add(readJson(reader, elementSchema)); }/* w w w . j av a2 s . co m*/ reader.endArray(); return result; }
From source file:co.cask.cdap.internal.io.SchemaTypeAdapter.java
License:Apache License
/** * Constructs {@link Schema.Type#UNION UNION} type schema from the json input. * * @param reader The {@link JsonReader} for streaming json input tokens. * @param knownRecords Set of record name already encountered during the reading. * @return A {@link Schema} of type {@link Schema.Type#UNION UNION}. * @throws IOException When fails to construct a valid schema from the input. *///from w w w . j ava 2s . co m private Schema readUnion(JsonReader reader, Map<String, Schema> knownRecords) throws IOException { List<Schema> unionSchemas = new ArrayList<>(); reader.beginArray(); while (reader.peek() != JsonToken.END_ARRAY) { unionSchemas.add(read(reader, knownRecords)); } reader.endArray(); return Schema.unionOf(unionSchemas); }
From source file:co.cask.cdap.internal.io.SchemaTypeAdapter.java
License:Apache License
/** * Constructs {@link Schema.Type#ENUM ENUM} type schema from the json input. * * @param reader The {@link JsonReader} for streaming json input tokens. * @return A {@link Schema} of type {@link Schema.Type#ENUM ENUM}. * @throws IOException When fails to construct a valid schema from the input. *//*from w w w .ja v a 2s.com*/ private Schema readEnum(JsonReader reader) throws IOException { if (!"symbols".equals(reader.nextName())) { throw new IOException("Property \"symbols\" missing for enum."); } List<String> enumValues = new ArrayList<>(); reader.beginArray(); while (reader.peek() != JsonToken.END_ARRAY) { enumValues.add(reader.nextString()); } reader.endArray(); return Schema.enumWith(enumValues); }
From source file:co.cask.cdap.internal.io.SchemaTypeAdapter.java
License:Apache License
/** * Constructs {@link Schema.Type#RECORD RECORD} type schema from the json input. * * @param reader The {@link JsonReader} for streaming json input tokens. * @param knownRecords Set of record name already encountered during the reading. * @return A {@link Schema} of type {@link Schema.Type#RECORD RECORD}. * @throws IOException When fails to construct a valid schema from the input. *//*from w ww . j a v a 2 s . c o m*/ private Schema readRecord(JsonReader reader, Map<String, Schema> knownRecords) throws IOException { if (!"name".equals(reader.nextName())) { throw new IOException("Property \"name\" missing for record."); } String recordName = reader.nextString(); // Read in fields schemas if (!"fields".equals(reader.nextName())) { throw new IOException("Property \"fields\" missing for record."); } /* put a null schema schema is null and in the map if this is a recursive reference. for example, if we are looking at the outer 'node' reference in the example below, when we get to the inner 'node' reference, we need some way to know that its a record type and not a Schema.Type. { "type": "record", "name": "node", "fields": [{ "name": "children", "type": [{ "type": "array", "items": ["node", "null"] }, "null"] }, { "name": "data", "type": "int" }] } the full schema will be put in at the end of this method */ knownRecords.put(recordName, null); List<Schema.Field> fieldBuilder = new ArrayList<>(); reader.beginArray(); while (reader.peek() != JsonToken.END_ARRAY) { reader.beginObject(); if (!"name".equals(reader.nextName())) { throw new IOException("Property \"name\" missing for record field."); } String fieldName = reader.nextString(); fieldBuilder.add(Schema.Field.of(fieldName, readInnerSchema(reader, "type", knownRecords))); reader.endObject(); } reader.endArray(); Schema schema = Schema.recordOf(recordName, fieldBuilder); knownRecords.put(recordName, schema); return schema; }
From source file:co.cask.common.internal.io.SchemaTypeAdapter.java
License:Apache License
/** * Constructs {@link Schema.Type#UNION UNION} type schema from the json input. * * @param reader The {@link JsonReader} for streaming json input tokens. * @param knownRecords Set of record name already encountered during the reading. * @return A {@link Schema} of type {@link Schema.Type#UNION UNION}. * @throws java.io.IOException When fails to construct a valid schema from the input. *//* www . jav a 2 s . co m*/ private Schema readUnion(JsonReader reader, Set<String> knownRecords) throws IOException { ImmutableList.Builder<Schema> unionSchemas = ImmutableList.builder(); reader.beginArray(); while (reader.peek() != JsonToken.END_ARRAY) { unionSchemas.add(read(reader, knownRecords)); } reader.endArray(); return Schema.unionOf(unionSchemas.build()); }
From source file:co.cask.common.internal.io.SchemaTypeAdapter.java
License:Apache License
/** * Constructs {@link Schema.Type#ENUM ENUM} type schema from the json input. * * @param reader The {@link JsonReader} for streaming json input tokens. * @return A {@link Schema} of type {@link Schema.Type#ENUM ENUM}. * @throws java.io.IOException When fails to construct a valid schema from the input. *//* ww w . j ava 2s .com*/ private Schema readEnum(JsonReader reader) throws IOException { if (!"symbols".equals(reader.nextName())) { throw new IOException("Property \"symbols\" missing for enum."); } ImmutableList.Builder<String> enumValues = ImmutableList.builder(); reader.beginArray(); while (reader.peek() != JsonToken.END_ARRAY) { enumValues.add(reader.nextString()); } reader.endArray(); return Schema.enumWith(enumValues.build()); }
From source file:co.cask.common.internal.io.SchemaTypeAdapter.java
License:Apache License
/** * Constructs {@link Schema.Type#RECORD RECORD} type schema from the json input. * * @param reader The {@link JsonReader} for streaming json input tokens. * @param knownRecords Set of record name already encountered during the reading. * @return A {@link Schema} of type {@link Schema.Type#RECORD RECORD}. * @throws java.io.IOException When fails to construct a valid schema from the input. *///from w w w .j a v a 2 s. c o m private Schema readRecord(JsonReader reader, Set<String> knownRecords) throws IOException { if (!"name".equals(reader.nextName())) { throw new IOException("Property \"name\" missing for record."); } String recordName = reader.nextString(); // Read in fields schemas if (!"fields".equals(reader.nextName())) { throw new IOException("Property \"fields\" missing for record."); } knownRecords.add(recordName); ImmutableList.Builder<Schema.Field> fieldBuilder = ImmutableList.builder(); reader.beginArray(); while (reader.peek() != JsonToken.END_ARRAY) { reader.beginObject(); if (!"name".equals(reader.nextName())) { throw new IOException("Property \"name\" missing for record field."); } String fieldName = reader.nextString(); fieldBuilder.add(Schema.Field.of(fieldName, readInnerSchema(reader, "type", knownRecords))); reader.endObject(); } reader.endArray(); return Schema.recordOf(recordName, fieldBuilder.build()); }
From source file:com.aelitis.azureus.util.ObjectTypeAdapterLong.java
License:Apache License
@Override public Object read(JsonReader in) throws IOException { JsonToken token = in.peek();// w w w . ja va2s . c o m switch (token) { case BEGIN_ARRAY: List<Object> list = new ArrayList<Object>(); in.beginArray(); while (in.hasNext()) { list.add(read(in)); } in.endArray(); return list; case BEGIN_OBJECT: Map<String, Object> map = new LinkedTreeMap<String, Object>(); in.beginObject(); while (in.hasNext()) { map.put(in.nextName(), read(in)); } in.endObject(); return map; case STRING: return in.nextString(); case NUMBER: { String value = in.nextString(); if (value.indexOf('.') >= 0) { return Double.parseDouble(value); } else { return Long.parseLong(value); } } case BOOLEAN: return in.nextBoolean(); case NULL: in.nextNull(); return null; default: throw new IllegalStateException(); } }