WordCountHBase

public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
// TODO Auto-generated method stub
String tablename = "test2";
Configuration conf = new Configuration();
conf.set(TableOutputFormat.OUTPUT_TABLE, tablename);
createHBaseTable(tablename);
Job job = new Job(conf,"WordCount table");
job.setJarByClass(WordCountHBase.class);
//job.setNumReduceTasks(3);
job.setMapperClass(Map.class);
job.setReducerClass(Reduce.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class);
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TableOutputFormat.class);
// 設置輸入目錄 
   FileInputFormat.addInputPath(job, new Path("hdfs://hadoop:9000/input")); 
System.exit(job.waitForCompletion(true)?0:1);
}


public static class Map extends Mapper<LongWritable, Text, Text, Text>
{
//輸出到reduce中,user+time , content
private Text user_time = new Text(); 
private Text contents = new Text(); 
@Override
public void map(LongWritable key, Text value, Context context) throws IOException,InterruptedException{
String str = value.toString();
String time=str.substring(0,19);
String content = str.substring(20);
String[] temp=content.split(" ");
String user=temp[0];
user_time.set(user+time);
if(temp.length==2)
contents.set(content.split(" ")[1]);
context.write(user_time, contents);
}






}

public static class Reduce extends TableReducer<Text, Text, NullWritable> {

@Override
public void reduce(Text key, Iterable<Text> value, Context context) throws IOException,InterruptedException{

String s="";
for(Text str:value)
{
s=str.toString();
System.out.println("--------------------------"+s);
}
//put接收一個行鍵
Put put = new Put(Bytes.toBytes(key.toString()));
//列族,列限定符,內容
put.add(Bytes.toBytes("info"),Bytes.toBytes("content"),Bytes.toBytes(s));
context.write(NullWritable.get(), put);
}
}
/**
* create a table
* @param tablename
* @throws IOException   河北-學生-(373776759)2014-02-11 20:02:12
*/
public static void createHBaseTable(String tablename) throws IOException {
HTableDescriptor htd = new HTableDescriptor(tablename);
HColumnDescriptor col = new HColumnDescriptor("info");
htd.addFamily(col);
Configuration cfg = HBaseConfiguration.create();
cfg.addResource("classpath:hbase-site.xml");
HBaseAdmin admin = new HBaseAdmin(cfg);
if(admin.tableExists(tablename)) {
System.out.println("table exists,trying recreate table!");
admin.disableTable(tablename);
admin.deleteTable(tablename);
admin.createTable(htd);
}
else {
System.out.println("create new table:"+tablename);
admin.createTable(htd);
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章