使用Eclipse编写java程序,来对HBase数据库进行增删改查等操作,Eclipse可以在Ubuntu软件中心搜索下载并安装。
一、 启动hadoop和hbase
start-dfs
.sh
start-hbase
.sh
二、新建Java工程和项目(文件)
在eclipse中新建Java工程和项目(文件)
(1)新建java工程
(2) 新建java class文件
右击工程->new->class
三、 在工程中导入外部jar包
这里只需要导入hbase安装目录中的lib文件中的所有jar包。在新建Java工程的界面点“next->library->add external jar …”
四、 编写代码
import org
.apache
.hadoop
.conf
.Configuration
;
import org
.apache
.hadoop
.hbase
.*
;
import org
.apache
.hadoop
.hbase
.client
.*
;
import java
.io
.IOException
;
public class ExampleForHbase{
public static Configuration configuration
;
public static Connection connection
;
public static Admin admin
;
public static void main(String
[] args
)throws IOException
{
createTable("Score",new
String[]{"sname","course"});
}
public static void init(){
configuration
= HBaseConfiguration
.create(); configuration
.set("hbase.rootdir","hdfs://localhost:9000/hbase");
try{
connection
= ConnectionFactory
.createConnection(configuration
);
admin
= connection
.getAdmin();
}catch (IOException e
){
e
.printStackTrace();
}
}
public static void close(){
try{
if(admin
!= null
){
admin
.close();
}
if(null
!= connection
){
connection
.close();
}
}catch (IOException e
){
e
.printStackTrace();
}
}
public static void createTable(String myTableName
,String
[] colFamily
) throws IOException
{
init();
TableName tableName
= TableName
.valueOf(myTableName
);
if(admin
.tableExists(tableName
)){
System
.out
.println("talbe is exists!");
}
else {
HTableDescriptor hTableDescriptor
= new HTableDescriptor(tableName
);
for(String str
:colFamily
){
HColumnDescriptor hColumnDescriptor
= new HColumnDescriptor(str
);
hTableDescriptor
.addFamily(hColumnDescriptor
);
}
admin
.createTable(hTableDescriptor
);
System
.out
.println("create table success");
}
close();
}
public static void deleteTable(String tableName
) throws IOException
{
init();
TableName tn
= TableName
.valueOf(tableName
);
if(admin
.tableExists(tn
)) {
admin
.disableTable(tn
);
admin
.deleteTable(tn
);
}
close();
}
public static void listTables() throws IOException
{
init();
HTableDescriptor hTableDescriptors
[] = admin
.listTables();
for(HTableDescriptor hTableDescriptor
:hTableDescriptors
){ System
.out
.println(hTableDescriptor
.getNameAsString());
}
close();
}
public static void insertRow(String tableName
,String rowKey
,String colFamily
,String col
,String val
) throws IOException
{
init();
Table table
= connection
.getTable(TableName
.valueOf(tableName
));
Put put
= new Put(rowKey
.getBytes());
put
.addColumn(colFamily
.getBytes(), col
.getBytes(), val
.getBytes());
table
.put(put
);
table
.close();
close();
}
public static void deleteRow(String tableName
,String rowKey
,String colFamily
,String col
) throws IOException
{
init();
Table table
= connection
.getTable(TableName
.valueOf(tableName
));
Delete delete
= new Delete(rowKey
.getBytes());
table
.delete(delete
);
table
.close();
close();
}
public static void getData(String tableName
,String rowKey
,String colFamily
,String col
)throws IOException
{
init();
Table table
= connection
.getTable(TableName
.valueOf(tableName
));
Get get
= new Get(rowKey
.getBytes());
get
.addColumn(colFamily
.getBytes(),col
.getBytes());
Result result
= table
.get(get
);
showCell(result
);
table
.close();
close();
}
public static void showCell(Result result
){
Cell
[] cells
= result
.rawCells();
for(Cell cell
:cells
){
System
.out
.println("RowName:"+new String(CellUtil
.cloneRow(cell
))+" ");
System
.out
.println("Timetamp:"+cell
.getTimestamp()+"
"
);
System
.out
.println("column Family:"+new String(CellUtil
.cloneFamily(cell
))+" ");
System
.out
.println("row Name:"+new String(CellUtil
.cloneQualifier(cell
))+" ");
System
.out
.println("value:"+new String(CellUtil
.cloneValue(cell
))+" ");
}
}
}
注:代码非原创,但是忘记当时看的哪位大佬的了,hhhhh
五、 编译运行程序
Run as->java application
六、 打包成jar包
右击class文件->export->runnable jarfile
七、 在终端运行java程序