sneceesay77 commented on a change in pull request #179: GORA-532: Apache Gora 
Benchmark initial pull request for review and comments
URL: https://github.com/apache/gora/pull/179#discussion_r315736122
 
 

 ##########
 File path: 
gora-benchmark/src/main/java/org/apache/gora/benchmark/GoraBenchmarkUtils.java
 ##########
 @@ -0,0 +1,428 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.gora.benchmark;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.io.StringWriter;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.Iterator;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Properties;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+import org.apache.avro.Schema;
+import org.apache.avro.SchemaBuilder;
+import org.apache.avro.SchemaBuilder.FieldAssembler;
+import org.apache.gora.compiler.GoraCompiler;
+import org.apache.gora.compiler.utils.LicenseHeaders;
+import org.json.JSONArray;
+import org.json.JSONObject;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.w3c.dom.Attr;
+import org.w3c.dom.Comment;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+
+import com.yahoo.ycsb.ByteIterator;
+
+/**
+ * The Class GoraBenchmarkUtils has some utilities that dynamically generate 
files needed to for gora.
+ * It generate the following files. 
+ * a. Database Mapping File
+ * b. Avro Files
+ * c. Data Beans
+ */
+public class GoraBenchmarkUtils {
+  /** The Constant AVRO_PATH. */
+  private static final String AVRO_PATH = "src/main/avro";
+  /** The Constant AVRO_FILE. */
+  private static final String AVRO_FILE = "user.json";
+  /** The field prefix. */
+  private static String FIELD_PREFIX = "field";
+  /** The Constant USER_ID_VALUE. */
+  private static final String USER_ID_VALUE = "userId";
+  /** The Constant AVRO_FULL_PATH. */
+  private static final String AVRO_FULL_PATH = AVRO_PATH + "/" + AVRO_FILE;
+  /** The Constant NULL. */
+  private static final String NULL = "null";
+  /** The Constant RECORD. */
+  private static final String RECORD = "User";
+  /** The Constant NAMESPACE_VALUE. */
+  private static final String NAMESPACE_VALUE = 
"org.apache.gora.benchmark.generated";
+  /** The Constant NAMESPACE_KEY. */
+  private static final String NAMESPACE_KEY = "namespace";
+  /** The Constant NAME_KEY. */
+  private static final String NAME_KEY = "name";
+  /** The Constant MONGODB. */
+  private static final String MONGODB = "mongodb";
+  /** The Constant COUCHDB. */
+  private static final String COUCHDB = "couchdb";
+  /** The Constant HBASE. */
+  private static final String HBASE = "hbase";
+  /** The Constant KEYCLASS. */
+  private static final String KEYCLASS = "java.lang.String";
+  /** The db mapping path. */
+  private static String DB_MAPPING_PATH = "src/main/resources";
+  /** The Constant MONGO_MAPPING_FILE. */
+  private static final String MONGO_MAPPING_FILE = "gora-mongodb-mapping.xml";
+  /** The Constant HBASE_MAPPING_FILE. */
+  private static final String HBASE_MAPPING_FILE = "gora-hbase-mapping.xml";
+  /** The Constant COUCHDB_MAPPING_FILE. */
+  private static final String COUCHDB_MAPPING_FILE = 
"gora-couchdb-mapping.xml";
+  /** The Constant BEAN_DESTINATION_DIR. */
+  private static final File BEAN_DESTINATION_DIR = new File("src/main/java/");
+  /** The Constant DEFAULT_DATA_STORE_KEY. */
+  private static final String DEFAULT_DATA_STORE_KEY = 
"gora.datastore.default";
+  private static final String GORA_ROOT_ELEMENT = "gora-otd";
+  private static final Logger LOG = 
LoggerFactory.getLogger(GoraBenchmarkUtils.class);
+
+  /**
+   * Checks if is field updatable.
+   *
+   * @param field
+   *          the field
+   * @param values
+   *          the values
+   * 
+   * @return true, if is field updatable
+   */
+  public static boolean isFieldUpdatable(String field, Map<String, 
ByteIterator> values) {
+    if (values.get(field) == null) {
+      return false;
+    }
+    return true;
+  }
+
+  /**
+   * Generate avro schema based on the number of fields. supplied when running
+   * the benchmark. These files are json files
+   *
+   * @param numberOfFields
+   *          the number of fields
+   */
+  public void generateAvroSchema(int numberOfFields) {
+    try {
+      File avroFile = new File(AVRO_FULL_PATH);
+      FieldAssembler<Schema> fieldAssembler = 
SchemaBuilder.record(RECORD).namespace(NAMESPACE_VALUE).fields();
+      
fieldAssembler.name(USER_ID_VALUE).type().stringType().stringDefault(NULL);
+      for (int i = 0; i < numberOfFields; i++) {
+        fieldAssembler.name(FIELD_PREFIX + 
i).type().stringType().stringDefault(NULL);
+      }
+      Schema sc = fieldAssembler.endRecord();
+      String schemaString = sc.toString();
+      JSONObject avroSchema = new JSONObject(schemaString);
+      OutputStreamWriter avroWriter = new OutputStreamWriter(new 
FileOutputStream(avroFile), StandardCharsets.UTF_8);
+      avroWriter.write(avroSchema.toString(4));
+      avroWriter.close();
+    } catch (FileNotFoundException e) {
+      LOG.info(e.getMessage(), e);
+    } catch (IOException e) {
+      LOG.info(e.getMessage(), e);
+    }
+  }
+
+  /**
+   * Generate database mapping file. Each database has its own mapping syntax.
+   * These files are xml files
+   *
+   * @param dbName
+   *          the db name
+   */
+  @SuppressWarnings("unchecked")
+  public void generateMappingFile(String dbName) {
 
 Review comment:
   I will have a look at this issue after GSoC since this is the final week and 
there is a lot to do. 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to