/// <exception cref="System.Exception"/> public virtual void TestDataDrivenDBInputFormat() { JobContext jobContext = Org.Mockito.Mockito.Mock <JobContext>(); Configuration configuration = new Configuration(); configuration.SetInt(MRJobConfig.NumMaps, 1); Org.Mockito.Mockito.When(jobContext.GetConfiguration()).ThenReturn(configuration); DataDrivenDBInputFormat <DBInputFormat.NullDBWritable> format = new DataDrivenDBInputFormat <DBInputFormat.NullDBWritable>(); IList <InputSplit> splits = format.GetSplits(jobContext); NUnit.Framework.Assert.AreEqual(1, splits.Count); DataDrivenDBInputFormat.DataDrivenDBInputSplit split = (DataDrivenDBInputFormat.DataDrivenDBInputSplit )splits[0]; NUnit.Framework.Assert.AreEqual("1=1", split.GetLowerClause()); NUnit.Framework.Assert.AreEqual("1=1", split.GetUpperClause()); // 2 configuration.SetInt(MRJobConfig.NumMaps, 2); DataDrivenDBInputFormat.SetBoundingQuery(configuration, "query"); NUnit.Framework.Assert.AreEqual("query", configuration.Get(DBConfiguration.InputBoundingQuery )); Job job = Org.Mockito.Mockito.Mock <Job>(); Org.Mockito.Mockito.When(job.GetConfiguration()).ThenReturn(configuration); DataDrivenDBInputFormat.SetInput(job, typeof(DBInputFormat.NullDBWritable), "query" , "Bounding Query"); NUnit.Framework.Assert.AreEqual("Bounding Query", configuration.Get(DBConfiguration .InputBoundingQuery)); }
/// <exception cref="System.IO.IOException"/> private void AssertSplits(string[] expectedSplitRE, IList <InputSplit> splits) { NUnit.Framework.Assert.AreEqual(expectedSplitRE.Length, splits.Count); for (int i = 0; i < expectedSplitRE.Length; i++) { DataDrivenDBInputFormat.DataDrivenDBInputSplit split = (DataDrivenDBInputFormat.DataDrivenDBInputSplit )splits[i]; string actualExpr = split.GetLowerClause() + " " + split.GetUpperClause(); NUnit.Framework.Assert.IsTrue("Split #" + (i + 1) + " expression is wrong." + " Expected " + expectedSplitRE[i] + " Actual " + actualExpr, Sharpen.Pattern.Matches(expectedSplitRE [i], actualExpr)); } }
/// <summary> /// Returns the query for selecting the records, /// subclasses can override this for custom behaviour. /// </summary> protected internal override string GetSelectQuery() { StringBuilder query = new StringBuilder(); DataDrivenDBInputFormat.DataDrivenDBInputSplit dataSplit = (DataDrivenDBInputFormat.DataDrivenDBInputSplit )GetSplit(); DBConfiguration dbConf = GetDBConf(); string[] fieldNames = GetFieldNames(); string tableName = GetTableName(); string conditions = GetConditions(); // Build the WHERE clauses associated with the data split first. // We need them in both branches of this function. StringBuilder conditionClauses = new StringBuilder(); conditionClauses.Append("( ").Append(dataSplit.GetLowerClause()); conditionClauses.Append(" ) AND ( ").Append(dataSplit.GetUpperClause()); conditionClauses.Append(" )"); if (dbConf.GetInputQuery() == null) { // We need to generate the entire query. query.Append("SELECT "); for (int i = 0; i < fieldNames.Length; i++) { query.Append(fieldNames[i]); if (i != fieldNames.Length - 1) { query.Append(", "); } } query.Append(" FROM ").Append(tableName); if (!dbProductName.StartsWith("ORACLE")) { // Seems to be necessary for hsqldb? Oracle explicitly does *not* // use this clause. query.Append(" AS ").Append(tableName); } query.Append(" WHERE "); if (conditions != null && conditions.Length > 0) { // Put the user's conditions first. query.Append("( ").Append(conditions).Append(" ) AND "); } // Now append the conditions associated with our split. query.Append(conditionClauses.ToString()); } else { // User provided the query. We replace the special token with our WHERE clause. string inputQuery = dbConf.GetInputQuery(); if (inputQuery.IndexOf(DataDrivenDBInputFormat.SubstituteToken) == -1) { Log.Error("Could not find the clause substitution token " + DataDrivenDBInputFormat .SubstituteToken + " in the query: [" + inputQuery + "]. Parallel splits may not work correctly." ); } query.Append(inputQuery.Replace(DataDrivenDBInputFormat.SubstituteToken, conditionClauses .ToString())); } Log.Debug("Using query: " + query.ToString()); return(query.ToString()); }