首页 \ 问答 \ 求一份尚硅谷的Java视频教程?

求一份尚硅谷的Java视频教程?

更新时间:2024-04-28 10:04

最满意答案

写了个Hbase新的api的增删改查的工具类,以供参考,直接拷贝代码就能用,散仙觉得基础的功能,都有了,代码如下:
    package com.dhgate.hbase.test;

import java.util.ArrayList;
import java.util.List;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
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.Delete;
import org.apache.hadoop.hbase.client.Get;
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.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.filter.PageFilter;
import org.apache.hadoop.hbase.filter.PrefixFilter;
import org.apache.hadoop.hbase.util.Bytes;

/**
 * 基于新的API
 * Hbase0.96版本
 * 写的工具类
 * 
 * @author qindongliang
 * 大数据技术交流群: 376932160
 * 
 * **/
public class HbaseCommons {

static Configuration conf=HBaseConfiguration.create();
	static String tableName="";

public static void main(String[] args)throws Exception {

		//String tableName="test";
		//createTable(tableName, null);

	}

/**
	 * 批量添加数据
	 * @param tableName 标名字
	 * @param rows rowkey行健的集合
	 * 本方法仅作示例,其他的内容需要看自己义务改变
	 * 
	 * **/
	public static void insertList(String tableName,String rows[])throws Exception{
		HTable table=new HTable(conf, tableName);
		List<Put> list=new ArrayList<Put>();
		for(String r:rows){
			Put p=new Put(Bytes.toBytes(r));
		   //此处示例添加其他信息
			//p.add(Bytes.toBytes("family"),Bytes.toBytes("column"), 1000, Bytes.toBytes("value"));
			list.add(p);
		}
		table.put(list);//批量添加
		table.close();//释放资源
	}

	/**
	 * 创建一个表
	 * @param tableName 表名字
	 * @param columnFamilys 列簇
	 * 
	 * **/
	public static void createTable(String tableName,String[] columnFamilys)throws Exception{
		//admin 对象
		HBaseAdmin admin=new HBaseAdmin(conf);
		if(admin.tableExists(tableName)){
			System.out.println("此表,已存在!");
		}else{
			//旧的写法
			//HTableDescriptor tableDesc=new HTableDescriptor(tableName);
			//新的api
			HTableDescriptor tableDesc=new HTableDescriptor(TableName.valueOf(tableName));

			for(String columnFamily:columnFamilys){
				tableDesc.addFamily(new HColumnDescriptor(columnFamily));
			}

			admin.createTable(tableDesc);
			System.out.println("建表成功!");

		}
		admin.close();//关闭释放资源

	}

	/**
	 * 删除一个表
	 * @param tableName 删除的表名
	 * */
	public static void deleteTable(String tableName)throws Exception{
		HBaseAdmin admin=new HBaseAdmin(conf);
		if(admin.tableExists(tableName)){
			admin.disableTable(tableName);//禁用表
			admin.deleteTable(tableName);//删除表
			System.out.println("删除表成功!");
		}else{
			System.out.println("删除的表不存在!");
		}
		admin.close();
	}

	/**
	 * 插入一条数据
	 * @param tableName 表明
	 * @param columnFamily 列簇
	 * @param column      列
	 * @param value       值
	 * 
	 * ***/
	public static void insertOneRow(String tableName,String rowkey,String columnFamily,String column,String value)throws Exception{

		HTable table=new HTable(conf, tableName);
		Put put=new Put(Bytes.toBytes(rowkey));
		put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));
		table.put(put);//放入表
		table.close();//释放资源

	}

	/**
	 * 删除一条数据
	 * @param tableName 表名
	 * @param row  rowkey行键
	 * 
	 * */
	public static void deleteOneRow(String tableName,String row)throws Exception{

		HTable table=new HTable(conf, tableName);
		Delete delete=new Delete(Bytes.toBytes(row));
		table.delete(delete);
		table.close();
	}

/**
	 * 删除多条数据
	 * @param tableName 表名
	 * @param rows 行健集合
	 * 
	 * **/
	public static void deleteList(String tableName,String rows[])throws Exception{
		HTable table=new HTable(conf, tableName);
		List<Delete> list=new ArrayList<Delete>();
		for(String row:rows){
			Delete del=new Delete(Bytes.toBytes(row));
			list.add(del);
		}
		table.delete(list);
		table.close();//释放资源

	}

/**
	 * 获取一条数据,根据rowkey
	 * @param  tableName 表名
	 * @param  row 行健
	 * 
	 * **/
	public static void getOneRow(String tableName,String row)throws Exception{
		HTable table=new HTable(conf, tableName);
		Get get=new Get(Bytes.toBytes(row));
		Result result=table.get(get);
		printRecoder(result);//打印记录
		table.close();//释放资源
	}

	/**
	 * 查看某个表下的所有数据
	 * 
	 * @param tableName 表名
	 * */
	public static void showAll(String tableName)throws Exception{
		HTable table=new HTable(conf, tableName);
		Scan scan=new Scan();
		ResultScanner rs=table.getScanner(scan);
		for(Result r:rs){
			printRecoder(r);//打印记录
		}
		table.close();//释放资源
	}

/**
	 * 查看某个表下的所有数据
	 * 
	 * @param tableName 表名
	 * @param rowKey  行健
	 * */
	public static void ScanPrefixByRowKey(String tableName,String rowKey)throws Exception{
		HTable table=new HTable(conf, tableName);
		Scan scan=new Scan();
		scan.setFilter(new PrefixFilter(Bytes.toBytes(rowKey)));
		ResultScanner rs=table.getScanner(scan);
		for(Result r:rs){
			printRecoder(r);//打印记录
		}
		table.close();//释放资源
	}

/**
	 * 查看某个表下的所有数据
	 * 
	 * @param tableName 表名
	 * @param rowKey 行健扫描
	 * @param limit  限制返回数据量
	 * */
	public static void ScanPrefixByRowKeyAndLimit(String tableName,String rowKey,long limit)throws Exception{
		HTable table=new HTable(conf, tableName);
		Scan scan=new Scan();
		scan.setFilter(new PrefixFilter(Bytes.toBytes(rowKey)));
		scan.setFilter(new  PageFilter(limit));
		ResultScanner rs=table.getScanner(scan);
		for(Result r:rs){
			printRecoder(r);//打印记录
		}
		table.close();//释放资源
	}

/**
	 * 根据rowkey扫描一段范围
	 * @param tableName 表名
	 * @param startRow 开始的行健
	 * @param stopRow  结束的行健
	 * **/
	public  void scanByStartAndStopRow(String tableName,String startRow,String stopRow)throws Exception{
		HTable table=new HTable(conf, tableName);
		Scan scan=new Scan();
		scan.setStartRow(Bytes.toBytes(startRow));
		scan.setStopRow(Bytes.toBytes(stopRow));
		ResultScanner rs=table.getScanner(scan);
		for(Result r:rs){
			printRecoder(r);
		}
		table.close();//释放资源

}
	/**
	 * 扫描整个表里面具体的某个字段的值
	 * @param tableName  表名
	 * @param columnFalimy 列簇
	 * @param column 列
	 * **/
	public static  void getValueDetail(String tableName,String columnFalimy,String column)throws Exception{

		HTable table=new HTable(conf, tableName);
		Scan scan=new Scan();
		ResultScanner rs=table.getScanner(scan);
		for(Result r:rs){
			System.out.println("值: " +new String(r.getValue(Bytes.toBytes(columnFalimy), Bytes.toBytes(column))));
		}
		table.close();//释放资源

	}

/**
	 * 打印一条记录的详情
	 * 
	 * */
	public  static void printRecoder(Result result)throws Exception{
		for(Cell cell:result.rawCells()){
			System.out.print("行健: "+new String(CellUtil.cloneRow(cell)));
			System.out.print("列簇: "+new String(CellUtil.cloneFamily(cell)));
			System.out.print(" 列: "+new String(CellUtil.cloneQualifier(cell)));
			System.out.print(" 值: "+new String(CellUtil.cloneValue(cell)));
			System.out.println("时间戳: "+cell.getTimestamp());	
		}
	}

}

    转载仅供参考,版权属于原作者。祝你愉快,满意请采纳哦

相关问答

更多
  • 写了个Hbase新的api的增删改查的工具类,以供参考,直接拷贝代码就能用,散仙觉得基础的功能,都有了,代码如下: package com.dhgate.hbase.test; import java.util.ArrayList; import java.util.List; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.Cel ...
  • 通过对Hbase API的使用,下面例子举例了常见对HBase的操作,如下所示: [java] view plain copy package net.csdn.jtlyuan; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.ha ...
  • Pheonix是完全不同的数据检索方法......我希望在测试时,测试数据可用! 以下代码应该有效。 for (Result result = scanner.next(); (result != null); result = scanner.next()) { for(KeyValue keyValue : result.list()) { System.out.println("Qualifier : " + keyValue.getKeyString() + " : Valu ...
  • 如果您在独立模式下的hbase-site.xml为空(),则不必设置任何内容。 如果已覆盖hbase-site.xml中的任何内容,则最好添加该hbase-site.xml,而不是单独设置参数。 Configuration config = HBaseConfiguration.create(); config.addResource("/hbase-site.xml"); If your hbase-site.xml in standalone mode is ...
  • 使用Java API: HColumnDescriptor cfDescriptor = new HColumnDescriptor(Bytes.toBytes("cfName")); cfDescriptor.setTimeToLive(20); // in seconds tableDesc.addFamily(cfDescriptor); admin.createTable(tableDesc); 并使用shell: alter ‘tableName′, NAME => ‘cfname′, TTL ...
  • 将Get操作列表传递给批处理调用: ... import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.client.Get; import org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.client.Result; imp ...
  • 在您的conf文件“hbase-env.sh”中:而不是您拥有的行: export HBASE_OPTS="-XX:+UseConcMarkSweepGC" 添加: -Djava.security.krb5.realm= -Djava.security.krb5.kdc= 因此该行将export HBASE_OPTS="-XX:+UseConcMarkSweepGC -Djava.security.krb5.realm= -Djava.security.krb5.kdc="在此处查看上下文: OSX上的Ha ...
  • 每列系列指定最大版本和其他ttl类型设置。 所以最大版本在HColumnDescriptor上 。 Max versions, and other ttl type settings, is specified per column family. So the max versions is on the HColumnDescriptor.
  • 通过设置“HADOOP_CLASSPATH”如下所示解决问题,然后在同一个终端运行hadoop / hbase jar: export HADOOP_CLASSPATH="$HADOOP_CLASSPATH:/usr/hdp/2.3.4.0-3485/hbase/lib/hbase-client.jar:/usr/hdp/2.3.4.0-3485/hbase/lib/hbase-common.jar:/usr/hdp/2.3.4.0-3485/hbase/lib/hbase-protocol-1.1.2. ...
  • 这似乎是群集的问题。 你能确保HBase幸福健康,所有Region服务器都启动并运行吗? 请打开shell并首先列出表格以确保基本检查。另外,检查是否有任何版本的罐子不匹配。 如果你从下面的集群运行是肯定的射击方法,以避免任何意外。 Configuration conf = HBaseConfiguration.create(); conf.addResource("core-site.xml"); conf.addResource("hbase-site.xml"); conf.addResource(" ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)