cloudera
https://repository.cloudera.com/artifactory/cloudera-repos/
org.apache.hadoop
hadoop-client
2.6.0-mr1-cdh5.14.0
org.apache.hbase
hbase-client
1.2.0-cdh5.14.0
org.apache.hbase
hbase-server
1.2.0-cdh5.14.0
junit
junit
4.12
test
org.testng
testng
6.14.3
test
org.apache.maven.plugins
maven-compiler-plugin
3.0
1.8
UTF-8
org.apache.maven.plugins
maven-shade-plugin
2.2
package
shade
*:*
META-INF/*.SF
META-INF/*.DSA
META-INF/*/RSA
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.property.clientPort", "2181");
configuration.set("hbase.zookeeper.quorum", "hadoop01,hadoop02,hadoop03");
configuration.set("hbase.master", "hadoop01:60000");
Connection connection = ConnectionFactory.createConnection(configuration);
Admin admin = connection.getAdmin();
//通过HTableDescriptor来实现我们表的参数设置,包括表名,列族等等
HTableDescriptor user = new HTableDescriptor(TableName.valueOf("myuser"));
//添加列族
user.addFamily(new HColumnDescriptor("info"));
user.addFamily(new HColumnDescriptor("data"));
//创建表
if (!admin.tableExists(TableName.valueOf("myuser"))) {
admin.createTable(user);
}
admin.close();
//获取config对象
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "hadoop01:2181,hadoop02:2181,hadoop03:2181");
//获取连接对象
Connection connection = ConnectionFactory.createConnection(configuration);
//获取表
Table myuser = connection.getTable(TableName.valueOf("myuser"));
//创建put对象,并指定rowkey
Put put = new Put("0001".getBytes());
put.addColumn("f1".getBytes(),"id".getBytes(), Bytes.toBytes(1));
put.addColumn("f1".getBytes(),"name".getBytes(), Bytes.toBytes("张三"));
put.addColumn("f1".getBytes(),"age".getBytes(), Bytes.toBytes(18));
put.addColumn("f2".getBytes(),"address".getBytes(), Bytes.toBytes("地球人"));
put.addColumn("f2".getBytes(),"phone".getBytes(), Bytes.toBytes("15874102589"));
//插入数据
myuser.put(put);
//关闭表
myuser.close();
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum","hadoop01,hadoop02,hadoop03");
Connection connection = ConnectionFactory.createConnection(configuration);
Table myuser = connection.getTable(TableName.valueOf("myuser"));
//创建put对象,并指定rowkey
Put put = new Put("0002".getBytes());
put.addColumn("info".getBytes(),"id".getBytes(),Bytes.toBytes(1));
put.addColumn("info".getBytes(),"name".getBytes(),Bytes.toBytes("曹操"));
put.addColumn("info".getBytes(),"age".getBytes(),Bytes.toBytes(30));
put.addColumn("data".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
put.addColumn("data".getBytes(),"address".getBytes(),Bytes.toBytes("沛国谯县"));
put.addColumn("data".getBytes(),"phone".getBytes(),Bytes.toBytes("16888888888"));
put.addColumn("data".getBytes(),"say".getBytes(),Bytes.toBytes("helloworld"));
Put put2 = new Put("0003".getBytes());
put2.addColumn("info".getBytes(),"id".getBytes(),Bytes.toBytes(2));
put2.addColumn("info".getBytes(),"name".getBytes(),Bytes.toBytes("刘备"));
put2.addColumn("info".getBytes(),"age".getBytes(),Bytes.toBytes(32));
put2.addColumn("data".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
put2.addColumn("data".getBytes(),"address".getBytes(),Bytes.toBytes("幽州涿郡涿县"));
put2.addColumn("data".getBytes(),"phone".getBytes(),Bytes.toBytes("17888888888"));
put2.addColumn("data".getBytes(),"say".getBytes(),Bytes.toBytes("talk is cheap , show me the code"));
Put put3 = new Put("0004".getBytes());
put3.addColumn("info".getBytes(),"id".getBytes(),Bytes.toBytes(3));
put3.addColumn("info".getBytes(),"name".getBytes(),Bytes.toBytes("孙权"));
put3.addColumn("info".getBytes(),"age".getBytes(),Bytes.toBytes(35));
put3.addColumn("data".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
put3.addColumn("data".getBytes(),"address".getBytes(),Bytes.toBytes("下邳"));
put3.addColumn("data".getBytes(),"phone".getBytes(),Bytes.toBytes("12888888888"));
put3.addColumn("data".getBytes(),"say".getBytes(),Bytes.toBytes("what are you 弄啥嘞!"));
Put put4 = new Put("0005".getBytes());
put4.addColumn("info".getBytes(),"id".getBytes(),Bytes.toBytes(4));
put4.addColumn("info".getBytes(),"name".getBytes(),Bytes.toBytes("诸葛亮"));
put4.addColumn("info".getBytes(),"age".getBytes(),Bytes.toBytes(28));
put4.addColumn("data".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
put4.addColumn("data".getBytes(),"address".getBytes(),Bytes.toBytes("四川隆中"));
put4.addColumn("data".getBytes(),"phone".getBytes(),Bytes.toBytes("14888888888"));
put4.addColumn("data".getBytes(),"say".getBytes(),Bytes.toBytes("出师表你背了嘛"));
Put put5 = new Put("0005".getBytes());
put5.addColumn("info".getBytes(),"id".getBytes(),Bytes.toBytes(5));
put5.addColumn("info".getBytes(),"name".getBytes(),Bytes.toBytes("司马懿"));
put5.addColumn("info".getBytes(),"age".getBytes(),Bytes.toBytes(27));
put5.addColumn("data".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
put5.addColumn("data".getBytes(),"address".getBytes(),Bytes.toBytes("哪里人有待考究"));
put5.addColumn("data".getBytes(),"phone".getBytes(),Bytes.toBytes("15888888888"));
put5.addColumn("data".getBytes(),"say".getBytes(),Bytes.toBytes("跟诸葛亮死掐"));
Put put6 = new Put("0006".getBytes());
put6.addColumn("info".getBytes(),"id".getBytes(),Bytes.toBytes(5));
put6.addColumn("info".getBytes(),"name".getBytes(),Bytes.toBytes("xiaobubu—吕布"));
put6.addColumn("info".getBytes(),"age".getBytes(),Bytes.toBytes(28));
put6.addColumn("data".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
put6.addColumn("data".getBytes(),"address".getBytes(),Bytes.toBytes("内蒙人"));
put6.addColumn("data".getBytes(),"phone".getBytes(),Bytes.toBytes("15788888888"));
put6.addColumn("data".getBytes(),"say".getBytes(),Bytes.toBytes("貂蝉去哪了"));
List listPut = new ArrayList();
listPut.add(put);
listPut.add(put2);
listPut.add(put3);
listPut.add(put4);
listPut.add(put5);
listPut.add(put6);
myuser.put(listPut);
myuser.close();
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "hadoop01,hadoop02,hadoop03");
Connection connection = ConnectionFactory.createConnection(configuration);
Table myuser = connection.getTable(TableName.valueOf("myuser"));
Get get = new Get("0002".getBytes());
Result result = myuser.get(get);
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
if (new String(CellUtil.cloneQualifier(cell)).equals("id")||new String(CellUtil.cloneQualifier(cell)).equals("age")){
System.out.println(new String(CellUtil.cloneFamily(cell))+":"+new String(CellUtil.cloneQualifier(cell))+"\t"+Bytes.toInt(CellUtil.cloneValue(cell)));
}else {
System.out.println(new String(CellUtil.cloneFamily(cell))+":"+new String(CellUtil.cloneQualifier(cell))+"\t"+new String(CellUtil.cloneValue(cell)));
}
}
connection.close();
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "hadoop01,hadoop02,hadoop03");
Connection connection = ConnectionFactory.createConnection(configuration);
Table myuser = connection.getTable(TableName.valueOf("myuser"));
Get get = new Get("0002".getBytes());
Result result = myuser.get(get);
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
if (new String(CellUtil.cloneQualifier(cell)).equals("id")||new String(CellUtil.cloneQualifier(cell)).equals("age")){
System.out.println(new String(CellUtil.cloneFamily(cell))+":"+new String(CellUtil.cloneQualifier(cell))+"\t"+Bytes.toInt(CellUtil.cloneValue(cell)));
}else {
System.out.println(new String(CellUtil.cloneFamily(cell))+":"+new String(CellUtil.cloneQualifier(cell))+"\t"+new String(CellUtil.cloneValue(cell)));
}
}
connection.close();
本文来自:大萝卜博客网