HBase程式設計 實做I/O操作

来源:互联网 发布:saas源码 java 编辑:程序博客网 时间:2024/06/05 06:39

HBase程式設計 實做I/O操作

 

新增Table

public static void createHBaseTable ( String tablename ) throws IOException
{
HTableDescriptor htd = new HTableDescriptor(tablename);
HColumnDescriptor col = new HColumnDescriptor("content:");
htd.addFamily(col);
HBaseConfiguration config = new HBaseConfiguration();
HBaseAdmin admin = new HBaseAdmin(config);
if(admin.tableExists(tablename))
{
admin.disableTable(tablename);
admin.deleteTable(tablename);
}
admin.createTable(htd);
}

 

Put 資料進Column

25
範例二:Put資料進Column
static public void putData(String tablename, String row, String family,
String column, String value) throws IOException {
HBaseConfiguration config = new HBaseConfiguration();
HTable table = new HTable(config, tablename);
byte[] brow = Bytes.toBytes(row);
byte[] bfamily = Bytes.toBytes(family);
byte[] bcolumn = Bytes.toBytes(column);
byte[] bvalue = Bytes.toBytes(value);
Put p = new Put(brow);
p.add(bfamily, bcolumn, bvalue);
table.put(p);
table.close();
}

 

Get Column Value

 

27
範例三:Get Column Value
static String getColumn ( String tablename, String row, String family,
String column ) {
HBaseConfiguration conf = new HBaseConfiguration();
String ret = "";
HTable table;
try {
table = new HTable(conf, Bytes.toBytes(tablename));
Get g = new Get(Bytes.toBytes(row));
Result rowResult = table.get(g);
ret = Bytes.toString(rowResult.getValue(Bytes.toBytes(family + “:”+ column)));
table.close();
} catch (IOException e) {
e.printStackTrace();
}
return ret;
}

 

Scan all Column

static void ScanColumn(String tablename, String family, String column) {
HBaseConfiguration conf = new HBaseConfiguration();
HTable table;
try {
table = new HTable(conf, Bytes.toBytes(tablename));
ResultScanner scanner = table.getScanner(Bytes.toBytes(family));
int i = 1;
for (Result rowResult : scanner) {
byte[] by = rowResult.getValue(
Bytes.toBytes(family), Bytes.toBytes(column) );
String str = Bytes.toString ( by );
System.out.println("row " + i + " is \"" + str +"\"");
i++;
}
} catch (IOException e) {
e.printStackTrace();
}
}

 

 

刪除資料表

 

static void drop ( String tablename ) {
HBaseConfiguration conf = new HBaseConfiguration();
try {
HBaseAdmin admin = new HBaseAdmin(conf);
if (admin.tableExists(tablename))
{
admin.disableTable(tablename);
admin.deleteTable(tablename);
System.out.println("Droped the table [" + tablename+ "]");
}else{
System.out.println("Table [" + tablename+ "] was not found!");
}
} catch (IOException e) {
e.printStackTrace();
}
}

 

 

MapReduce & HBase的搭配

1>WordCountHBase

public class WordCountHBase
{
public static class Map extends
Mapper<LongWritable,Text,Text,
IntWritable>
{
private IntWritable i = new
IntWritable(1);
public void map(LongWritable key,Text
value,Context context) throws
IOException, InterruptedException
{
String s[] =
value.toString().trim().split(" ");
for( String m : s)
{
context.write(new Text(m), i);
}
}
}

 

public static class Reduce extends
TableReducer<Text, IntWritable,
NullWritable>
{
public void reduce(Text key,
Iterable<IntWritable> values, Context
context) throws IOException,
InterruptedException
{
int sum = 0;
for(IntWritable i : values)
{
sum += i.get();
}
Put put = new
Put(Bytes.toBytes(key.toString()));
put.add(Bytes.toBytes("content"),
Bytes.toBytes("count"),
Bytes.toBytes(String.valueOf(sum)));
context.write(NullWritable.get(), put);
}
}

 

public static void createHBaseTable(String
tablename)throws IOException
{
HTableDescriptor htd = new
HTableDescriptor(tablename);
HColumnDescriptor col = new
HColumnDescriptor("content:");
htd.addFamily(col);
HBaseConfiguration config = new
HBaseConfiguration();
HBaseAdmin admin = new
HBaseAdmin(config);
if(admin.tableExists(tablename))
{
admin.disableTable(tablename);
admin.deleteTable(tablename);
}
System.out.println("create new table: " +
tablename);

admin.createTable(htd);
}

 

 

 

public static void main(String args[]) throws Exception
{
String tablename = "wordcount";
Configuration conf = new Configuration();
conf.set(TableOutputFormat.OUTPUT_TABLE,
tablename);
createHBaseTable(tablename);
String input = args[0];
Job job = new Job(conf, "WordCount table with " + input);
job.setJarByClass(WordCountHBase.class);
job.setNumReduceTasks(3);
job.setMapperClass(Map.class);
job.setReducerClass(Reduce.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TableOutputFormat.class);
FileInputFormat.addInputPath(job, new Path(input));
System.exit(job.waitForCompletion(true)?0:1);
}
}

 

 

 

 

原创粉丝点击