做了几天工程,对HBase中的表操作熟悉了一下。下面总结一下常用的表操作和容易出错的几个方面。当然主要来源于大牛们的文章。我在前人的基础上稍作解释。
1.连接HBase中的表testtable,用户名:root,密码:root
public void ConnectHBaseTable() { Configuration conf = new Configuration(); conf.set("hadoop.job.ugi", "root,root"); HBaseConfiguration config = new HBaseConfiguration(); try { table = new HTable(config, "testtable"); }catch(Exception e){e.printStackTrace();} }
2.根据行名name获得一行数据,存入Result.注意HBase中的表数据是字节存储的。
下面的例子表示获得行名为name的行的famA列族col1列的数据。
String rowId = "name"; Get get = new Get(rowId); Result result = hTable.get(get); byte[] value = result.getValue(famA, col1); System.out.println(Bytes.toString(value));
3.向表中存数据
下面的例子表示写入一行。行名为abcd,famA列族col1列的数据为"hello world!"。
byte[] rowId = Bytes.toBytes("abcd"); byte[] famA = Bytes.toBytes("famA"); byte[] col1 = Bytes.toBytes("col1"); Put put = new Put(rowId). add(famA, col1, Bytes.toBytes("hello world!")); hTable.put(put);
4.扫描的用法(scan):便于获得自己需要的数据,相当于SQL查询。
byte[] famA = Bytes.toBytes("famA"); byte[] col1 = Bytes.toBytes("col1"); HTable hTable = new HTable("test");
//表示要查询的行名是从a开始,到z结束。 Scan scan = new Scan(Bytes.toBytes("a"), Bytes.toBytes("z"));
//用scan.setStartRow(Bytes.toBytes(""));设置起始行
//用scan.setStopRow(Bytes.toBytes(""));设置终止行
//表示查询famA族col1列
scan.addColumn(famA, col1);
//注意,下面是filter的写法。相当于SQL的where子句
//表示famA族col1列的数据等于"hello world!" SingleColumnValueFilter singleColumnValueFilterA = new SingleColumnValueFilter( famA, col1, CompareOp.EQUAL, Bytes.toBytes("hello world!")); singleColumnValueFilterA.setFilterIfMissing(true);
//表示famA族col1列的数据等于"hello hbase!" SingleColumnValueFilter singleColumnValueFilterB = new SingleColumnValueFilter( famA, col1, CompareOp.EQUAL, Bytes.toBytes("hello hbase!")); singleColumnValueFilterB.setFilterIfMissing(true);
//表示famA族col1列的数据是两者中的一个 FilterList filter = new FilterList(Operator.MUST_PASS_ONE, Arrays .asList((Filter) singleColumnValueFilterA, singleColumnValueFilterB)); scan.setFilter(filter); ResultScanner scanner = hTable.getScanner(scan); //遍历每个数据 for (Result result : scanner) { System.out.println(Bytes.toString(result.getValue(famA, col1))); }
5.上面的代码容易出错的地方在于,需要导入HBase的类所在的包。导入时需要选择包,由于类可能出现在HBase的各个子包中,所以要选择好,下面列出常用的包。尽量用HBase的包
import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.HBaseConfiguration;import org.apache.hadoop.hbase.client.Get;import org.apache.hadoop.hbase.client.HTable;import org.apache.hadoop.hbase.client.Put;import org.apache.hadoop.hbase.client.Result;import org.apache.hadoop.hbase.client.ResultScanner;import org.apache.hadoop.hbase.client.Scan;import org.apache.hadoop.hbase.filter.Filter;import org.apache.hadoop.hbase.filter.FilterList;import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;import org.apache.hadoop.hbase.filter.FilterList.Operator;import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;import java.text.SimpleDateFormat;import java.util.Arrays;import java.util.Date;
6.下面列出HBase常用的操作
(1)时间戳到时间的转换.单一的时间戳无法给出直观的解释。
public String GetTimeByStamp(String timestamp) {
long datatime= Long.parseLong(timestamp); Date date=new Date(datatime); SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:MM:ss"); String timeresult=format.format(date); System.out.println("Time : "+timeresult); return timeresult; }
(2)时间到时间戳的转换。注意时间是字符串格式。字符串与时间的相互转换,此不赘述。
public String GetStampByTime(String time) { String Stamp=""; SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date; try { date=sdf.parse(time); Stamp=date.getTime()+"000"; System.out.println(Stamp); }catch(Exception e){e.printStackTrace();} return Stamp; }
上面就是我的一点心得。以后碰到什么问题,再来解决。
参考文献:http://www.nearinfinity.com/blogs/aaron_mccurry/using_hbase-dsl.html