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);
}
}
WordCountHBase
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章
MapperScheduler.xml
橡皮高
2019-01-19 13:33:31
mapper.xml裏面的sql語句 原
y yan_liu
2018-12-02 21:31:08
MyBatis之Mapper詳解及常用技巧
sun2shadows
2018-09-11 07:49:11
MyBatis搭建項目
sun2shadows
2018-09-11 07:48:52
關於Spring+MyBatis的MapperScannerConfigurer無法加載place-holder屬性問題分析
leshjmail
2018-09-11 03:04:17
Spring4-RowMapper
素顏豬
2018-09-10 04:12:25
通用Mapper接口方法以及說明
CalMeE
2018-08-27 12:42:55
HBase數據壓縮方式的選擇
周小虎_
2018-08-27 16:47:41
Region Splitting 過程
周小虎_
2018-08-27 16:47:40
HBase Region split 策略
周小虎_
2018-08-27 16:47:40
幾個簡單的HBase的Java程序(待續)
KevinWen777
2018-08-27 11:47:29
hive與hbase的十大區別與聯繫
KevinWen777
2018-08-27 11:47:24
HBASE coprocessor 的分析
brave_panda
2018-08-26 18:35:14
Region Server意外退出之後…
magicdreaming
2018-08-26 17:36:09
hbase的replication使用
magicdreaming
2018-08-26 17:36:03