hbase连接java(转载)
来源:互联网 发布:西安淘宝模特兼职 编辑:程序博客网 时间:2024/06/06 12:49
1、需要的jar包:
commons-codec-1.4.jar
commons-logging-1.0.4.jar
hadoop-0.20.2-core.jar
hbase-0.20.6.jar
log4j-1.2.15.jar
zookeeper-3.2.2.jar
2、已有表结构:
1、表名:scores
2、列族:
course:art
course:math
grade:
3、scan 'scores'的内容:
ROW COLUMN+CELL
Jerry column=course:art, timestamp=1301294630194, value=80
Jerry column=course:math, timestamp=1301294630132, value=100
Jerry column=grade:, timestamp=1301294630073, value=2
Jim column=course:art, timestamp=1301294630363, value=97
Jim column=course:math, timestamp=1301294630305, value=100
Jim column=grade:, timestamp=1301294630247, value=3
Tom column=course:art, timestamp=1301294630015, value=97
Tom column=course:math, timestamp=1301294629987, value=87
Tom column=grade:, timestamp=1301294629931, value=1
packageorg.myhbase;
importjava.io.IOException;
importjava.util.ArrayList;
importjava.util.List;
importorg.apache.hadoop.conf.Configuration;
importorg.apache.hadoop.hbase.HBaseConfiguration;
importorg.apache.hadoop.hbase.KeyValue;
importorg.apache.hadoop.hbase.client.Get;
importorg.apache.hadoop.hbase.client.HTable;
importorg.apache.hadoop.hbase.client.Result;
importorg.apache.hadoop.hbase.client.ResultScanner;
importorg.apache.hadoop.hbase.client.Scan;
importorg.apache.hadoop.hbase.filter.FilterList;
importorg.apache.hadoop.hbase.filter.SingleColumnValueFilter;
importorg.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
importorg.apache.hadoop.hbase.io.Cell;
importorg.apache.hadoop.hbase.util.Bytes;
public class HBaseBasic03{
private staticHBaseConfiguration hbaseConfig=null;
static{
Configuration config=newConfiguration();
config.set("hbase.zookeeper.quorum","192.168.10.149,192.168.10.44,192.168.10.49");
config.set("hbase.zookeeper.property.clientPort","2181");
hbaseConfig=newHBaseConfiguration(config);
}
public static voidselectByRowKey(String tablename,String rowKey) throwsIOException{
HTable table=newHTable(hbaseConfig,tablename);
Get g = newGet(Bytes.toBytes(rowKey));
Resultr=table.get(g);
for(KeyValuekv:r.raw()){
System.out.println("column:"+new String(kv.getColumn()));
System.out.println("value:"+new String(kv.getValue()));
}
}
public static voidselectByRowKeyColumn(String tablename,String rowKey,String column)throws IOException{
HTable table=newHTable(hbaseConfig,tablename);
Get g = newGet(Bytes.toBytes(rowKey));
g.addColumn(Bytes.toBytes(column));
Resultr=table.get(g);
for(KeyValuekv:r.raw()){
System.out.println("column:"+new String(kv.getColumn()));
System.out.println("value:"+new String(kv.getValue()));
}
}
public static voidselectByFilter(Stringtablename,List<String> arr) throwsIOException{
HTable table=newHTable(hbaseConfig,tablename);
FilterList filterList = newFilterList();
Scan s1 = newScan();
for(String v:arr){ //各个条件之间是“与”的关系
String []s=v.split(",");
filterList.addFilter(newSingleColumnValueFilter(Bytes.toBytes(s[0]),
Bytes.toBytes(s[1]),
CompareOp.EQUAL,Bytes.toBytes(s[2])
)
);
//添加下面这一行后,则只返回指定的cell,同一行中的其他cell不返回
//s1.addColumn(Bytes.toBytes(s[0]),Bytes.toBytes(s[1]));
}
s1.setFilter(filterList);
ResultScannerResultScannerFilterList = table.getScanner(s1);
for(Resultrr=ResultScannerFilterList.next();rr!=null;rr=ResultScannerFilterList.next()){
for(KeyValuekv:rr.list()){
System.out.println("row :"+new String(kv.getRow()));
System.out.println("column :"+new String(kv.getColumn()));
System.out.println("value :"+new String(kv.getValue()));
}
}
}
public static void main(String[] args) throws IOException{
//按rowkey查询,查询Tom行的所有cell
HBaseBasic03.selectByRowKey("scores","Tom");
// 按rokey 和 column来查询,查询Tom行course列族的所有列值
HBaseBasic03.selectByRowKeyColumn("scores","Tom","course");
// Filter多条件查询,条件:查询course列族中art列值为97 ,且 course列族中math列值为100的行
List<String>arr=newArrayList<String>();
arr.add("course,art,97");
arr.add("course,math,100");
HBaseBasic03.selectByFilter("scores",arr);
}
}
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
famA, col1, CompareOp.EQUAL, Bytes.toBytes("hello world!"));
singleColumnValueFilterA
//表示famA族col1列的数据等于"hello hbase!"
SingleColumnValueFilter singleColumnValueFilterB
famA, col1, CompareOp.EQUAL, Bytes.toBytes("hello hbase!"));
singleColumnValueFilterB
//表示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的包
importorg.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;
importorg.apache.hadoop.hbase.filter.SingleColumnValueFilter;
importorg.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(Stringtimestamp)
{
long datatime=Long.parseLong(timestamp);
Date date=new Date(datatime);
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-ddHH:MM:ss");
String timeresult=format.format(date);
System.out.println("Time : "+timeresult);
return timeresult;
}
(2)时间到时间戳的转换。注意时间是字符串格式。字符串与时间的相互转换,此不赘述。
public String GetStampByTime(Stringtime)
{
String Stamp="";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-ddHH:mm:ss");
Date date;
try
{
date=sdf.parse(time);
Stamp=date.getTime()+"000";
System.out.println(Stamp);
}catch(Exception e){e.printStackTrace();}
return Stamp;
}
- hbase连接java(转载)
- hbase连接java(转载)
- Java连接HBASE数据库
- Java连接HBASE数据库
- Kerberos+Java连接Hbase
- java 连接hbase
- java连接hbase优化
- java连接Hbase数据库
- java 连接HBase客户端
- maven java连接hbase
- java连接hbase
- hbase java client无法连接 hbase server
- java通过api连接hbase
- Java连接HBase超时解决方法
- 【转载】java连接MYSQL
- Hbase 新api连接java实现
- JAVA安全客户端连接到Hbase
- Java 连接hbase 进行简单操作
- CDH4简介
- Hbase几种数据入库方式比较
- 注解
- VS2010 操作Word(三)表格操作
- HBASE SHELL基本命令
- hbase连接java(转载)
- HBase 0.94版本新特性、性能优化详…
- Facebook's New Realtime Analytic…
- hbase很有价值的读写性能提升(转载…
- HBase vs Cassandra-装载
- HBase二级索引与Join
- Bulk Loading - Hbase
- HBase加载大数据
- bulk-load装载hdfs数据到hbase