好文档 - 专业文书写作范文服务资料分享网站

educoder平台使用Java操作HBase

天下 分享 时间: 加入收藏 我要投稿 点赞

1创建表

package step1;

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.TableName; import org.apache.hadoop.hbase.client.Admin;

import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;

import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; 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.Put; 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.client.Table;

import org.apache.hadoop.hbase.client.TableDescriptor;

import org.apache.hadoop.hbase.client.TableDescriptorBuilder; import org.apache.hadoop.hbase.util.Bytes; /**

* HBase 1.0 version of ExampleClient that uses {@code Connection}, * {@code Admin} and {@code Table}. */

public class Task{

public void createTable()throws Exception{ /********* Begin *********/

Configuration config = HBaseConfiguration.create();

Connection connection = ConnectionFactory.createConnection(config); try {

// Create table

Admin admin = connection.getAdmin(); try {

TableName tableName = TableName.valueOf(\ // 新API构建表

// TableDescriptor对象通过TableDescriptorBuilder构建; TableDescriptorBuilder tableDescriptor TableDescriptorBuilder.newBuilder(tableName); ColumnFamilyDescriptor family

ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(\构建列族对象 tableDescriptor.setColumnFamily(family); // 设置列族 admin.createTable(tableDescriptor.build()); // 创建表

= =

TableName emp = TableName.valueOf(\

// 新API构建表

// TableDescriptor对象通过TableDescriptorBuilder构建; TableDescriptorBuilder empDescriptor = TableDescriptorBuilder.newBuilder(emp); ColumnFamilyDescriptor empfamily = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(\构建列族对象 empDescriptor.setColumnFamily(empfamily); // 设置列族 admin.createTable(empDescriptor.build()); // 创建表 } finally {

admin.close(); } } finally {

connection.close(); }

/********* End *********/ } }

命令行:

start-dfs.sh(Hadoop启动) 回车

start-hbase.sh(hbase启动) 测评

——————————————————————————————————————— 2添加数据 package step2;

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.TableName; import org.apache.hadoop.hbase.client.Admin;

import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;

import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; 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.Put; 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.client.Table;

import org.apache.hadoop.hbase.client.TableDescriptor;

import org.apache.hadoop.hbase.client.TableDescriptorBuilder; import org.apache.hadoop.hbase.util.Bytes; public class Task {

public void insertInfo()throws Exception{ /********* Begin *********/

Configuration config = HBaseConfiguration.create();

Connection connection = ConnectionFactory.createConnection(config); Admin admin = connection.getAdmin();

TableName tableName = TableName.valueOf(\ TableDescriptorBuilder tableDescriptor TableDescriptorBuilder.newBuilder(tableName); ColumnFamilyDescriptor family

ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(\构建列族对象 tableDescriptor.setColumnFamily(family); // 设置列族 admin.createTable(tableDescriptor.build()); // 创建表 //添加数据

byte[] row1 = Bytes.toBytes(\ Put put1 = new Put(row1);

byte[] columnFamily1 = Bytes.toBytes(\ //列

byte[] qualifier1 = Bytes.toBytes(String.valueOf(1)); //列族修饰词 byte[] value1 = Bytes.toBytes(\张三丰\ //值 put1.addColumn(columnFamily1, qualifier1, value1); byte[] row2 = Bytes.toBytes(\ Put put2 = new Put(row2);

= =

byte[] columnFamily2 = Bytes.toBytes(\ //列

byte[] qualifier2 = Bytes.toBytes(String.valueOf(2)); //列族修饰词 byte[] value2 = Bytes.toBytes(\张无忌\ //值 put2.addColumn(columnFamily2, qualifier2, value2); Table table = connection.getTable(tableName); table.put(put1); table.put(put2);

/********* End *********/ } } 测评

——————————————————————————————————————— 3获取数据 package step3;

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.TableName;

import org.apache.hadoop.hbase.client.Admin;

import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;

import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; 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.Put; 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.client.Table;

import org.apache.hadoop.hbase.client.TableDescriptor;

import org.apache.hadoop.hbase.client.TableDescriptorBuilder; import org.apache.hadoop.hbase.util.Bytes; public class Task {

public void queryTableInfo()throws Exception{ /********* Begin *********/

Configuration config = HBaseConfiguration.create();

Connection connection = ConnectionFactory.createConnection(config); Admin admin = connection.getAdmin();

TableName tableName = TableName.valueOf(\ Table table = connection.getTable(tableName);

//获取数据

Get get = new Get(Bytes.toBytes(\ //定义get对象

Result result = table.get(get); //通过table对象获取数据 //System.out.println(\

//很多时候我们只需要获取“值” 这里表示获取 data:1 列族的值

byte[] valueBytes = result.getValue(Bytes.toBytes(\获取到的是字节数组

//将字节转成字符串

String valueStr = new String(valueBytes,\-8\ System.out.println(\

TableName tableStep3Name = TableName.valueOf(\ Table step3Table = connection.getTable(tableStep3Name); //批量查询

Scan scan = new Scan();

ResultScanner scanner = step3Table.getScanner(scan); try {

int i = 0;

for (Result scannerResult: scanner) { //byte[] value = scannerResult.getValue(Bytes.toBytes(\Bytes.toBytes(1));

// System.out.println(\ byte[] row = scannerResult.getRow();

System.out.println(\-8\ } } finally {

scanner.close(); }

/********* End *********/ } }

测评

——————————————————————————————————————— 4删除表

package step4;

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.TableName; import org.apache.hadoop.hbase.client.Admin;

import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;

import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; 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.Put; 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.client.Table;

import org.apache.hadoop.hbase.client.TableDescriptor;

import org.apache.hadoop.hbase.client.TableDescriptorBuilder; import org.apache.hadoop.hbase.util.Bytes; public class Task {

public void deleteTable()throws Exception{ /********* Begin *********/

Configuration config = HBaseConfiguration.create();

Connection connection = ConnectionFactory.createConnection(config); Admin admin = connection.getAdmin();

TableName tableName = TableName.valueOf(\ admin.disableTable(tableName); admin.deleteTable(tableName); /********* End *********/ } }

测评

educoder平台使用Java操作HBase

1创建表packagestep1;importjava.io.IOException;importorg.apache.hadoop.conf.Configuration;importorg.apache.hadoop.hbase.HBaseConfiguration;importorg.apache.hadoop.
推荐度:
点击下载文档文档为doc格式
0oc611daso77t6k14pg60zn011onzd01aks
领取福利

微信扫码领取福利

微信扫码分享