产品动态
产品公告
安全公告
/usr/local/service/hbase:[root@172 ~]# su hadoop[hadoop@10root]$ cd /usr/local/service/hbase
[hadoop@10hbase]$ bin/hbase shell
hbase(main):001:0> create 'test', 'cf'
list 指令来查看您建立的表是否已经存在。hbase(main):002:0> list 'test'TABLEtest1 row(s) in 0.0030 seconds=> ["test"]
put指令来为您创建的表加入元素:hbase(main):003:0> put 'test', 'row1', 'cf:a', 'value1'0 row(s) in 0.0850 secondshbase(main):004:0> put 'test', 'row2', 'cf:b', 'value2'0 row(s) in 0.0110 secondshbase(main):005:0> put 'test', 'row3', 'cf:c', 'value3'0 row(s) in 0.0100 seconds
scan指令来遍历整个表:hbase(main):006:0> scan 'test'ROW COLUMN+CELLrow1 column=cf:a, timestamp=1530276759697, value=value1row2 column=cf:b, timestamp=1530276777806, value=value2row3 column=cf:c, timestamp=1530276792839, value=value33 row(s) in 0.2110 seconds
get指令来取得表中指定行的值:hbase(main):007:0> get 'test', 'row1'COLUMN CELLcf:a timestamp=1530276759697, value=value1 row(s) in 0.0790 seconds
drop指令来删除一个表,在删除表之前需要先使用disable指令来禁用一个表:hbase(main):010:0> disable 'test'hbase(main):011:0> drop 'test'
quit指令来关闭 hbase shell。D://mavenWorkplace中,输入如下命令新建一个 Maven 工程:mvn archetype:generate -DgroupId=$yourgroupID -DartifactId=$yourartifactID-DarchetypeArtifactId=maven-archetype-quickstart
D://mavenWorkplace 目录下就会生成一个名为 $yourartifactID 的工程文件夹。其中的文件结构如下所示:simple---pom.xml 核心配置,项目根下---src---main---java Java 源码目录---resources Java 配置文件目录---test---java 测试源码目录---resources 测试配置目录
<dependencies><dependency><groupId>org.apache.hbase</groupId><artifactId>hbase-client</artifactId><version>1.2.4</version></dependency></dependencies>
<build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>1.8</source><target>1.8</target><encoding>utf-8</encoding></configuration></plugin><plugin><artifactId>maven-assembly-plugin</artifactId><configuration><descriptorRefs><descriptorRef>jar-with-dependencies</descriptorRef></descriptorRefs></configuration><executions><execution><id>make-assembly</id><phase>package</phase><goals><goal>single</goal></goals></execution></executions></plugin></plugins></build>
/usr/local/service/hbase/conf 目录,查看 hbase-site.xml 的 hbase.zookeeper.quorum 配置获得 zookeeper 的 IP 地址 $quorum,hbase.zookeeper.property.clientPort 配置获得 zookeeper 的端口号 $clientPort。import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.*;import org.apache.hadoop.hbase.client.*;import org.apache.hadoop.hbase.util.Bytes;import org.apache.hadoop.hbase.io.compress.Compression.Algorithm;import java.io.IOException;/*** Created by tencent on 2018/6/30.*/public class PutExample {public static void main(String[] args) throws IOException {Configuration conf = HBaseConfiguration.create();conf.set("hbase.zookeeper.quorum","$quorum");conf.set("hbase.zookeeper.property.clientPort","$clientPort");conf.set("zookeeper.znode.parent", "$znodePath");Connection connection = ConnectionFactory.createConnection(conf);Admin admin = connection.getAdmin();HTableDescriptor table = new HTableDescriptor(TableName.valueOf("test1"));table.addFamily(new HColumnDescriptor("cf").setCompressionType(Algorithm.NONE));System.out.print("Creating table. ");if (admin.tableExists(table.getTableName())) {admin.disableTable(table.getTableName());admin.deleteTable(table.getTableName());}admin.createTable(table);Table table1 = connection.getTable(TableName.valueOf("test1"));Put put1 = new Put(Bytes.toBytes("row1"));put1.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("a"),Bytes.toBytes("value1"));table1.put(put1);Put put2 = new Put(Bytes.toBytes("row2"));put2.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("b"),Bytes.toBytes("value2"));table1.put(put2);Put put3 = new Put(Bytes.toBytes("row3"));put3.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("c"),Bytes.toBytes("value3"));table1.put(put3);System.out.println(" Done.");}}
mvn package
scp $localfile root@公网IP地址:$remotefolder
[hadoop@10 hadoop]$ java –jar $package.jar
list命令来查看使用 API 创建的 Hbase 表是否成功。如果成功可使用scan命令来查看表的具体内容。[hadoop@10hbase]$ bin/hbase shellhbase(main):002:0> list 'test1'TABLETest11 row(s) in 0.0030 seconds=> ["test1"]hbase(main):006:0> scan 'test1'ROW COLUMN+CELLrow1 column=cf:a, timestamp=1530276759697, value=value1row2 column=cf:b, timestamp=1530276777806, value=value2row3 column=cf:c, timestamp=1530276792839, value=value33 row(s) in 0.2110 seconds
文档反馈