Hbase的介绍以及安装与配置方法详见Hbase的安装与实践(一)
四、使用Java API操作Hbase
进行操作前请确保在master主机上已经安装eclipse,前面的任务已经安装过,这里不再赘述
Ⅰ.准备工作
1.配置pom.xml文件,在<version>下面添加以下内容
<dependencies>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.2.4</version>
</dependency>
<dependency>
<groupId>jdk.tools</groupId>
<artifactId>jdk.tools</artifactId>
<version>1.8</version>
<scope>system</scope>
<systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>
</dependency>
</dependencies>
2. 在src/main/resources新建文件log4j.properties,在里面写入以下内容
log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/spring.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.logger.org.apache.hadoop.util.NativeCodeLoader=ERROR
Ⅱ.创建表
3.在src/main/java下新建一个包(名字可自取),并在包内建立新的java类,取名为create_table(名字可自取) ,并且写入以下内容
ublic static void main(String[] args) throws Exception{
Configuration conf=HBaseConfiguration.create();
Connection conn=ConnectionFactory.createConnection(conf);
Admin admin=conn.getAdmin();
TableName tableName=TableName.valueOf("t1");
HTableDescriptor desc=new HTableDescriptor(tableName);
HColumnDescriptor family=new HColumnDescriptor("f1");
desc.addFamily(family);
admin.createTable(desc);
System.out.println("create table success");
}
其中导入包的名称如下:
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.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
Ⅲ.添加数据
建立新的java文件,取名为add_data(名字自取),添加下面的内容
public static void main(String[] args) throws Exception{
Configuration conf=HBaseConfiguration.create();
Connection conn=ConnectionFactory.createConnection(conf);
TableName tableName=TableName.valueOf("t1");
Table table=conn.getTable(tableName);
Put put = new Put(Bytes.toBytes("row1"));
put.addColumn(Bytes.toBytes("f1"),Bytes.toBytes("name"), Bytes.toBytes("xiaoming"));
put.addColumn(Bytes.toBytes("f1"),Bytes.toBytes("age"), Bytes.toBytes("20"));
put.addColumn(Bytes.toBytes("f1"),Bytes.toBytes("address"), Bytes.toBytes("beijing"));
table.put(put);
table.close();
System.out.println("put data success!");
}
导入的包:
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
Ⅳ.查询数据
建立新的java文件,取名为get_data(名字自取),添加下面的内容
public static void main(String[] args) throws Exception{
Configuration conf=HBaseConfiguration.create();
Connection conn=ConnectionFactory.createConnection(conf);
TableName tableName=TableName.valueOf("t1");
Table table=conn.getTable(tableName);
Get get = new Get(Bytes.toBytes("row1"));
get.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("name"));
get.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("age"));
Result rs=table.get(get);
KeyValue kv = rs.getColumnLatest(Bytes.toBytes("f1"), Bytes.toBytes("name"));
String value=Bytes.toString(kv.getValue());
System.out.println("查询到的name值为:"+value);
table.close();
System.out.println("get data success!");
}
导入的包
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
Ⅴ.删除数据
建立新的java文件,取名为delete_data(名字自取),添加下面的内容
public static void main(String[] args) throws Exception{
Configuration conf=HBaseConfiguration.create();
Connection conn=ConnectionFactory.createConnection(conf);
TableName tableName=TableName.valueOf("t1");
Table table=conn.getTable(tableName);
Delete delete=new Delete(Bytes.toBytes("row1"));
table.delete(delete);
table.close();
System.out.println("delete data success!");
}
导入的包
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
以上所有操作都可以使用Hbase Shell的命令进行验证
五、Hbase过滤器
我不会,老师没讲