HBASE操作(2)-JAVA API
2016-06-02 本文已影响212人
逸飞u
安装
0.安装 Eclipse
1.打开 Eclipse,新建 JAVA 项目
2.在项目中点属性,选择JAVA Build Path\Libraries
,点添加扩展 jar 包
3.把这些目录下的 jar 包都添加进去
hbase
hbase\lib
hadoop
hadoop\lib
4.修改 Eclipse 所在系统的 hosts 文件
修改C:\Windows\system32\drivers\etc\hosts文件
添加如下内容
xxx.xxx.xxx.xxx hadoop0 xxx.xxx.xxx.xxx hadoop1 xxx.xxx.xxx.xxx hadoop2
5.完成安装,开始写代码
使用
举例:
- import
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
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;
- demo 数据
public static final String TABLE_NAME = "users" ;
public static final String FAMILY_NAME = "info" ;
public static final String COLUMN_NAME = "age" ;
public static final String ROW_KEY = "xiaoming" ;
- 设置 HBase 连接
//HBase设置(集群)
private static Configuration getConfiguration() {
Configuration conf = HBaseConfiguration. create();
conf.set( "hbase.rootdir" , "hdfs://hadoop0:9000/hbase" );
conf.set( "hbase.zookeeper.quorum" , "hadoop0,hadoop1,hadoop2" );
return conf ;
}
- 创建表
//创建表
private static void createTabele(HBaseAdmin hbaseAdmin) throws IOException {
if (!hbaseAdmin .tableExists( TABLE_NAME)) {
HTableDescriptor hTableDescriptor = new HTableDescriptor(TABLE_NAME );
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(
FAMILY_NAME );
hTableDescriptor .addFamily(hColumnDescriptor );
hbaseAdmin .createTable(hTableDescriptor );
}
}
- 插入或修改记录
//插入或修改记录
private static void putRecord(HTable htable) throws IOException {
// TODO Auto-generated method stub
Put put = new Put(ROW_KEY .getBytes());
put.add( FAMILY_NAME .getBytes(), COLUMN_NAME.getBytes(), "25" .getBytes());
htable.put( put);
System. out .println("insert or update a record" );
}
//插入或修改记录2
private static void putRecord2(HTable htable, String val)
throws IOException {
// TODO Auto-generated method stub
Put put = new Put(ROW_KEY .getBytes());
put.add( FAMILY_NAME .getBytes(), COLUMN_NAME.getBytes(), val .getBytes());
htable.put( put);
System. out .println("insert or update a record" );
}
读取记录
//读取记录
private static void getRecord(HTable htable) throws Throwable {
Get get = new Get(ROW_KEY .getBytes());
Result result = htable.get( get);
byte [] value = result .getValue( FAMILY_NAME.getBytes(),
COLUMN_NAME .getBytes());
System. out .println("查询结果为:" + new String( value));
}
- 显示所有记录
//显示所有记录
private static void scanTable(HTable htable) throws IOException {
// TODO Auto-generated method stub
System. out .println("遍历表结果如下:" );
Scan scan = new Scan();
ResultScanner resultScanner = htable .getScanner( scan);
for (Result result : resultScanner ) {
//遍历数据
// byte[] value = result.getValue(FAMILY_NAME.getBytes(),
// COLUMN_NAME.getBytes());
// System.out.println(new String(value));
System. out .println("Scan: " + result );
}
}
- 删除表
//删除表
private static void dropTabele(HBaseAdmin hbaseAdmin ) throws Throwable {
if (hbaseAdmin .tableExists( TABLE_NAME)) {
try {
hbaseAdmin .disableTable( TABLE_NAME);
hbaseAdmin .deleteTable( TABLE_NAME);
} catch (IOException e ){
e.printStackTrace();
System. out .println("Delete " + hbaseAdmin + "失败" );
}
}
System. out .println("Delete " + hbaseAdmin + "成功" );
}
}