原帖地址: http://blog.csdn.net/nsrainbow/article/details/42649249 最新课程请关注原作者博客,获得更好的显示体验
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.crazycake</groupId> <artifactId>playhbase</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>playhbase</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <resources> <resource> <directory>${basedir}/conf</directory> <filtering>false</filtering> <includes> <include>hbase-site.xml</include> </includes> </resource> </resources> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>0.98.4-hadoop2</version> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>2.0.2</version> <configuration> <source>1.6</source> <target>1.6</target> <encoding>UTF-8</encoding> <optimise>true</optimise> <compilerArgument>-nowarn</compilerArgument> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.3</version> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ApacheLicenseResourceTransformer"> </transformer> </transformers> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
<?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="configuration.xsl"?> <!-- /** * Copyright 2010 The Apache Software Foundation * * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ --> <configuration> <property> <name>hbase.cluster.distributed</name> <value>true</value> </property> <property> <name>hbase.zookeeper.quorum</name> <value>host1,host2</value> </property> <property> <name>hbase.zookeeper.property.clientPort</name> <value>2181</value> </property> </configuration>
package org.crazycake.playhbase; 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.MasterNotRunningException; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.ZooKeeperConnectionException; 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.util.Bytes; public class CreateTable { public static void main(String[] args) throws MasterNotRunningException, ZooKeeperConnectionException, IOException { Configuration config = HBaseConfiguration.create(); // 这边注释起来的是动态设定zookeeper参数的方法,如果你没有hbase-site.xml 或者想动态改变 // 可以采用动态方式设定 // // config.set("hbase.zookeeper.quorum", // "zookeepernode0,zookeepernode1,zookeepernode2"); //config.set("hbase.zookeeper.property.clientPort", "2181"); //config.set("hbase.cluster.distributed", "true"); // 使用配置文件创建一个 admin 对象 HBaseAdmin admin = new HBaseAdmin(config); // 创建表 HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf("people")); // 创建2个列簇 tableDescriptor.addFamily(new HColumnDescriptor("name")); tableDescriptor.addFamily(new HColumnDescriptor("contactinfo")); admin.createTable(tableDescriptor); // 接下来搞点数据进去呗 String[][] people = { { "1", "Marcel", "Haddad", "[email protected]"}, { "2", "Franklin", "Holtz", "[email protected]" }, { "3", "Dwayne", "McKee", "[email protected]" }, { "4", "Rae", "Schroeder", "[email protected]" }, { "5", "Rosalie", "burton", "[email protected]"}, { "6", "Gabriela", "Ingram", "[email protected]"} }; HTable table = new HTable(config, "people"); // 把这些数据插入到表里面 for (int i = 0; i< people.length; i++) { //第一列做rowkey Put person = new Put(Bytes.toBytes(people[i][0])); //把 Marcel 放到 name 这个列簇的 first 这个字段去 person.add(Bytes.toBytes("name"), Bytes.toBytes("first"), Bytes.toBytes(people[i][1])); person.add(Bytes.toBytes("name"), Bytes.toBytes("last"), Bytes.toBytes(people[i][2])); person.add(Bytes.toBytes("contactinfo"), Bytes.toBytes("email"), Bytes.toBytes(people[i][3])); table.put(person); } // 最后要记得提交和关闭表 table.flushCommits(); table.close(); } }
tail -200f /var/log/hbase/hbase-hbase-master-host1.localdomain.log
hbase(main):003:0> scan 'people' ROW COLUMN+CELL 1 column=contactinfo:email, timestamp=1421338694666, [email protected] 1 column=name:first, timestamp=1421338694666, value=Marcel 1 column=name:last, timestamp=1421338694666, value=Haddad 2 column=contactinfo:email, timestamp=1421338694932, [email protected] 2 column=name:first, timestamp=1421338694932, value=Franklin 2 column=name:last, timestamp=1421338694932, value=Holtz 3 column=contactinfo:email, timestamp=1421338694977, [email protected] 3 column=name:first, timestamp=1421338694977, value=Dwayne 3 column=name:last, timestamp=1421338694977, value=McKee 4 column=contactinfo:email, timestamp=1421338695034, [email protected] 4 column=name:first, timestamp=1421338695034, value=Rae 4 column=name:last, timestamp=1421338695034, value=Schroeder 5 column=contactinfo:email, timestamp=1421338695054, [email protected] 5 column=name:first, timestamp=1421338695054, value=Rosalie 5 column=name:last, timestamp=1421338695054, value=burton 6 column=contactinfo:email, timestamp=1421338695076, [email protected] 6 column=name:first, timestamp=1421338695076, value=Gabriela 6 column=name:last, timestamp=1421338695076, value=Ingram 6 row(s) in 0.3910 seconds
package org.crazycake.playhbase; import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.client.ResultScanner; import org.apache.hadoop.hbase.client.Scan; import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp; import org.apache.hadoop.hbase.filter.RegexStringComparator; import org.apache.hadoop.hbase.filter.SingleColumnValueFilter; import org.apache.hadoop.hbase.util.Bytes; /** * 根据email 来搜索用户 * @author alexxiyang (https://github.com/alexxiyang) * */ public class SearchByEmail { public static void main(String[] args) throws IOException { //创建配置 Configuration config = HBaseConfiguration.create(); // 打开表 HTable table = new HTable(config, "people"); //定义一系列要用到的列簇和列 // 定义列簇 byte[] contactFamily = Bytes.toBytes("contactinfo"); // 列 byte[] emailQualifier = Bytes.toBytes("email"); //列簇 byte[] nameFamily = Bytes.toBytes("name"); //列 byte[] firstNameQualifier = Bytes.toBytes("first"); byte[] lastNameQualifier = Bytes.toBytes("last"); // 创建一个正则表达式的比较器 RegexStringComparator emailFilter = new RegexStringComparator("[email protected]"); // 创建一个filter,把这个正则比较器传进去 SingleColumnValueFilter filter = new SingleColumnValueFilter(contactFamily, emailQualifier, CompareOp.EQUAL, emailFilter); // 创建一个 scan对象 Scan scan = new Scan(); //把filter 传进去 scan.setFilter(filter); // 开始查询,并获取结果 ResultScanner results = table.getScanner(scan); // 遍历结果打印数据 for (Result result : results) { String id = new String(result.getRow()); byte[] firstNameObj = result.getValue(nameFamily, firstNameQualifier); String firstName = new String(firstNameObj); byte[] lastNameObj = result.getValue(nameFamily, lastNameQualifier); String lastName = new String(lastNameObj); System.out.println(firstName + " " + lastName + " - ID: " + id); byte[] emailObj = result.getValue(contactFamily, emailQualifier); String email = new String(emailObj); System.out.println(firstName + " " + lastName + " - " + email + " - ID: " + id); } //关闭结果 results.close(); //关闭表 table.close(); } }运行结果
Rosalie burton - ID: 5 Rosalie burton - [email protected] - ID: 5
package org.crazycake.playhbase; import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.client.HBaseAdmin; /** * 删除表 * @author alexxiyang (https://github.com/alexxiyang) * */ public class DeleteTable { public static void main(String[] args) throws IOException { //创建配置 Configuration config = HBaseConfiguration.create(); // 建立 admin HBaseAdmin admin = new HBaseAdmin(config); // 先 disable 表,再delete admin.disableTable("people"); admin.deleteTable("people"); } }
hbase(main):004:0> list TABLE employee employee2 student users 4 row(s) in 4.8460 seconds => ["employee", "employee2", "student", "users"]
hbase(main):002:0> create 'users','info' 0 row(s) in 36.5110 seconds => Hbase::Table - users hbase(main):003:0> list TABLE employee employee2 student users 4 row(s) in 0.4520 seconds => ["employee", "employee2", "student", "users"] hbase(main):004:0> put 'users',1,'info:name','ted' 0 row(s) in 0.8350 seconds hbase(main):005:0> scan 'users' ROW COLUMN+CELL 1 column=info:name, timestamp=1421252020520, value=ted 1 row(s) in 0.3140 seconds
tail -200f /var/log/zookeeper/zookeeper.log