Map reduce- Count of words starting with the letter a-z - mapreduce

I would like to get the count of number of words that start with letter "a-z" for which 'am trying this code but its just printing the count of number of words starting with "z".
public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
String line = value.toString();
StringTokenizer itr = new StringTokenizer(line);
while( itr.hasMoreTokens()){
String token= itr.nextToken();
if(token.startsWith("a-z")){
word.set("A_Count");
//word.set("Z_Count");
context.write(word, ONE);
}
}
}

I have tried this out and it runs fine.
public class Map extends Mapper<LongWritable, Text, Text, IntWritable> {
private final IntWritable VALUE = new IntWritable(1);
private final Text KEY = new Text("COUNT");
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line = value.toString();
StringTokenizer tokenizer = new StringTokenizer(line);
while (tokenizer.hasMoreTokens()) {
String token = tokenizer.nextToken();
if(token.startsWith("a-z")){
context.write(KEY, VALUE);
}
}
}
My reducer looks like
public class Reduce extends Reducer<Text, IntWritable, IntWritable, NullWritable> {
public void reduce(Text key, Iterable<IntWritable> values, Context context)
throws IOException, InterruptedException {
int sum = 0;
for (IntWritable val : values) {
sum += val.get();
}
context.write(new IntWritable(sum),NullWritable.get());
}
}

Related

Which regex to validate JavaFX Textfield for decimal number in scientific notation

Using as a starting point the DecimalField class found on this site, I wrote the following:
import javafx.scene.control.TextField;
import java.util.regex.Pattern;
public class DecimalField extends TextField {
public Boolean rate, positive, integer;
Pattern decimalPattern;
DecimalField(Boolean rate, Boolean positive) {
this.rate = rate;
this.positive = positive;
decimalPattern = Pattern.compile ("[-+]?[0-9]*(\\.[0-9]*)?");
// decimalPattern = Pattern.compile("[-+]?(\\b[0-9]+(\\.[0-9]*)?|\\.[0-9]+)([eE][-+]?[0-9]+\\b)?");
if (rate) {
decimalPattern = Pattern.compile ("[-+]?[0-9]*(\\.[0-9]*)?[%]?");
} else if (positive) {
decimalPattern = Pattern.compile ("[1-9][0-9]*(\\.[0-9]*)?");
}
}
#Override
public void replaceText(int start, int end, String text) {
if (validate (start, text)) {
super.replaceText (start, end, text);
}
}
#Override
public void replaceSelection(String text) {
if (validate (Integer.MAX_VALUE, text)) {
super.replaceSelection (text);
}
}
private boolean validate(int start, String text) {
String currentText = (getText ().isEmpty ()) ? "" : getText ();
if (start == 0) { //to handle "-1.1" or ".1" cases
return decimalPattern.matcher (text + currentText).matches ();
} else {
return decimalPattern.matcher (currentText + text).matches ();
}
}
}
Depending on the parameters sent to the constructor, this class can be used to restrict entries to a standard decimal number, to a positive only (i.e. > 0) decimal number, or to a number followed by the per-cent character.
It seems to work fine (a small test application is provided), but I wanted to also be able to specify a number in scientific notation such as 25.56e-5
I could not write the right regex pattern. A pattern such as "[0-9.eE+-]*" would limit the entry to acceptable characters but would not enforce the number syntax! Suggestions are welcome.
Here is the test program:
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class DecimalFieldTest extends Application {
#Override
public void start(Stage primaryStage) {
// Boolean rate;
// rate = false; positive = true;
Label basicLbl = new Label("Basic Decimal");
DecimalField decimalField = new DecimalField (false, false);
Label rateLbl = new Label("Rate Decimal");
DecimalField rateDecimalField = new DecimalField (true, false);
Label positiveLbl = new Label("Positive Decimal");
DecimalField positiveDecimalField = new DecimalField (false, true);
Button clickMe = new Button ("Click Me");
clickMe.setOnAction (new EventHandler<ActionEvent> () {
#Override
public void handle(ActionEvent event) {
String s;
s = decimalField.getText ();
if (!s.isEmpty ()) getOut(s, false);
s = rateDecimalField.getText ();
if (!s.isEmpty ()) getOut(s, true);
s = positiveDecimalField.getText ();
if (!s.isEmpty ()) getOut(s, false);
}
});
decimalField.setOnAction (new EventHandler<ActionEvent> () {
#Override
public void handle(ActionEvent event) {
getOut(decimalField.getText (),false);
}
});
rateDecimalField.setOnAction (new EventHandler<ActionEvent> () {
#Override
public void handle(ActionEvent event) {
getOut(rateDecimalField.getText (),true);
}
});
positiveDecimalField.setOnAction (new EventHandler<ActionEvent> () {
#Override
public void handle(ActionEvent event) {
getOut(positiveDecimalField.getText (),false);
}
});
VBox root = new VBox (5, basicLbl, decimalField,
rateLbl, rateDecimalField, positiveLbl, positiveDecimalField, clickMe);
root.setAlignment (Pos.CENTER);
Scene scene = new Scene (root, 300, 250);
primaryStage.setScene (scene);
primaryStage.show ();
}
void getOut(String s, Boolean rate) {
// for rate : textField.getText().replaceAll("%","")
String ss = s.replaceAll ("%", "");
double value = Double.parseDouble (ss);
if (rate) {
System.out.println (String.format (ss + " <-> " + value) + "%");
} else {
System.out.println (String.format (ss + " <-> " + value));
}
}
public static void main(String[] args) {
launch (args);
}
}
You can use NumberTextField with BigDecimal and different number formats.
package control;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.geometry.Pos;
import javafx.scene.control.TextField;
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.text.ParseException;
/**
* Number text field
*
* <p>
* Original source code from:
* https://dzone.com/articles/javafx-numbertextfield-and
*/
public class NumberTextField extends TextField {
private final NumberFormat numberFormat;
private ObjectProperty<BigDecimal> value = new SimpleObjectProperty<>();
public NumberTextField(NumberFormat numberFormat) {
this(BigDecimal.ZERO, numberFormat, 100);
}
public NumberTextField(NumberFormat numberFormat, double width) {
this(BigDecimal.ZERO, numberFormat, width);
}
/**
* Number field with properties.
*
* #param value decimal value
* #param numberFormat number format
* #param width min, max and pref width
*/
public NumberTextField(BigDecimal value, NumberFormat numberFormat, double width) {
super();
this.numberFormat = numberFormat;
setMinWidth(width);
setMaxWidth(width);
setPrefWidth(width);
initHandlers();
setValue(value);
setAlignment(Pos.BOTTOM_RIGHT);
}
public final BigDecimal getValue() {
return value.get();
}
public final void setValue(BigDecimal value) {
this.value.set(value);
}
public ObjectProperty<BigDecimal> valueProperty() {
return value;
}
private void initHandlers() {
// try to parse when focus is lost or RETURN is hit
setOnAction((ActionEvent arg0) -> {
parseAndFormatInput();
});
focusedProperty().addListener((ObservableValue<? extends Boolean> observable,
Boolean oldValue, Boolean newValue) -> {
if (!newValue) {
parseAndFormatInput();
}
});
// Set text in field if BigDecimal property is changed from outside.
valueProperty().addListener((ObservableValue<? extends BigDecimal> observable,
BigDecimal oldValue, BigDecimal newValue) -> {
setText(numberFormat.format(newValue));
});
}
/**
* Tries to parse the user input to a number according to the provided
* NumberFormat
*/
private void parseAndFormatInput() {
try {
String input = getText();
if (input == null || input.length() == 0) {
return;
}
Number parsedNumber = numberFormat.parse(input);
BigDecimal newValue = new BigDecimal(parsedNumber.toString());
setValue(newValue);
selectAll();
} catch (ParseException ex) {
// If parsing fails keep old number
setText(numberFormat.format(value.get()));
}
}
}
And as BigDecimalTextField:
NumberTextField bigDecimalField =
new NumberTextField(new DecimalFormat("#,###,###,##0.00"));

Sharing data between master and reduce

I need to perform aggregation using the results form all the reduce tasks. Basically the reduce task finds the sum and count and a value. I need to add all the sums and counts and find the final average.
I tried using conf.setInt in reduce. But when I try to access it from the main function it fails
class Main {
public static class MyReducer
extends Reducer<Text, Text,Text,IntWritable> {
public void reduce(Text key, Iterable<Text> values,
Context context
) throws IOException, InterruptedException {
int i = 0;
int fd = 0, fc = 0;
fd = context.getConfiguration().getInt("fd", -1);
fc = context.getConfiguration().getInt("fc", -1);
//when I check the value of fd, fc here they are fine. fc fd is shared across all reduce tasks and the updated value is seen by all reduce task. Only main function doesnt seem to have access to it.
}
}
public static void main(String[] args) throws Exception{
Configuration conf = new Configuration();
conf.setInt("fc", 5);
Job job = new Job(conf, "Flight Data");
job.setJarByClass(FlightData.class);
job.setMapperClass(TokenizerMapper.class);
job.setReducerClass(MyReducer.class);
job.setPartitionerClass(FirstPartitioner.class);
job.setGroupingComparatorClass(GroupComparator.class);
job.setSortComparatorClass(KeyComparator.class);
job.setNumReduceTasks(10);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
flightCount = job.getConfiguration().getInt("fc", -1);
flightDelay = job.getConfiguration().getInt("fd", -1);
//here when I access fc, fd, I get back 5 & 5
System.out.println("Final " + flightCount +" " + flightDelay+ " " + flightDelay/flightCount);
}
Override the run() of the mapper and reducer using the new org.apache.hadoop.mapreduce API. In these methods you can emit the accumulated sum/count from each mapper or reducer.
Also you would need to limit the reducer count by 1 so as to get a global sum of all the sums generated by multiple mappers.
See the below code for more clarity:
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
public class AggregationExample extends Configured implements Tool {
/**
* This is Mapper.
*
*/
public static class MapJob extends Mapper<LongWritable, Text, Text, Text> {
private Text outputKey = new Text();
private Text outputValue = new Text();
private double sum;
#Override
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
try {
// say that you need to sum up the value part
sum+= Double.valueOf(value);
}
#Override
public void run(Context context) throws IOException, InterruptedException {
setup(context);
while (context.nextKeyValue()) {
map(context.getCurrentKey(), context.getCurrentValue(), context);
}
// emit out the sum per mapper
outputKey.set(sum);
context.write(outputKey, outputValue);// Notice that the outputValue is empty
cleanup(context);
}
}
/**
* This is Reducer.
*
*/
public static class ReduceJob extends Reducer<Text, Text, Text, Text> {
private Text outputKey = new Text();
private Text outputValue = new Text();
private double sum;
#Override
protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException,
InterruptedException {
// summation of values from each mapper
sum += Double.valueOf(key.toString());
}
#Override
public void run(Context context) throws IOException, InterruptedException {
setup(context);
while (context.nextKey()) {
reduce(context.getCurrentKey(), context.getValues(), context);
}
// emit out the global sums
outputKey.set(sum);
context.write(outputKey, outputValue);
cleanup(context);
}
}
#Override
public int run(String[] args) throws Exception {
try {
Configuration conf = getConf();
// output key and value separator is empty as in final output only
// key is emitted and value is empty
conf.set("mapred.textoutputformat.separator", "");
// Configuring mapred to have just one reducer as we need to find
// single sum values from all the inputs
conf.setInt("mapred.tasktracker.reduce.tasks.maximum", 1);
conf.setInt("mapred.reduce.tasks", 1);
Job job = new Job(conf);
job.setJarByClass(AggregationExample.class);
job.setJobName("Aggregation Example");
job.setMapperClass(MapJob.class);
job.setReducerClass(ReduceJob.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class);
FileInputFormat.setInputPaths(job, args[0]);
FileOutputFormat.setOutputPath(job, new Path(args[1]));
boolean success = job.waitForCompletion(true);
return success ? 0 : 1;
} catch (Exception e) {
e.printStackTrace();
return 1;
}
}
public static void main(String[] args) throws Exception {
if (args.length < 2) {
System.out
.println("Usage: AggregationExample <comma sparated list of input directories> <output dir>");
System.exit(-1);
}
int result = ToolRunner.run(new AggregationExample(), args);
System.exit(result);
}
}
You may very well map this approach to your problem.
Found the solution. I used counters
http://diveintodata.org/2011/03/15/an-example-of-hadoop-mapreduce-counter/
public class FlightData {
//enum for counters used by reducers
public static enum FlightCounters {
FLIGHT_COUNT,
FLIGHT_DELAY;
}
public static class MyReducer
extends Reducer<Text, Text,Text,IntWritable> {
public void reduce(Text key, Iterable<Text> values,
Context context
) throws IOException, InterruptedException {
delay1 = Float.parseFloat(origin[5]);
delay2 = Float.parseFloat(dest[5]);
context.getCounter(FlightCounters.FLIGHT_COUNT).increment(1);
context.getCounter(FlightCounters.FLIGHT_DELAY)
.increment((long) (delay1 + delay2));
}
}
public static void main(String[] args) throws Exception{
float flightCount, flightDelay;
job.waitForCompletion(true);
//get the final results updated in counter by all map and reduce tasks
flightCount = job.getCounters()
.findCounter(FlightCounters.FLIGHT_COUNT).getValue();
flightDelay = job.getCounters()
.findCounter(FlightCounters.FLIGHT_DELAY).getValue();
}
}

how can add list to a fragment of pageindicator?

i want to add list view to one page of pageindicator the code oof page indicator is :
public class VpiAbsTestActivity extends SherlockFragmentActivity {
private static final String[] CONTENT = new String[] { "This", "Is", "A", "ViewPager", "Demo" };
TestFragmentAdapter mAdapter;
ViewPager mPager;
PageIndicator mIndicator;
public boolean onCreateOptionsMenu(Menu menu) {
SubMenu subMenu1 = menu.addSubMenu("Action Item");
subMenu1.add(0,15, 0, "Sample");
subMenu1.add(0,20, 0,"Menu");
subMenu1.add("Items");
MenuItem subMenu1Item = subMenu1.getItem();
subMenu1Item.setIcon(R.drawable.ic_title_share_default);
subMenu1Item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
SubMenu subMenu2 = menu.addSubMenu("Overflow Item");
subMenu2.add("These");
subMenu2.add("Are");
subMenu2.add("Sample");
subMenu2.add("Items");
MenuItem subMenu2Item = subMenu2.getItem();
subMenu2Item.setIcon(R.drawable.ic_compose);
subMenu2Item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
return super.onCreateOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case 10:
Toast.makeText(VpiAbsTestActivity.this, "Now "+item.getItemId(), Toast.LENGTH_SHORT).show();
return true;
case 15:
Toast.makeText(VpiAbsTestActivity.this, "Now = "+item.getItemId(), Toast.LENGTH_SHORT).show();
return true;
case 20:
Toast.makeText(VpiAbsTestActivity.this, "Now == "+item.getItemId(), Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_tabs);
mAdapter = new TestFragmentAdapter(getSupportFragmentManager());
mPager = (ViewPager)findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
mIndicator = (TabPageIndicator)findViewById(R.id.indicator);
mIndicator.setViewPager(mPager);
}
class TestFragmentAdapter extends FragmentPagerAdapter {
private int mCount = CONTENT.length;
public TestFragmentAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return TestFragment.newInstance(String.valueOf(position));
}
#Override
public int getCount() {
return mCount;
}
#Override
public CharSequence getPageTitle(int position) {
return CONTENT[position];
}
}
}
i want to add a list view to page one for example this list but i cant write a list fragment for this code please tel me what i do
public static final String[] list = new String[]{"France", "London", "Sweden"};
what can I do ?
First of all you have to understand what is a Fragment and how to instantiate it and how to send to it data (you should look the guide dedicated to Fragments and the documentation for Fragments here).
Then you have to do 2 things :
Create your custom list fragment, I suggest you to look at the android documentation of ListFragment and create a class that inherits from ListFragment.
public class CustomListFragment extends ListFragment {
// your code here to add content
}
Modify your TestFragmentAdapter to instanciate your new CustomListFragment when you are in the first page (see example below)
public class TestFragmentAdapter
extends FragmentPagerAdapter
{
protected final Class[] CONTENT_CLASSES = new Class[] { CustomListFragment.class, TestFragment.class};
protected final String[] CONTENT_TITLES = new String[] { "List", "Test"};
public TabAdapter( FragmentManager manager, Activity context )
{
super( manager );
this.context = context;
}
#Override
public Fragment getItem( int i )
{
Class targetFragmentClass = CONTENT_CLASSES[i];
Bundle args = new Bundle();
// add your args here
Fragment targetFragment = Fragment.instantiate( context, targetFragmentClass.getName(), args );
return targetFragment;
}
#Override
public int getCount()
{
return CONTENT_CLASSES.length;
}
#Override
public CharSequence getPageTitle( int position )
{
return CONTENT_TITLES[i];
}
}

How to create a list dynamically from a class name (which is passed as argument) and return the list in c#.net

How to create a list dynamically from a class name (which is passed as an argument) and return the list in C#.
Below code may not work. I have posted it to give an idea:
public T ConvertDataSetToList<T>(DataSet _ds, String tableName, string className)
{
Type classType=Type.GetType(className);
List<T> newList = new List<T>();
//System.Activator.CreateInstance(Type.GetType(className));
try
{
Details _Details;
for (int iRowCount = 0; iRowCount < _ds.Tables[tableName].Rows.Count; iRowCount++)
{
_Details = FillDTO(_ds.Tables[tableName].Rows[iRowCount]);
newList.Add(_msDetails);
}
}
catch (Exception ex) { }
return newList;
}
You can dot it with some basic mapping stuff.
//Maps a dataset
public List<T> MapDataSet<T>(DataSet anyDataset
, string tablename) where T : new()
{
return MapDataTable<T>(anyDataset.Tables[tablename]);
}
// Maps a datatable
public List<T> MapDataTable<T>(DataTable table) where T : new()
{
List<T> result = new List<T>();
foreach(DataRow row in table.Rows)
{
result.Add(MapDataRow<T>(row));
}
return result;
}
// maps a DataRow to an arbitrary class (rudimentary)
public T MapDataRow<T>(DataRow row) where T: new()
{
// we map columns to class properties
Type destinationType = typeof(T);
// create our new class
T mappedTo = new T();
// iterate over the columns
for(int columnIndex=0;columnIndex<row.ItemArray.Length;columnIndex++)
{
// get a matching property of our class
PropertyInfo fieldTo = destinationType.GetProperty(
row.Table.Columns[columnIndex].ColumnName );
if (fieldTo !=null)
{
// map our fieldvalue to our property
fieldTo.SetValue(mappedTo, row[columnIndex], new object[] {});
}
else
{
// sorry, field doens't match any property on class
}
}
return mappedTo;
}
Here is basic test app to demonstrate its usage
void Main()
{
DataTable dt = new DataTable("hello");
dt.Columns.Add("foo");
dt.Columns.Add("bar");
dt.Columns.Add("foobar");
DataRow row = dt.NewRow();
row[0]="blah1";
row[1] ="two";
row[2] = "fb1";
dt.Rows.Add(row);
row = dt.NewRow();
row[0]="apples";
row[1] ="pears";
row[2] = "duh";
dt.Rows.Add(row);
List<DTO> list = MapDataTable<DTO>(dt);
List<SecondDTO> list2 = MapDataTable<SecondDTO>(dt);
}
// sample DTO object
public class DTO
{
public string foo { get; set;}
public string bar { get; set; }
}
// another sample DTO object
public class SecondDTO
{
public string foo { get; set;}
public string foobar { get; set; }
}

How to set value of list<String> in DynamicJasper

I need to generate dynamic count of columns in me report. So I set to my JasperPrint the massive of Object:
Object[] obj = new Object[selectedUsers.size()];
//fill the massive
JasperPrint jp = DynamicJasperHelper.generateJasperPrint(dr, new ClassicLayoutManager(), new JRBeanArrayDataSource(obj));
My obj is a class:
public class ResultsDTO {
private String login;
private Integer id;
private List<String> list;
private Object[] results;
public Object[] getResults() {
return results;
}
public void setResults(Object[] results) {
this.results = results;
}
public ResultsDTO(){
}
public ResultsDTO(Integer id,String login) {
super();
this.login = login;
this.id = id;
}
public ResultsDTO(String login, Integer id, List<String> list) {
super();
this.login = login;
this.id = id;
this.list = list;
}
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
public void addToList(String day_result){
this.list.add(day_result);
}
}
and then I try to create columns:
FastReportBuilder firstReport = new FastReportBuilder();
List<AbstractColumn> column_list = new ArrayList<AbstractColumn>();
AbstractColumn columnId = getColumn("id", Integer.class,"№", 30, headerStyle, detailStyle);
AbstractColumn columnLogin = getColumn("login", String.class,"ФИО", 150, headerStyle, detailStyle);
for (int i = 0; i < header.size(); i++){
AbstractColumn column = getColumn("results", Object.class, header.get(i), 80, headerStyle, detailStyle);
column_list.add(column);
}
Eventually I have an exception:
net.sf.jasperreports.engine.design.JRValidationException: Report
design not valid :
1. Class "java.lang.Object" not supported for text field expression.
Please, help! I don' know how to use jasper and list or array
Jasper Reports does not allow Object as a valid type for its elements. I has to be one of the following:
String
Number (or any subclass of it)
Date
Boolean
You should ask each element in the form for its class and pass proper class to the column builder.