Search in sources :

Example 1 with EndOfFileException

use of voldemort.serialization.json.EndOfFileException in project voldemort by voldemort.

the class VoldemortClientShell method parseObject.

public static Object parseObject(SerializerDefinition serializerDef, String argStr, MutableInt parsePos, PrintStream errorStream) {
    Object obj = null;
    try {
        // TODO everything is read as json string now..
        JsonReader jsonReader = new JsonReader(new StringReader(argStr));
        obj = jsonReader.read();
        // mark how much of the original string, we blew through to
        // extract the avrostring.
        parsePos.setValue(jsonReader.getCurrentLineOffset() - 1);
        if (StoreDefinitionUtils.isAvroSchema(serializerDef.getName())) {
            // TODO Need to check all the avro siblings work
            // For avro, we hack and extract avro key/value as a string,
            // before we do the actual parsing with the schema
            String avroString = (String) obj;
            // From here on, this is just normal avro parsing.
            Schema latestSchema = Schema.parse(serializerDef.getCurrentSchemaInfo());
            try {
                JsonDecoder decoder = new JsonDecoder(latestSchema, avroString);
                GenericDatumReader<Object> datumReader = new GenericDatumReader<Object>(latestSchema);
                obj = datumReader.read(null, decoder);
            } catch (IOException io) {
                errorStream.println("Error parsing avro string " + avroString);
                io.printStackTrace();
            }
        } else {
            // all json processing does some numeric type tightening
            obj = tightenNumericTypes(obj);
        }
    } catch (EndOfFileException eof) {
        // can be thrown from the jsonReader.read(..) call indicating, we
        // have nothing more to read.
        obj = null;
    }
    return obj;
}
Also used : JsonDecoder(org.apache.avro.io.JsonDecoder) EndOfFileException(voldemort.serialization.json.EndOfFileException) GenericDatumReader(org.apache.avro.generic.GenericDatumReader) Schema(org.apache.avro.Schema) StringReader(java.io.StringReader) JsonReader(voldemort.serialization.json.JsonReader) IOException(java.io.IOException)

Example 2 with EndOfFileException

use of voldemort.serialization.json.EndOfFileException in project voldemort by voldemort.

the class VoldemortClientShell method evaluateCommand.

// useful as this separates the repeated prompt from the evaluation
// using no modifier as no sub-class will have access but all classes within
// package will
boolean evaluateCommand(String line, boolean printCommands) {
    try {
        if (line.toLowerCase().startsWith("put")) {
            processPut(line.substring("put".length()));
        } else if (line.toLowerCase().startsWith("getall")) {
            processGetAll(line.substring("getall".length()));
        } else if (line.toLowerCase().startsWith("getmetadata")) {
            String[] args = line.substring("getmetadata".length() + 1).split("\\s+");
            int remoteNodeId = Integer.valueOf(args[0]);
            String key = args[1];
            Versioned<String> versioned = adminClient.metadataMgmtOps.getRemoteMetadata(remoteNodeId, key);
            if (versioned == null) {
                commandOutput.println("null");
            } else {
                commandOutput.println(versioned.getVersion());
                commandOutput.print(": ");
                commandOutput.println(versioned.getValue());
                commandOutput.println();
            }
        } else if (line.toLowerCase().startsWith("get")) {
            processGet(line.substring("get".length()));
        } else if (line.toLowerCase().startsWith("delete")) {
            processDelete(line.substring("delete".length()));
        } else if (line.startsWith("preflist")) {
            processPreflist(line.substring("preflist".length()));
        } else if (line.toLowerCase().startsWith("fetchkeys")) {
            String[] args = line.substring("fetchkeys".length() + 1).split("\\s+");
            int remoteNodeId = Integer.valueOf(args[0]);
            String storeName = args[1];
            List<Integer> partititionList = parseCsv(args[2]);
            Iterator<ByteArray> partitionKeys = adminClient.bulkFetchOps.fetchKeys(remoteNodeId, storeName, partititionList, null, false);
            BufferedWriter writer = null;
            try {
                if (args.length > 3) {
                    writer = new BufferedWriter(new FileWriter(new File(args[3])));
                } else
                    writer = new BufferedWriter(new OutputStreamWriter(commandOutput));
            } catch (IOException e) {
                errorStream.println("Failed to open the output stream");
                e.printStackTrace(errorStream);
            }
            if (writer != null) {
                while (partitionKeys.hasNext()) {
                    ByteArray keyByteArray = partitionKeys.next();
                    StringBuilder lineBuilder = new StringBuilder();
                    lineBuilder.append(ByteUtils.getString(keyByteArray.get(), "UTF-8"));
                    lineBuilder.append("\n");
                    writer.write(lineBuilder.toString());
                }
                writer.flush();
            }
        } else if (line.toLowerCase().startsWith("fetch")) {
            String[] args = line.substring("fetch".length() + 1).split("\\s+");
            int remoteNodeId = Integer.valueOf(args[0]);
            String storeName = args[1];
            List<Integer> partititionList = parseCsv(args[2]);
            Iterator<Pair<ByteArray, Versioned<byte[]>>> partitionEntries = adminClient.bulkFetchOps.fetchEntries(remoteNodeId, storeName, partititionList, null, false);
            BufferedWriter writer = null;
            try {
                if (args.length > 3) {
                    writer = new BufferedWriter(new FileWriter(new File(args[3])));
                } else
                    writer = new BufferedWriter(new OutputStreamWriter(commandOutput));
            } catch (IOException e) {
                errorStream.println("Failed to open the output stream");
                e.printStackTrace(errorStream);
            }
            if (writer != null) {
                while (partitionEntries.hasNext()) {
                    Pair<ByteArray, Versioned<byte[]>> pair = partitionEntries.next();
                    ByteArray keyByteArray = pair.getFirst();
                    Versioned<byte[]> versioned = pair.getSecond();
                    StringBuilder lineBuilder = new StringBuilder();
                    lineBuilder.append(ByteUtils.getString(keyByteArray.get(), "UTF-8"));
                    lineBuilder.append("\t");
                    lineBuilder.append(versioned.getVersion());
                    lineBuilder.append("\t");
                    lineBuilder.append(ByteUtils.getString(versioned.getValue(), "UTF-8"));
                    lineBuilder.append("\n");
                    writer.write(lineBuilder.toString());
                }
                writer.flush();
            }
        } else if (line.startsWith("help")) {
            commandOutput.println();
            commandOutput.println("Commands:");
            commandOutput.println(PROMPT + "put key value --- Associate the given value with the key.");
            commandOutput.println(PROMPT + "get key --- Retrieve the value associated with the key.");
            commandOutput.println(PROMPT + "getall key1 [key2...] --- Retrieve the value(s) associated with the key(s).");
            commandOutput.println(PROMPT + "delete key --- Remove all values associated with the key.");
            commandOutput.println(PROMPT + "preflist key --- Get node preference list for given key.");
            String metaKeyValues = voldemort.store.metadata.MetadataStore.METADATA_KEYS.toString();
            commandOutput.println(PROMPT + "getmetadata node_id meta_key --- Get store metadata associated " + "with meta_key from node_id. meta_key may be one of " + metaKeyValues.substring(1, metaKeyValues.length() - 1) + ".");
            commandOutput.println(PROMPT + "fetchkeys node_id store_name partitions <file_name> --- Fetch all keys " + "from given partitions (a comma separated list) of store_name on " + "node_id. Optionally, write to file_name. " + "Use getmetadata to determine appropriate values for store_name and partitions");
            commandOutput.println(PROMPT + "fetch node_id store_name partitions <file_name> --- Fetch all entries " + "from given partitions (a comma separated list) of store_name on " + "node_id. Optionally, write to file_name. " + "Use getmetadata to determine appropriate values for store_name and partitions");
            commandOutput.println(PROMPT + "help --- Print this message.");
            commandOutput.println(PROMPT + "exit --- Exit from this shell.");
            commandOutput.println();
            commandOutput.println("Avro usage:");
            commandOutput.println("For avro keys or values, ensure that the entire json string is enclosed within single quotes (').");
            commandOutput.println("Also, the field names and strings should STRICTLY be enclosed by double quotes(\")");
            commandOutput.println("eg: > put '{\"id\":1,\"name\":\"Vinoth Chandar\"}' '[{\"skill\":\"java\", \"score\":90.27, \"isendorsed\": true}]'");
        } else if (line.equals("quit") || line.equals("exit")) {
            commandOutput.println("bye.");
            System.exit(0);
        } else {
            errorStream.println("Invalid command. (Try 'help' for usage.)");
            return false;
        }
    } catch (EndOfFileException e) {
        errorStream.println("Expected additional token.");
    } catch (SerializationException e) {
        errorStream.print("Error serializing values: ");
        e.printStackTrace(errorStream);
    } catch (VoldemortException e) {
        errorStream.println("Exception thrown during operation.");
        e.printStackTrace(errorStream);
    } catch (ArrayIndexOutOfBoundsException e) {
        errorStream.println("Invalid command. (Try 'help' for usage.)");
    } catch (Exception e) {
        errorStream.println("Unexpected error:");
        e.printStackTrace(errorStream);
    }
    return true;
}
Also used : SerializationException(voldemort.serialization.SerializationException) Versioned(voldemort.versioning.Versioned) EndOfFileException(voldemort.serialization.json.EndOfFileException) FileWriter(java.io.FileWriter) IOException(java.io.IOException) SerializationException(voldemort.serialization.SerializationException) EndOfFileException(voldemort.serialization.json.EndOfFileException) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) Iterator(java.util.Iterator) ByteArray(voldemort.utils.ByteArray) OutputStreamWriter(java.io.OutputStreamWriter) List(java.util.List) ArrayList(java.util.ArrayList) File(java.io.File) Pair(voldemort.utils.Pair)

Aggregations

IOException (java.io.IOException)2 EndOfFileException (voldemort.serialization.json.EndOfFileException)2 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 OutputStreamWriter (java.io.OutputStreamWriter)1 StringReader (java.io.StringReader)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Schema (org.apache.avro.Schema)1 GenericDatumReader (org.apache.avro.generic.GenericDatumReader)1 JsonDecoder (org.apache.avro.io.JsonDecoder)1 SerializationException (voldemort.serialization.SerializationException)1 JsonReader (voldemort.serialization.json.JsonReader)1 ByteArray (voldemort.utils.ByteArray)1 Pair (voldemort.utils.Pair)1 Versioned (voldemort.versioning.Versioned)1