Search in sources :

Example 31 with JSONObject

use of org.json_voltpatches.JSONObject in project voltdb by VoltDB.

the class TestMapCache method testInitialCache.

@Test
public void testInitialCache() throws Exception {
    ZooKeeper zk = getClient(0);
    configure("/cache01", zk);
    MapCache dut = new MapCache(zk, "/cache01");
    dut.start(true);
    Map<String, JSONObject> cache = dut.pointInTimeCache();
    assertEquals("3 items cached.", 3, cache.size());
    assertEquals("aaval", cache.get("/cache01/aa").get("key"));
    assertEquals("bbval", cache.get("/cache01/bb").get("key"));
    assertEquals("ccval", cache.get("/cache01/cc").get("key"));
    dut.shutdown();
    zk.close();
}
Also used : ZooKeeper(org.apache.zookeeper_voltpatches.ZooKeeper) JSONObject(org.json_voltpatches.JSONObject) Test(org.junit.Test)

Example 32 with JSONObject

use of org.json_voltpatches.JSONObject in project voltdb by VoltDB.

the class TestJSONInterface method testExportTypes.

public void testExportTypes() throws Exception {
    try {
        VoltProjectBuilder builder = new VoltProjectBuilder();
        builder.setHTTPDPort(8095);
        boolean success = builder.compile(Configuration.getPathToCatalogForTest("json.jar"));
        assertTrue(success);
        VoltDB.Configuration config = new VoltDB.Configuration();
        config.m_pathToCatalog = config.setPathToCatalogForTest("json.jar");
        config.m_pathToDeployment = builder.getPathToDeployment();
        server = new ServerThread(config);
        server.start();
        server.waitForInitialization();
        //Get exportTypes
        String json = getUrlOverJSON(protocolPrefix + "localhost:8095/deployment/export/types", null, null, null, 200, "application/json");
        JSONObject jobj = new JSONObject(json);
        assertTrue(jobj.getString("types").contains("FILE"));
        assertTrue(jobj.getString("types").contains("JDBC"));
        assertTrue(jobj.getString("types").contains("KAFKA"));
        assertTrue(jobj.getString("types").contains("HTTP"));
        assertTrue(jobj.getString("types").contains("RABBITMQ"));
        assertTrue(jobj.getString("types").contains("CUSTOM"));
    } finally {
        if (server != null) {
            server.shutdown();
            server.join();
        }
        server = null;
    }
}
Also used : Configuration(org.voltdb.VoltDB.Configuration) JSONObject(org.json_voltpatches.JSONObject) VoltProjectBuilder(org.voltdb.compiler.VoltProjectBuilder) Configuration(org.voltdb.VoltDB.Configuration)

Example 33 with JSONObject

use of org.json_voltpatches.JSONObject in project voltdb by VoltDB.

the class TestJSONInterface method testSimple.

public void testSimple() throws Exception {
    try {
        String simpleSchema = "create table blah (" + "ival bigint default 23 not null, " + "sval varchar(200) default 'foo', " + "dateval timestamp, " + "fval float, " + "decval decimal, " + "PRIMARY KEY(ival));";
        File schemaFile = VoltProjectBuilder.writeStringToTempFile(simpleSchema);
        String schemaPath = schemaFile.getPath();
        schemaPath = URLEncoder.encode(schemaPath, "UTF-8");
        VoltDB.Configuration config = new VoltDB.Configuration();
        VoltProjectBuilder builder = new VoltProjectBuilder();
        builder.addSchema(schemaPath);
        builder.addPartitionInfo("blah", "ival");
        builder.addStmtProcedure("Insert", "insert into blah values (?,?,?,?,?);");
        builder.addProcedures(CrazyBlahProc.class);
        builder.setHTTPDPort(8095);
        boolean success = builder.compile(Configuration.getPathToCatalogForTest("json.jar"), 1, 1, 0, 0) != null;
        assertTrue(success);
        config.m_pathToCatalog = config.setPathToCatalogForTest("json.jar");
        config.m_pathToDeployment = builder.getPathToDeployment();
        server = new ServerThread(config);
        server.start();
        server.waitForInitialization();
        ParameterSet pset;
        String responseJSON;
        Response response;
        // Call insert
        pset = ParameterSet.fromArrayNoCopy(1, "hello", new TimestampType(System.currentTimeMillis()), 5.0, "5.0");
        responseJSON = callProcOverJSON("Insert", pset, null, null, false);
        System.out.println(responseJSON);
        response = responseFromJSON(responseJSON);
        assertTrue(response.status == ClientResponse.SUCCESS);
        // Call insert again (with failure expected)
        responseJSON = callProcOverJSON("Insert", pset, null, null, false);
        System.out.println(responseJSON);
        response = responseFromJSON(responseJSON);
        assertTrue(response.status != ClientResponse.SUCCESS);
        // Call proc with complex params
        pset = ParameterSet.fromArrayNoCopy(1, 5, new double[] { 1.5, 6.0, 4 }, new VoltTable(new VoltTable.ColumnInfo("foo", VoltType.BIGINT)), new BigDecimal(5), new BigDecimal[] {}, new TimestampType(System.currentTimeMillis()));
        responseJSON = callProcOverJSON("CrazyBlahProc", pset, null, null, false);
        System.out.println(responseJSON);
        response = responseFromJSON(responseJSON);
        assertEquals(ClientResponse.SUCCESS, response.status);
        // check the JSON itself makes sense
        JSONObject jsonObj = new JSONObject(responseJSON);
        JSONArray results = jsonObj.getJSONArray("results");
        assertEquals(4, response.results.length);
        JSONObject table = results.getJSONObject(0);
        JSONArray data = table.getJSONArray("data");
        assertEquals(1, data.length());
        JSONArray row = data.getJSONArray(0);
        assertEquals(1, row.length());
        long value = row.getLong(0);
        assertEquals(1, value);
        // try to pass a string as a date
        java.sql.Timestamp ts = new java.sql.Timestamp(System.currentTimeMillis());
        ts.setNanos(123456000);
        pset = ParameterSet.fromArrayNoCopy(1, 5, new double[] { 1.5, 6.0, 4 }, new VoltTable(new VoltTable.ColumnInfo("foo", VoltType.BIGINT)), new BigDecimal(5), new BigDecimal[] {}, ts.toString());
        responseJSON = callProcOverJSON("CrazyBlahProc", pset, null, null, false);
        System.out.println(responseJSON);
        response = responseFromJSON(responseJSON);
        assertEquals(ClientResponse.SUCCESS, response.status);
        response.results[3].advanceRow();
        System.out.println(response.results[3].getTimestampAsTimestamp(0).getTime());
        assertEquals(123456, response.results[3].getTimestampAsTimestamp(0).getTime() % 1000000);
        // now try a null short value sent as a int  (param expects short)
        pset = ParameterSet.fromArrayNoCopy(1, VoltType.NULL_SMALLINT, new double[] { 1.5, 6.0, 4 }, new VoltTable(new VoltTable.ColumnInfo("foo", VoltType.BIGINT)), new BigDecimal(5), new BigDecimal[] {}, new TimestampType(System.currentTimeMillis()));
        responseJSON = callProcOverJSON("CrazyBlahProc", pset, null, null, false);
        System.out.println(responseJSON);
        response = responseFromJSON(responseJSON);
        assertFalse(response.status == ClientResponse.SUCCESS);
        // now try an out of range long value (param expects short)
        pset = ParameterSet.fromArrayNoCopy(1, Long.MAX_VALUE - 100, new double[] { 1.5, 6.0, 4 }, new VoltTable(new VoltTable.ColumnInfo("foo", VoltType.BIGINT)), new BigDecimal(5), new BigDecimal[] {}, new TimestampType(System.currentTimeMillis()));
        responseJSON = callProcOverJSON("CrazyBlahProc", pset, null, null, false);
        System.out.println(responseJSON);
        response = responseFromJSON(responseJSON);
        assertFalse(response.status == ClientResponse.SUCCESS);
        // now try bigdecimal with small value
        pset = ParameterSet.fromArrayNoCopy(1, 4, new double[] { 1.5, 6.0, 4 }, new VoltTable(new VoltTable.ColumnInfo("foo", VoltType.BIGINT)), 5, new BigDecimal[] {}, new TimestampType(System.currentTimeMillis()));
        responseJSON = callProcOverJSON("CrazyBlahProc", pset, null, null, false);
        System.out.println(responseJSON);
        response = responseFromJSON(responseJSON);
        System.out.println(response.statusString);
        assertEquals(ClientResponse.SUCCESS, response.status);
        // now try null
        pset = ParameterSet.fromArrayNoCopy(1, 4, new double[] { 1.5, 6.0, 4 }, new VoltTable(new VoltTable.ColumnInfo("foo", VoltType.BIGINT)), 5, new BigDecimal[] {}, null);
        responseJSON = callProcOverJSON("CrazyBlahProc", pset, null, null, false);
        System.out.println(responseJSON);
        response = responseFromJSON(responseJSON);
        System.out.println(response.statusString);
        assertEquals(ClientResponse.SUCCESS, response.status);
        // now try jsonp
        responseJSON = callProcOverJSONRaw("Procedure=@Statistics&Parameters=[TABLE]&jsonp=fooBar", 200);
        System.out.println(responseJSON);
        assertTrue(responseJSON.startsWith("fooBar("));
        // now try adhoc
        pset = ParameterSet.fromArrayNoCopy("select * from blah");
        responseJSON = callProcOverJSON("@AdHoc", pset, null, null, false);
        System.out.println(responseJSON);
        response = responseFromJSON(responseJSON);
        System.out.println(response.statusString);
        assertEquals(ClientResponse.SUCCESS, response.status);
        // now try adhoc insert with a huge bigint
        pset = ParameterSet.fromArrayNoCopy("insert into blah values (974599638818488300, NULL, NULL, NULL, NULL);");
        responseJSON = callProcOverJSON("@AdHoc", pset, null, null, false);
        System.out.println(responseJSON);
        response = responseFromJSON(responseJSON);
        System.out.println(response.statusString);
        assertEquals(ClientResponse.SUCCESS, response.status);
        pset = ParameterSet.fromArrayNoCopy("select * from blah where ival = 974599638818488300;");
        responseJSON = callProcOverJSON("@AdHoc", pset, null, null, false);
        System.out.println(responseJSON);
        response = responseFromJSON(responseJSON);
        System.out.println(response.statusString);
        assertEquals(ClientResponse.SUCCESS, response.status);
        assertEquals(1, response.results.length);
        assertEquals(1, response.results[0].getRowCount());
        // Call @AdHoc with zero parameters
        pset = ParameterSet.emptyParameterSet();
        responseJSON = callProcOverJSON("@AdHoc", pset, null, null, false);
        assertTrue(responseJSON.contains("Adhoc system procedure requires at least the query parameter."));
        // Call @AdHoc with many parameters (more than 2)
        pset = ParameterSet.fromArrayNoCopy("select * from blah", "foo", "bar");
        responseJSON = callProcOverJSON("@AdHoc", pset, null, null, false);
        System.err.println(responseJSON);
        assertTrue(responseJSON.contains("Too many actual arguments were passed for the parameters in the sql " + "statement(s): (2 vs. 0)"));
    } finally {
        if (server != null) {
            server.shutdown();
            server.join();
        }
        server = null;
    }
}
Also used : Configuration(org.voltdb.VoltDB.Configuration) JSONArray(org.json_voltpatches.JSONArray) BigDecimal(java.math.BigDecimal) ClientResponse(org.voltdb.client.ClientResponse) HttpResponse(org.apache.http.HttpResponse) JSONObject(org.json_voltpatches.JSONObject) VoltProjectBuilder(org.voltdb.compiler.VoltProjectBuilder) Configuration(org.voltdb.VoltDB.Configuration) TimestampType(org.voltdb.types.TimestampType) File(java.io.File)

Example 34 with JSONObject

use of org.json_voltpatches.JSONObject in project voltdb by VoltDB.

the class TestJSONInterface method responseFromJSON.

public static Response responseFromJSON(String jsonStr) throws JSONException, IOException {
    Response response = new Response();
    JSONObject jsonObj = new JSONObject(jsonStr);
    JSONArray resultsJson = jsonObj.getJSONArray("results");
    response.results = new VoltTable[resultsJson.length()];
    for (int i = 0; i < response.results.length; i++) {
        JSONObject tableJson = resultsJson.getJSONObject(i);
        response.results[i] = VoltTable.fromJSONObject(tableJson);
    }
    if (jsonObj.isNull("status") == false) {
        response.status = (byte) jsonObj.getInt("status");
    }
    if (jsonObj.isNull("appstatus") == false) {
        response.appStatus = (byte) jsonObj.getInt("appstatus");
    }
    if (jsonObj.isNull("statusstring") == false) {
        response.statusString = jsonObj.getString("statusstring");
    }
    if (jsonObj.isNull("appstatusstring") == false) {
        response.appStatusString = jsonObj.getString("appstatusstring");
    }
    if (jsonObj.isNull("exception") == false) {
        response.exception = jsonObj.getString("exception");
    }
    return response;
}
Also used : ClientResponse(org.voltdb.client.ClientResponse) HttpResponse(org.apache.http.HttpResponse) JSONObject(org.json_voltpatches.JSONObject) JSONArray(org.json_voltpatches.JSONArray)

Example 35 with JSONObject

use of org.json_voltpatches.JSONObject in project voltdb by VoltDB.

the class AdhocDDLTestBase method isDRedTable.

protected boolean isDRedTable(String table) throws Exception {
    VoltTable tableinfo = m_client.callProcedure("@SystemCatalog", "TABLES").getResults()[0];
    for (int i = 0; i < tableinfo.m_rowCount; i++) {
        tableinfo.advanceToRow(i);
        String tablename = (String) tableinfo.get(2, VoltType.STRING);
        if (tablename.equals(table)) {
            try {
                String remarks = (String) tableinfo.get(4, VoltType.STRING);
                if (remarks == null) {
                    return false;
                }
                JSONObject jsEntry = new JSONObject(remarks);
                return Boolean.valueOf(jsEntry.getString(JdbcDatabaseMetaDataGenerator.JSON_DRED_TABLE));
            } catch (JSONException e) {
                return false;
            }
        }
    }
    return false;
}
Also used : JSONObject(org.json_voltpatches.JSONObject) JSONException(org.json_voltpatches.JSONException)

Aggregations

JSONObject (org.json_voltpatches.JSONObject)123 JSONException (org.json_voltpatches.JSONException)45 JSONArray (org.json_voltpatches.JSONArray)30 IOException (java.io.IOException)20 KeeperException (org.apache.zookeeper_voltpatches.KeeperException)18 HashMap (java.util.HashMap)17 File (java.io.File)14 Map (java.util.Map)14 ByteBuffer (java.nio.ByteBuffer)13 ExecutionException (java.util.concurrent.ExecutionException)12 ZooKeeper (org.apache.zookeeper_voltpatches.ZooKeeper)12 Test (org.junit.Test)11 ArrayList (java.util.ArrayList)8 TreeSet (java.util.TreeSet)6 JSONStringer (org.json_voltpatches.JSONStringer)6 HashSet (java.util.HashSet)5 BinaryPayloadMessage (org.voltcore.messaging.BinaryPayloadMessage)5 ImmutableList (com.google_voltpatches.common.collect.ImmutableList)4 ImmutableMap (com.google_voltpatches.common.collect.ImmutableMap)4 InetAddress (java.net.InetAddress)4