HBase-1.0读写数据

1,Hbase Api读取数据

public class WordCount

{
public static void main(String[] args) throws IOException
{
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
Connection connect = ConnectionFactory.createConnection(conf);
Table table = connect.getTable(TableName.valueOf("test"));
List<String> rstlist = new ArrayList<String>();

Date time = new Date();
long t1=time.getTime();
Scan s = new Scan();
s.setFilter(new FirstKeyOnlyFilter());
ResultScanner scanner = table.getScanner(s);

for(Result r: scanner)
{
NavigableMap<byte[], NavigableMap<byte[], byte[]>> returnMap = r.getNoVersionMap();
NavigableMap<byte[], byte[]> te = returnMap.get((returnMap).firstKey());
System.out.println(new String(te.get(te.firstKey())));
}
time = new Date();
long t2=time.getTime();
System.out.print((t2-t1)+" ms");
for(int i =0;i<rstlist.size();i++)
{
System.out.print(rstlist.get(i));
}

System.out.print(rstlist.size());

}

2,使用分页过滤器进行过滤的式例代码:

Filter filter = new PageFilter(15);

int totalRow = 0;

byte[] lastRow = null;

while(true){

Scan scan = new Scan();

scan.setFilter(filter);

 if(lastRow (!=null){

byte[] startRow = Bytes.add(lastRow,POSTIFX);

System.out.println("start row:" + Bytes.toStringBinary(startRow));

scan.setStartRow(startRow);

}

ResultScanner scanner = table.getScanner(scan);

int localRows = 0;

Result result;

while((result = scanner.next())!=null){

System.out.println(localRows++ + ":" + result);

totalRows++;

localRows++:

lastRow = result.getRow();

}

scanner.close();

if(localRows ==0)

break;

}

System.out.println("total rows: "+totalRows);

3,Hbase作为输出源式例:

public class Main extends Configured implements Tool{

static final Log log = LogFactory.getLog(Main.class);

@Override

public int run(String[] args) throws Exception{

if(args!=3){

Log.info("Usage: 3 paremeters needed!");

System.exit(1);

}

String input = args[0];

String table = args[1];

String columns = args[2];

Configuration conf = HBaseConfiguration.create();

conf.set("hbase.master","");

conf.set(hbase.zookeeper.quorum",>>);

Job job = new Job(conf,"");

job.setJarByClass(Main.class);

job.setMapperClass(ExampleMapper.class);

job.setOutputFormatClass(TableOutputFormat.calss);

job.getConfiguration().set(TableOutputFormat.OUTPUT_TABLE,table);

job.setOutputKeyClass(ImmutableBytesWritable.class);

job.setOutputValueClass(Writbale.class);

job.setNumbReduceTasks(0);


FileInputFormat.addInputPath(job,new Path(input));


return job.waitForCompletion(true) ? 0:1);

}

public static void main(String[] args)throws IOException{

Configuration conf = new Configuration();

String[] otherArgs = new GenericoptionParser(conf,args).getRemainingArgs();

in tres = 1;

try{

res = ToolRunner.run(conf,new Main(),otherArgs);

}

catch(Exception e){

e.printStackTrace();

}

System.exit(res);

}

}


public class ExampleMapper extends Mapper<LongWritable, Text, ImmutableBytesWritable,Writable>{

private byte[] family = null;

private byte[] qualifier = null;

private byte[] val = null;

private String rowkey = null;

private long ts =System.currentTimeMillis();

@Override

protected void map(LongWritb=able key, Text value,Context context)throws IOException, InterruptedException{

try{

String line = value.toString();

String[] arr = line.split("\t",-1);


if(arr.length ==2)

{

rowkey = arr[0];

String[] vales = arr[1].split("\t",-1);

if(vales.length == 4){

family = vales[0].getBytes();

qualifier = vales[1].getBytes();

val = vales[2].getBytes();

ts = Long.parseLong(vales[3]);


Put put = new Put(rowkey.getBytes(),ts);

put.add(family, qualifier,val);

context.write(new ImmutableBytesWritable(rowkey.getBytes()),put);

}

}

}catch(Exception e)

{

e.printStackTrace();

}

}

}

4,获取scan扫描相关参数


public class MainRowkeyScan {


public static void main(String[] args) throws IOException{
Configuration conf =  HBaseConfiguration.create();
conf.set("hbase.rootdir","hdfs://192.168..1.121/hbase");

Connection connect = ConnectionFactory.createConnection(conf);
/*Admin admin = connect.getAdmin();
HTableDescriptor Hd = admin.getTableDescriptor(TableName.valueOf("fa"));*/


admin.createTable(tableDesc,splitKeys);

Put put = new Put("eefffa".getBytes());
         put.addColumn("f1".getBytes(),"col".getBytes(), "value".getBytes());
         Table t = connect.getTable(TableName.valueOf("Mine"));
t.put(put);*/
try {

Scan scan = new Scan();

scan.setStartRow("MMC8000GPSANDASYN051113-20124-00000000".getBytes());
scan.setStopRow("MMC8000GPSANDASYN051113-40124-00000000".getBytes());

scan.setScanMetricsEnabled(true);
Table table = connect.getTable(TableName.valueOf("TS"));

ResultScanner result = table.getScanner(scan);

for(Result rt : result){
System.out.println(new String(rt.getRow()));
}
/*String SCAN_ATTRIBUTES_METRICS_ENABLE = "scan.attributes.metrics.enable";
scan.setAttribute(SCAN_ATTRIBUTES_METRICS_ENABLE, Bytes.toBytes(Boolean.TRUE));*/

System.out.println(scan.isScanMetricsEnabled());
ScanMetrics sm = scan.getScanMetrics();
Map<String, Long> te = sm.getMetricsMap();
for(int i=0;i<te.size();i++){

// System.out.println(te.get());
}
System.out.println("RPC "+sm.countOfRPCcalls);
System.out.println("bytes "+sm.countOfBytesInResults);
System.out.println("regions "+sm.countOfRegions);
System.out.println("regions "+sm.countOfRemoteRPCRetries);
System.out.println("regions "+sm.countOfRPCRetries);
System.out.println("regions "+sm.countOfRemoteRPCcalls);
// System.out.println("RPC "+sm.countOfRegions.doubleValue());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值