hbase java api样例(版本1.3.1,新API)
Posted 大墨垂杨
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hbase java api样例(版本1.3.1,新API)相关的知识,希望对你有一定的参考价值。
验证了如下几种java api的使用方法。
1.创建表
2.创建表(预分区)
3.单条插入
4.批量插入
5.批量插入(写缓存)
6.单条get
7.批量get
8.简单scan
具体请参考GitHub。
https://github.com/quchunhui/hbase_sample
pom.xml文件
<?xml version="1.0" encoding="UTF-8"?> <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"> <artifactId>hbase_sample</artifactId> <groupId>hbase_sample</groupId> <version>1.0</version> <modelVersion>4.0.0</modelVersion> <dependencies> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-server</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-common</artifactId> <version>1.3.1</version> </dependency> </dependencies> <build> <sourceDirectory>src/main/java</sourceDirectory> <outputDirectory>target/classes</outputDirectory> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>2.4</version> <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> </project>
===创建表===
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; public class create_table_sample1 { public static void main(String[] args) throws Exception { Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "192.168.1.80,192.168.1.81,192.168.1.82"); Connection connection = ConnectionFactory.createConnection(conf); Admin admin = connection.getAdmin(); HTableDescriptor desc = new HTableDescriptor(TableName.valueOf("TEST1")); HColumnDescriptor family1 = new HColumnDescriptor(constants.COLUMN_FAMILY_DF.getBytes()); family1.setTimeToLive(2 * 60 * 60 * 24); //过期时间 family1.setMaxVersions(2); //版本数 desc.addFamily(family1); HColumnDescriptor family2 = new HColumnDescriptor(constants.COLUMN_FAMILY_EX.getBytes()); family2.setTimeToLive(3 * 60 * 60 * 24); //过期时间 family2.setMaxVersions(3); //版本数 desc.addFamily(family2); admin.createTable(desc); } }
===创建表(预分区)===
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; import org.apache.hadoop.hbase.regionserver.BloomType; import org.apache.hadoop.hbase.util.Bytes; public class create_table_sample2 { public static void main(String[] args) throws Exception { Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "192.168.1.80,192.168.1.81,192.168.1.82"); Connection connection = ConnectionFactory.createConnection(conf); Admin admin = connection.getAdmin(); TableName table_name = TableName.valueOf("TEST1"); if (admin.tableExists(table_name)) { admin.disableTable(table_name); admin.deleteTable(table_name); } HTableDescriptor desc = new HTableDescriptor(table_name); HColumnDescriptor family1 = new HColumnDescriptor(constants.COLUMN_FAMILY_DF.getBytes()); family1.setTimeToLive(3 * 60 * 60 * 24); //过期时间 family1.setBloomFilterType(BloomType.ROW); //按行过滤 family1.setMaxVersions(3); //版本数 desc.addFamily(family1); HColumnDescriptor family2 = new HColumnDescriptor(constants.COLUMN_FAMILY_EX.getBytes()); family2.setTimeToLive(2 * 60 * 60 * 24); //过期时间 family2.setBloomFilterType(BloomType.ROW); //按行过滤 family2.setMaxVersions(2); //版本数 desc.addFamily(family2); byte[][] splitKeys = { Bytes.toBytes("row01"), Bytes.toBytes("row02"), Bytes.toBytes("row04"), Bytes.toBytes("row06"), Bytes.toBytes("row08"), }; admin.createTable(desc, splitKeys); } }
===单条插入===
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes; import java.util.Random; public class table_put_sample1 { public static void main(String[] args) throws Exception { Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "192.168.1.80,192.168.1.81,192.168.1.82"); Connection connection = ConnectionFactory.createConnection(conf); Table table = connection.getTable(TableName.valueOf(constants.TABLE_NAME)); Random random = new Random(); String[] rows = new String[] {"01", "02", "03"}; String[] names = new String[] {"zhang san", "li si", "wang wu", "wei liu"}; String[] sexs = new String[] {"men", "women"}; String[] heights = new String[] {"165cm", "170cm", "175cm", "180cm"}; String[] weights = new String[] {"50kg", "55kg", "60kg", "65kg", "70kg", "75kg", "80kg"}; Put put = new Put(Bytes.toBytes("row" + rows[random.nextInt(rows.length)])); String name = names[random.nextInt(names.length)]; put.addColumn(constants.COLUMN_FAMILY_DF.getBytes(), "name".getBytes(), name.getBytes()); String sex = sexs[random.nextInt(sexs.length)]; put.addColumn(constants.COLUMN_FAMILY_DF.getBytes(), "sex".getBytes(), sex.getBytes()); String height = heights[random.nextInt(heights.length)]; put.addColumn(constants.COLUMN_FAMILY_EX.getBytes(), "height".getBytes(), height.getBytes()); String weight = weights[random.nextInt(weights.length)]; put.addColumn(constants.COLUMN_FAMILY_EX.getBytes(), "weight".getBytes(), weight.getBytes()); table.put(put); table.close(); connection.close(); } }
===批量插入===
方式一:
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes; import java.util.ArrayList; import java.util.List; import java.util.Random; public class table_put_sample2 { public static void main(String[] args) throws Exception { Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "192.168.1.80,192.168.1.81,192.168.1.82"); Connection connection = ConnectionFactory.createConnection(conf); Table table = connection.getTable(TableName.valueOf(constants.TABLE_NAME)); Random random = new Random(); String[] rows = new String[] {"01", "02", "03"}; String[] names = new String[] {"zhang san", "li si", "wang wu", "wei liu"}; String[] sexs = new String[] {"men", "women"}; String[] heights = new String[] {"165cm", "170cm", "175cm", "180cm"}; String[] weights = new String[] {"50kg", "55kg", "60kg", "65kg", "70kg", "75kg", "80kg"}; List<Put> puts = new ArrayList<>(); for(String row : rows) { Put put = new Put(Bytes.toBytes("row" + row)); String name = names[random.nextInt(names.length)]; put.addColumn(constants.COLUMN_FAMILY_DF.getBytes(), "name".getBytes(), name.getBytes()); String sex = sexs[random.nextInt(sexs.length)]; put.addColumn(constants.COLUMN_FAMILY_DF.getBytes(), "sex".getBytes(), sex.getBytes()); String height = heights[random.nextInt(heights.length)]; put.addColumn(constants.COLUMN_FAMILY_EX.getBytes(), "height".getBytes(), height.getBytes()); String weight = weights[random.nextInt(weights.length)]; put.addColumn(constants.COLUMN_FAMILY_EX.getBytes(), "weight".getBytes(), weight.getBytes()); puts.add(put); } table.put(puts); table.close(); connection.close(); } }
方式二:
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes; import java.util.ArrayList; import java.util.List; import java.util.Random; public class table_put_sample3 { public static void main(String[] args) throws Exception { Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "192.168.1.80,192.168.1.81,192.168.1.82"); Connection connection = ConnectionFactory.createConnection(conf); Table table = connection.getTable(TableName.valueOf(constants.TABLE_NAME)); Random random = new Random(); String[] rows = new String[] {"01", "02", "03"}; String[] names = new String[] {"zhang san", "li si", "wang wu", "wei liu"}; String[] sexs = new String[] {"men", "women"}; String[] heights = new String[] {"165cm", "170cm", "175cm", "180cm"}; String[] weights = new String[] {"50kg", "55kg", "60kg", "65kg", "70kg", "75kg", "80kg"}; List<Row> batch = new ArrayList<>(); for(String row : rows) { Put put = new Put(Bytes.toBytes("row" + row)); String name = names[random.nextInt(names.length)]; put.addColumn(constants.COLUMN_FAMILY_DF.getBytes(), "name".getBytes(), name.getBytes()); String sex = sexs[random.nextInt(sexs.length)]; put.addColumn(constants.COLUMN_FAMILY_DF.getBytes(), "sex".getBytes(), sex.getBytes()); String height = heights[random.nextInt(heights.length)]; put.addColumn(constants.COLUMN_FAMILY_EX.getBytes(), "height".getBytes(), height.getBytes()); String weight = weights[random.nextInt(weights.length)]; put.addColumn(constants.COLUMN_FAMILY_EX.getBytes(), "weight".getBytes(), weight.getBytes()); batch.add(put); } Object[] results = new Object[batch.size()]; try { table.batch(batch, results); } catch (InterruptedException e) { e.printStackTrace(); return; } batch.clear(); table.close(); connection.close(); } }
===批量插入(写缓存)===
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes; import java.util.ArrayList; import java.util.List; import java.util.Random; public class table_put_sample4 { public static void main(String[] args) throws Exception { Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "192.168.1.80,192.168.1.81,192.168.1.82"); conf.set("hbase.client.write.buffer", "1048576");//1M Connection connection = ConnectionFactory.createConnection(conf); BufferedMutator table = connection.getBufferedMutator(TableName.valueOf(constants.TABLE_NAME)); System.out.print("[--------]write buffer size = " + table.getWriteBufferSize()); Random random = new Random(); String[] rows = new String[] {"01", "02", "03", "04", "05"}; String[] names = new String[] {"zhang san", "li si", "wang wu", "wei liu"}; String[] sexs = new String[] {"men", "women"}; String[] heights = new String[] {"165cm", "170cm", "175cm", "180cm"}; String[] weights = new String[] {"50kg", "55kg", "60kg", "65kg", "70kg", "75kg", "80kg"}; List<Mutation> batch = new ArrayList<>(); for(String row : rows) { Put put = new Put(Bytes.toBytes("row" + row)); String name = names[random.nextInt(names.length)]; put.addColumn(constants.COLUMN_FAMILY_DF.getBytes(), "name".getBytes(), name.getBytes()); String sex = sexs[random.nextInt(sexs.length)]; put.addColumn(constants.COLUMN_FAMILY_DF.getBytes(), "sex".getBytes(), sex.getBytes()); String height = heights[random.nextInt(heights.length)]; put.addColumn(constants.COLUMN_FAMILY_EX.getBytes(), "height".getBytes(), height.getBytes()); String weight = weights[random.nextInt(weights.length)]; put.addColumn(constants.COLUMN_FAMILY_EX.getBytes(), "weight".getBytes(), weight.getBytes()); batch.add(put); } table.mutate(batch); table.flush(); table.close(); connection.close(); } }
===单条get===
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; public class table_get_sample1 { public static void main(String[] args) throws Exception { Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "192.168.1.80,192.168.1.81,192.168.1.82"); Connection connection = ConnectionFactory.createConnection(conf); Table table = connection.getTable(TableName.valueOf(constants.TABLE_NAME)); Get get = new Get(("row01").getBytes()); Result result = table.get(get); byte[] name = result.getValue(constants.COLUMN_FAMILY_DF.getBytes(), "name".getBytes()); byte[] weight = result.getValue(constants.COLUMN_FAMILY_EX.getBytes(), "weight".getBytes()); System.out.print("[------]name=" + new String(name) + "\n"); System.out.print("[------]name=" + new String(weight) + "\n"); } }
===批量get===
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import java.util.ArrayList; import java.util.List; public class table_get_sample4 { public static void main(String[] args) throws Exception { Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "192.168.1.80,192.168.1.81,192.168.1.82"); Connection connection = ConnectionFactory.createConnection(conf); Table table = connection.getTable(TableName.valueOf(constants.TABLE_NAME)); List<Get> gets = new ArrayList<>(); Get get1 = new Get(("row01").getBytes()); get1.addColumn(constants.COLUMN_FAMILY_DF.getBytes(), "name".getBytes()); get1.addColumn(constants.COLUMN_FAMILY_DF.getBytes(), "sex".getBytes()); get1.addColumn(constants.COLUMN_FAMILY_EX.getBytes(), "height".getBytes()); get1.addColumn(constants.COLUMN_FAMILY_EX.getBytes(), "weight".getBytes()); gets.add(get1); Get get2 = new Get(("row02").getBytes()); get2.addColumn(constants.COLUMN_FAMILY_DF.getBytes(), "name".getBytes()); get2.addColumn(constants.COLUMN_FAMILY_DF.getBytes(), "sex".getBytes()); get2.addColumn(constants.COLUMN_FAMILY_EX.getBytes(), "height".getBytes()); get2.addColumn(constants.COLUMN_FAMILY_EX.getBytes(), "weight".getBytes()); gets.add(get2); Result[] results = table.get(gets); for ( Result result : results) { byte[] name = result.getValue(constants.COLUMN_FAMILY_DF.getBytes(), "name".getBytes()); byte[] sex = result.getValue(constants.COLUMN_FAMILY_DF.getBytes(), "sex".getBytes()); byte[] height = result.getValue(constants.COLUMN_FAMILY_EX.getBytes(), "height".getBytes()); byte[] weight = result.getValue(constants.COLUMN_FAMILY_EX.getBytes(), "weight".getBytes()); System.out.print("[------]name=" + new String(name) + "\n"); System.out.print("[------]sex=" + new String(sex) + "\n"); System.out.print("[------]height=" + new String(height) + "\n"); System.out.print("[------]weight=" + new String(weight) + "\n"); } } }
===简单scan===
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; public class table_scan_sample3 { public static void main(String[] args) throws Exception { Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "192.168.1.80,192.168.1.81,192.168.1.82"); Connection connection = ConnectionFactory.createConnection(conf); Table table = connection.getTable(TableName.valueOf(constants.TABLE_NAME)); Scan scan = new Scan(); scan.addColumn(constants.COLUMN_FAMILY_DF.getBytes(), "name".getBytes()); scan.addFamily(constants.COLUMN_FAMILY_EX.getBytes()); ResultScanner rs = table.getScanner(scan); for (Result r = rs.next(); r != null; r = rs.next()) { byte[] row_key = r.getRow(); System.out.print("[------]row_key=" + new String(row_key) + "\n"); byte[] name = r.getValue(constants.COLUMN_FAMILY_DF.getBytes(), "name".getBytes()); System.out.print("[------]name=" + new String(name) + "\n"); byte[] weight = r.getValue(constants.COLUMN_FAMILY_EX.getBytes(), "weight".getBytes()); System.out.print("[------]weight=" + new String(weight) + "\n"); } } }
--END--
以上是关于hbase java api样例(版本1.3.1,新API)的主要内容,如果未能解决你的问题,请参考以下文章