PageRenderTime 52ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/custom_processing/infinit.e.hadoop.template/src/testing/InfiniteHadoopTestUtils.java

https://github.com/IKANOW/Infinit.e
Java | 140 lines | 97 code | 20 blank | 23 comment | 15 complexity | 2b7247b9cbe0eca24f5259f7c6f42900 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /*******************************************************************************
  2. * Copyright 2012 The Infinit.e Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. ******************************************************************************/
  16. package testing;
  17. import java.io.BufferedReader;
  18. import java.io.File;
  19. import java.io.FileReader;
  20. import java.io.IOException;
  21. import java.util.List;
  22. import javax.xml.parsers.DocumentBuilder;
  23. import javax.xml.parsers.DocumentBuilderFactory;
  24. import org.apache.hadoop.conf.Configuration;
  25. import org.apache.hadoop.util.Tool;
  26. import org.apache.hadoop.util.ToolRunner;
  27. import org.w3c.dom.Document;
  28. import org.w3c.dom.Element;
  29. import org.w3c.dom.Node;
  30. import org.w3c.dom.NodeList;
  31. import com.google.gson.GsonBuilder;
  32. import com.ikanow.infinit.e.data_model.api.BaseApiPojo;
  33. import com.ikanow.infinit.e.data_model.api.knowledge.DocumentPojoApiMap;
  34. import com.ikanow.infinit.e.data_model.store.MongoDbConnection;
  35. import com.ikanow.infinit.e.data_model.store.document.DocumentPojo;
  36. import com.mongodb.MongoException;
  37. public class InfiniteHadoopTestUtils {
  38. //////////////////////////////////////////////////////////////////////////////////////////////////
  39. // Run Hadoop inside Eclipse
  40. public static int runStandalone(Tool runner, String configPath, String [] args) throws Exception
  41. {
  42. Configuration config = new Configuration();
  43. config.set("mapred.job.tracker", "local");
  44. config.set("fs.default.name", "local");
  45. // Now read in via XML
  46. File fXmlFile = new File(configPath);
  47. DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
  48. DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
  49. Document doc = dBuilder.parse(fXmlFile);
  50. NodeList nList = doc.getElementsByTagName("property");
  51. for (int temp = 0; temp < nList.getLength(); temp++) {
  52. Node nNode = nList.item(temp);
  53. if (nNode.getNodeType() == Node.ELEMENT_NODE) {
  54. Element eElement = (Element) nNode;
  55. String name = getTagValue("name", eElement);
  56. String value = getTagValue("value", eElement);
  57. if ((null != name) && (null != value)) {
  58. config.set(name, value);
  59. }
  60. }
  61. }
  62. return ToolRunner.run( config, runner, args );
  63. }
  64. //////////////////////////////////////////////////////////////////////////////////////////////////
  65. // Load document results from the GUI into a (local) mongoDB
  66. public static void loadSampleData(String dataPath, String mongoServer, int mongoPort, boolean resetBeforeLoading) throws MongoException, IOException
  67. {
  68. String json = readFile(dataPath);
  69. MongoDbConnection mongoConnection = new MongoDbConnection(mongoServer, mongoPort);
  70. if (resetBeforeLoading) {
  71. mongoConnection.getMongo().getDB("test").getCollection("docs").drop();
  72. }
  73. QueryResults res = QueryResults.fromApi(json, QueryResults.class);
  74. List<DocumentPojo> documents = res.documents;
  75. if (null == documents) {
  76. documents = res.data; // (maybe it was a direct JSON call not a GUI save file)
  77. }
  78. for (DocumentPojo doc: documents) {
  79. mongoConnection.getMongo().getDB("test").getCollection("docs").insert(doc.toDb());
  80. }
  81. }//TESTED
  82. public static class QueryResults extends BaseApiPojo
  83. {
  84. @Override
  85. public GsonBuilder extendBuilder(GsonBuilder gp) { // Extend the builder to apply standard document deserialization
  86. return new DocumentPojoApiMap().extendBuilder(gp);
  87. }
  88. public List<DocumentPojo> documents; // (from GUI save)
  89. public List<DocumentPojo> data; // (from direct JSON call to rest)
  90. //(and lots of other stuff that will get discarded for now)
  91. }
  92. //////////////////////////////////////////////////////////////////////////////////////////////////
  93. // Utilities
  94. private static String getTagValue(String sTag, Element eElement) {
  95. NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
  96. Node nValue = (Node) nlList.item(0);
  97. if (null != nValue) {
  98. return nValue.getNodeValue();
  99. }
  100. else {
  101. return null;
  102. }
  103. }
  104. private static String readFile( String file ) throws IOException {
  105. StringBuilder stringBuilder = new StringBuilder();
  106. BufferedReader reader = new BufferedReader( new FileReader (file));
  107. try {
  108. String line = null;
  109. String ls = System.getProperty("line.separator");
  110. while( ( line = reader.readLine() ) != null ) {
  111. stringBuilder.append( line );
  112. stringBuilder.append( ls );
  113. }
  114. }
  115. finally {
  116. reader.close();
  117. }
  118. return stringBuilder.toString();
  119. }
  120. }