public static String toJsonString(final List<XAttr> xAttrs) throws IOException { final List<String> names = Lists.newArrayListWithCapacity(xAttrs.size()); for (XAttr xAttr : xAttrs) { names.add(XAttrHelper.getPrefixName(xAttr)); } String ret = JSON.toString(names); final Map<String, Object> finalMap = new TreeMap<String, Object>(); finalMap.put("XAttrNames", ret); return JSON.toString(finalMap); }
/** Convert a HdfsFileStatus object to a Json string. */ public static String toJsonString(final HdfsFileStatus status, boolean includeType) { if (status == null) { return null; } final Map<String, Object> m = new TreeMap<String, Object>(); m.put("pathSuffix", status.getLocalName()); m.put("type", PathType.valueOf(status)); if (status.isSymlink()) { m.put("symlink", status.getSymlink()); } m.put("length", status.getLen()); m.put("owner", status.getOwner()); m.put("group", status.getGroup()); FsPermission perm = status.getPermission(); m.put("permission", toString(perm)); if (perm.getAclBit()) { m.put("aclBit", true); } if (perm.getEncryptedBit()) { m.put("encBit", true); } m.put("accessTime", status.getAccessTime()); m.put("modificationTime", status.getModificationTime()); m.put("blockSize", status.getBlockSize()); m.put("replication", status.getReplication()); m.put("fileId", status.getFileId()); m.put("childrenNum", status.getChildrenNum()); m.put("storagePolicy", status.getStoragePolicy()); return includeType ? toJsonString(FileStatus.class, m) : JSON.toString(m); }
public void testTagsMetricsPair() throws IOException { TagsMetricsPair pair = new TagsMetricsPair(outputRecord.getTagsCopy(), outputRecord.getMetricsCopy()); String s = JSON.toString(pair); assertEquals( "[{\"testTag1\":\"testTagValue1\",\"testTag2\":\"testTagValue2\"}," + "{\"testMetric1\":1,\"testMetric2\":33}]", s); }
public static List<String> toXAttrNames(final Map<?, ?> json) throws IOException { if (json == null) { return null; } final String namesInJson = (String) json.get("XAttrNames"); final Object[] xattrs = (Object[]) JSON.parse(namesInJson); final List<String> names = Lists.newArrayListWithCapacity(json.keySet().size()); for (int i = 0; i < xattrs.length; i++) { names.add((String) (xattrs[i])); } return names; }
/** Convert a AclStatus object to a Json string. */ public static String toJsonString(final AclStatus status) { if (status == null) { return null; } final Map<String, Object> m = new TreeMap<String, Object>(); m.put("owner", status.getOwner()); m.put("group", status.getGroup()); m.put("stickyBit", status.isStickyBit()); m.put("entries", status.getEntries()); final Map<String, Map<String, Object>> finalMap = new TreeMap<String, Map<String, Object>>(); finalMap.put(AclStatus.class.getSimpleName(), m); return JSON.toString(finalMap); }
protected void setMessages(List<Message> messages) { try { for (Message msg : messages) { msg.put(Bayeux.CLIENT_FIELD, _clientId); } String json = JSON.toString(messages); if (_formEncoded) setRequestContent(new ByteArrayBuffer("message=" + URLEncoder.encode(json, "utf-8"))); else setRequestContent(new ByteArrayBuffer(json, "utf-8")); } catch (Exception e) { Log.warn(e); } }
@Test public void testGetTest2() throws Exception { MuleClient client = muleContext.getClient(); MuleMessage result = client.request("http://127.0.0.1:8081/test2", 1000); assertThat(result, is(notNullValue())); Object parsedJson = JSON.parse(result.getPayloadAsString()); assertTrue(parsedJson instanceof Object[]); Object[] array = (Object[]) parsedJson; assertEquals(1, array.length); assertTrue(array[0] instanceof Map); @SuppressWarnings("unchecked") Map<String, Object> map = (Map<String, Object>) array[0]; assertEquals("hello2", map.get("var1")); assertEquals("goodbye2", map.get("var2")); }
/** Convert a HdfsFileStatus object to a Json string. */ public static String toJsonString(final HdfsFileStatus status, boolean includeType) { if (status == null) { return null; } final Map<String, Object> m = new TreeMap<String, Object>(); m.put("localName", status.getLocalName()); m.put("isDir", status.isDir()); m.put("len", status.getLen()); m.put("owner", status.getOwner()); m.put("group", status.getGroup()); m.put("permission", toString(status.getPermission())); m.put("accessTime", status.getAccessTime()); m.put("modificationTime", status.getModificationTime()); m.put("blockSize", status.getBlockSize()); m.put("replication", status.getReplication()); return includeType ? toJsonString(HdfsFileStatus.class, m) : JSON.toString(m); }
@Override // JournalNodeMXBean public String getJournalsStatus() { // jid:{Formatted:True/False} Map<String, Map<String, String>> status = new HashMap<String, Map<String, String>>(); synchronized (this) { for (Map.Entry<String, Journal> entry : journalsById.entrySet()) { Map<String, String> jMap = new HashMap<String, String>(); jMap.put("Formatted", Boolean.toString(entry.getValue().isFormatted())); status.put(entry.getKey(), jMap); } } // It is possible that some journals have been formatted before, while the // corresponding journals are not in journalsById yet (because of restarting // JN, e.g.). For simplicity, let's just assume a journal is formatted if // there is a directory for it. We can also call analyzeStorage method for // these directories if necessary. // Also note that we do not need to check localDir here since // validateAndCreateJournalDir has been called before we register the // MXBean. File[] journalDirs = localDir.listFiles( new FileFilter() { @Override public boolean accept(File file) { return file.isDirectory(); } }); for (File journalDir : journalDirs) { String jid = journalDir.getName(); if (!status.containsKey(jid)) { Map<String, String> jMap = new HashMap<String, String>(); jMap.put("Formatted", "true"); status.put(jid, jMap); } } return JSON.toString(status); }
String toJson() { return JSON.toString(this); }
/** Convert a key-value pair to a Json string. */ public static String toJsonString(final String key, final Object value) { final Map<String, Object> m = new TreeMap<String, Object>(); m.put(key, value); return JSON.toString(m); }
@SuppressWarnings({"unchecked", "deprecation"}) @Test public void testNameNodeMXBeanInfo() throws Exception { Configuration conf = new Configuration(); MiniDFSCluster cluster = null; try { cluster = new MiniDFSCluster.Builder(conf).build(); cluster.waitActive(); FSNamesystem fsn = cluster.getNameNode().namesystem; MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); ObjectName mxbeanName = new ObjectName("Hadoop:service=NameNode,name=NameNodeInfo"); // get attribute "ClusterId" String clusterId = (String) mbs.getAttribute(mxbeanName, "ClusterId"); assertEquals(fsn.getClusterId(), clusterId); // get attribute "BlockPoolId" String blockpoolId = (String) mbs.getAttribute(mxbeanName, "BlockPoolId"); assertEquals(fsn.getBlockPoolId(), blockpoolId); // get attribute "Version" String version = (String) mbs.getAttribute(mxbeanName, "Version"); assertEquals(fsn.getVersion(), version); assertTrue(version.equals(VersionInfo.getVersion() + ", r" + VersionInfo.getRevision())); // get attribute "Used" Long used = (Long) mbs.getAttribute(mxbeanName, "Used"); assertEquals(fsn.getUsed(), used.longValue()); // get attribute "Total" Long total = (Long) mbs.getAttribute(mxbeanName, "Total"); assertEquals(fsn.getTotal(), total.longValue()); // get attribute "safemode" String safemode = (String) mbs.getAttribute(mxbeanName, "Safemode"); assertEquals(fsn.getSafemode(), safemode); // get attribute nondfs Long nondfs = (Long) (mbs.getAttribute(mxbeanName, "NonDfsUsedSpace")); assertEquals(fsn.getNonDfsUsedSpace(), nondfs.longValue()); // get attribute percentremaining Float percentremaining = (Float) (mbs.getAttribute(mxbeanName, "PercentRemaining")); assertEquals(fsn.getPercentRemaining(), percentremaining.floatValue(), DELTA); // get attribute Totalblocks Long totalblocks = (Long) (mbs.getAttribute(mxbeanName, "TotalBlocks")); assertEquals(fsn.getTotalBlocks(), totalblocks.longValue()); // get attribute alivenodeinfo String alivenodeinfo = (String) (mbs.getAttribute(mxbeanName, "LiveNodes")); Map<String, Map<String, Object>> liveNodes = (Map<String, Map<String, Object>>) JSON.parse(alivenodeinfo); assertTrue(liveNodes.size() > 0); for (Map<String, Object> liveNode : liveNodes.values()) { assertTrue(liveNode.containsKey("nonDfsUsedSpace")); assertTrue(((Long) liveNode.get("nonDfsUsedSpace")) > 0); assertTrue(liveNode.containsKey("capacity")); assertTrue(((Long) liveNode.get("capacity")) > 0); assertTrue(liveNode.containsKey("numBlocks")); assertTrue(((Long) liveNode.get("numBlocks")) == 0); } assertEquals(fsn.getLiveNodes(), alivenodeinfo); // get attribute deadnodeinfo String deadnodeinfo = (String) (mbs.getAttribute(mxbeanName, "DeadNodes")); assertEquals(fsn.getDeadNodes(), deadnodeinfo); // get attribute NameDirStatuses String nameDirStatuses = (String) (mbs.getAttribute(mxbeanName, "NameDirStatuses")); assertEquals(fsn.getNameDirStatuses(), nameDirStatuses); Map<String, Map<String, String>> statusMap = (Map<String, Map<String, String>>) JSON.parse(nameDirStatuses); Collection<URI> nameDirUris = cluster.getNameDirs(0); for (URI nameDirUri : nameDirUris) { File nameDir = new File(nameDirUri); System.out.println("Checking for the presence of " + nameDir + " in active name dirs."); assertTrue(statusMap.get("active").containsKey(nameDir.getAbsolutePath())); } assertEquals(2, statusMap.get("active").size()); assertEquals(0, statusMap.get("failed").size()); // This will cause the first dir to fail. File failedNameDir = new File(nameDirUris.toArray(new URI[0])[0]); assertEquals(0, FileUtil.chmod(new File(failedNameDir, "current").getAbsolutePath(), "000")); cluster.getNameNodeRpc().rollEditLog(); nameDirStatuses = (String) (mbs.getAttribute(mxbeanName, "NameDirStatuses")); statusMap = (Map<String, Map<String, String>>) JSON.parse(nameDirStatuses); for (URI nameDirUri : nameDirUris) { File nameDir = new File(nameDirUri); String expectedStatus = nameDir.equals(failedNameDir) ? "failed" : "active"; System.out.println( "Checking for the presence of " + nameDir + " in " + expectedStatus + " name dirs."); assertTrue(statusMap.get(expectedStatus).containsKey(nameDir.getAbsolutePath())); } assertEquals(1, statusMap.get("active").size()); assertEquals(1, statusMap.get("failed").size()); } finally { if (cluster != null) { for (URI dir : cluster.getNameDirs(0)) { FileUtil.chmod(new File(new File(dir), "current").getAbsolutePath(), "755"); } cluster.shutdown(); } } }
public static String toJsonString(final List<XAttr> xAttrs, final XAttrCodec encoding) throws IOException { final Map<String, Object> finalMap = new TreeMap<String, Object>(); finalMap.put("XAttrs", toJsonArray(xAttrs, encoding)); return JSON.toString(finalMap); }