hadoop提供了一个跑在yarn上的示例,可以运行

执行命令

bin/hadoop jar share/hadoop/yarn/hadoop-yarn-applications-distributedshell-2.5.2.jar org.apache.hadoop.yarn.applications.distributedshell.Client --jar share/hadoop/yarn/hadoop-yarn-applications-distributedshell-2.5.2.jar --shell_command ls --num_containers 10 --container_memory 350 --master_memory 350 --priority 10



执行日志-----日志

15/07/21 18:13:23 INFO distributedshell.Client: Initializing Client
15/07/21 18:13:23 INFO distributedshell.Client: Running Client
15/07/21 18:13:24 INFO client.RMProxy: Connecting to ResourceManager at Hadoop-Master/192.168.70.65:8032
15/07/21 18:13:24 INFO distributedshell.Client: Got Cluster metric info from ASM, numNodeManagers=3
15/07/21 18:13:24 INFO distributedshell.Client: Got Cluster node info from ASM
15/07/21 18:13:24 INFO distributedshell.Client: Got node report from ASM for, nodeId=Hadoop-Node2:38911, nodeAddressHadoop-Node2:8042, nodeRackName/default-rack, nodeNumContainers0
15/07/21 18:13:24 INFO distributedshell.Client: Got node report from ASM for, nodeId=Hadoop-Node1:45191, nodeAddressHadoop-Node1:8042, nodeRackName/default-rack, nodeNumContainers0
15/07/21 18:13:24 INFO distributedshell.Client: Got node report from ASM for, nodeId=Hadoop-Node0:44608, nodeAddressHadoop-Node0:8042, nodeRackName/default-rack, nodeNumContainers0
15/07/21 18:13:24 INFO distributedshell.Client: Queue info, queueName=default, queueCurrentCapacity=0.0, queueMaxCapacity=1.0, queueApplicationCount=0, queueChildQueueCount=0
15/07/21 18:13:24 INFO distributedshell.Client: User ACL Info for Queue, queueName=root, userAcl=SUBMIT_APPLICATIONS
15/07/21 18:13:24 INFO distributedshell.Client: User ACL Info for Queue, queueName=root, userAcl=ADMINISTER_QUEUE
15/07/21 18:13:24 INFO distributedshell.Client: User ACL Info for Queue, queueName=default, userAcl=SUBMIT_APPLICATIONS
15/07/21 18:13:24 INFO distributedshell.Client: User ACL Info for Queue, queueName=default, userAcl=ADMINISTER_QUEUE
15/07/21 18:13:24 INFO distributedshell.Client: Max mem capabililty of resources in this cluster 8192
15/07/21 18:13:24 INFO distributedshell.Client: Max virtual cores capabililty of resources in this cluster 32
15/07/21 18:13:24 INFO distributedshell.Client: Copy App Master jar from local filesystem and add to local environment
15/07/21 18:13:25 INFO distributedshell.Client: Set the environment for the application master
15/07/21 18:13:25 INFO distributedshell.Client: Setting up app master command
15/07/21 18:13:25 INFO distributedshell.Client: Completed setting up app master command {{JAVA_HOME}}/bin/java -Xmx350m org.apache.hadoop.yarn.applications.distributedshell.ApplicationMaster --container_memory 350 --container_vcores 1 --num_containers 10 --priority 0 1>/AppMaster.stdout 2>/AppMaster.stderr 
15/07/21 18:13:25 INFO distributedshell.Client: Submitting application to ASM
15/07/21 18:13:25 INFO impl.YarnClientImpl: Submitted application application_1430970937754_0008
15/07/21 18:13:26 INFO distributedshell.Client: Got application report from ASM for, appId=8, clientToAMToken=null, appDiagnostics=, appMasterHost=N/A, appQueue=default, appMasterRpcPort=-1, appStartTime=1437473605691, yarnAppState=ACCEPTED, distributedFinalState=UNDEFINED, appTrackingUrl=http://Hadoop-Master:8088/proxy/application_1430970937754_0008/, appUser=hadoop
15/07/21 18:13:27 INFO distributedshell.Client: Got application report from ASM for, appId=8, clientToAMToken=null, appDiagnostics=, appMasterHost=N/A, appQueue=default, appMasterRpcPort=-1, appStartTime=1437473605691, yarnAppState=ACCEPTED, distributedFinalState=UNDEFINED, appTrackingUrl=http://Hadoop-Master:8088/proxy/application_1430970937754_0008/, appUser=hadoop
15/07/21 18:13:28 INFO distributedshell.Client: Got application report from ASM for, appId=8, clientToAMToken=null, appDiagnostics=, appMasterHost=Hadoop-Node1/192.168.70.61, appQueue=default, appMasterRpcPort=-1, appStartTime=1437473605691, yarnAppState=RUNNING, distributedFinalState=UNDEFINED, appTrackingUrl=http://Hadoop-Master:8088/proxy/application_1430970937754_0008/A, appUser=hadoop
15/07/21 18:13:29 INFO distributedshell.Client: Got application report from ASM for, appId=8, clientToAMToken=null, appDiagnostics=, appMasterHost=Hadoop-Node1/192.168.70.61, appQueue=default, appMasterRpcPort=-1, appStartTime=1437473605691, yarnAppState=RUNNING, distributedFinalState=UNDEFINED, appTrackingUrl=http://Hadoop-Master:8088/proxy/application_1430970937754_0008/A, appUser=hadoop
15/07/21 18:13:30 INFO distributedshell.Client: Got application report from ASM for, appId=8, clientToAMToken=null, appDiagnostics=, appMasterHost=Hadoop-Node1/192.168.70.61, appQueue=default, appMasterRpcPort=-1, appStartTime=1437473605691, yarnAppState=RUNNING, distributedFinalState=UNDEFINED, appTrackingUrl=http://Hadoop-Master:8088/proxy/application_1430970937754_0008/A, appUser=hadoop
15/07/21 18:13:31 INFO distributedshell.Client: Got application report from ASM for, appId=8, clientToAMToken=null, appDiagnostics=, appMasterHost=Hadoop-Node1/192.168.70.61, appQueue=default, appMasterRpcPort=-1, appStartTime=1437473605691, yarnAppState=RUNNING, distributedFinalState=UNDEFINED, appTrackingUrl=http://Hadoop-Master:8088/proxy/application_1430970937754_0008/A, appUser=hadoop
15/07/21 18:13:32 INFO distributedshell.Client: Got application report from ASM for, appId=8, clientToAMToken=null, appDiagnostics=, appMasterHost=Hadoop-Node1/192.168.70.61, appQueue=default, appMasterRpcPort=-1, appStartTime=1437473605691, yarnAppState=RUNNING, distributedFinalState=UNDEFINED, appTrackingUrl=http://Hadoop-Master:8088/proxy/application_1430970937754_0008/A, appUser=hadoop
15/07/21 18:13:33 INFO distributedshell.Client: Got application report from ASM for, appId=8, clientToAMToken=null, appDiagnostics=, appMasterHost=Hadoop-Node1/192.168.70.61, appQueue=default, appMasterRpcPort=-1, appStartTime=1437473605691, yarnAppState=RUNNING, distributedFinalState=UNDEFINED, appTrackingUrl=http://Hadoop-Master:8088/proxy/application_1430970937754_0008/A, appUser=hadoop
15/07/21 18:13:34 INFO distributedshell.Client: Got application report from ASM for, appId=8, clientToAMToken=null, appDiagnostics=, appMasterHost=Hadoop-Node1/192.168.70.61, appQueue=default, appMasterRpcPort=-1, appStartTime=1437473605691, yarnAppState=FINISHED, distributedFinalState=SUCCEEDED, appTrackingUrl=http://Hadoop-Master:8088/proxy/application_1430970937754_0008/A, appUser=hadoop
15/07/21 18:13:34 INFO distributedshell.Client: Application has completed successfully. Breaking monitoring loop
15/07/21 18:13:34 INFO distributedshell.Client: Application completed successfully
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用Java代码提交Hive SQL到YARN运行,以下是一个示例: ```java import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.security.UserGroupInformation; import org.apache.hadoop.yarn.api.records.ApplicationId; import org.apache.hadoop.yarn.api.records.LocalResource; import org.apache.hadoop.yarn.api.records.LocalResourceType; import org.apache.hadoop.yarn.api.records.LocalResourceVisibility; import org.apache.hadoop.yarn.client.api.YarnClient; import org.apache.hadoop.yarn.client.api.YarnClientApplication; import org.apache.hadoop.yarn.conf.YarnConfiguration; import org.apache.hadoop.yarn.util.Apps; import org.apache.hadoop.yarn.util.ConverterUtils; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.Collections; import java.util.HashMap; import java.util.Map; public class SubmitHiveSqlToYarn { public static void main(String[] args) throws Exception { // 初始化YARN配置 Configuration conf = new YarnConfiguration(); // 创建YARN客户端 YarnClient yarnClient = YarnClient.createYarnClient(); yarnClient.init(conf); yarnClient.start(); // 创建YARN应用 YarnClientApplication app = yarnClient.createApplication(); // 设置应用名称 String appName = "Hive SQL on YARN"; app.setApplicationName(appName); // 创建容器启动命令 String command = "hive -e \"SELECT * FROM my_table\""; // 设置本地资源 Map<String, LocalResource> localResources = new HashMap<>(); File scriptFile = new File("path/to/hive_script.hql"); FileInputStream scriptFileStream = new FileInputStream(scriptFile); LocalResource scriptFileResource = createLocalResource(scriptFileStream, conf); localResources.put("hive_script.hql", scriptFileResource); // 设置容器环境变量 Map<String, String> env = new HashMap<>(); env.put("CLASSPATH", "./*"); // 设置容器资源 Map<String, String> containerResources = new HashMap<>(); containerResources.put("hive_script.hql", scriptFile.getAbsolutePath()); // 设置容器启动命令 Apps.addToEnvironment(env, "HADOOP_USER_NAME", UserGroupInformation.getCurrentUser().getShortUserName()); Apps.addToEnvironment(env, "HADOOP_HOME", System.getenv("HADOOP_HOME")); Apps.addToEnvironment(env, "HADOOP_CONF_DIR", System.getenv("HADOOP_CONF_DIR")); String command = "hive -f hive_script.hql"; containerResources.put("script", scriptFile.getAbsolutePath()); // 设置应用主类 String mainClass = "org.apache.hadoop.yarn.applications.distributedshell.ApplicationMaster"; // 设置应用主类参数 String[] mainArgs = new String[]{ "--jar", "path/to/your/jar", "--shell_command", command, "--shell_env", "CLASSPATH=./*", "--shell_env", "HADOOP_USER_NAME=" + UserGroupInformation.getCurrentUser().getShortUserName(), "--shell_env", "HADOOP_HOME=" + System.getenv("HADOOP_HOME"), "--shell_env", "HADOOP_CONF_DIR=" + System.getenv("HADOOP_CONF_DIR"), "--container_memory", "1024", "--num_containers", "1" }; // 提交应用 ApplicationId appId = app.getNewApplicationResponse().getApplicationId(); Apps.addToEnvironment(env, "APP_ID", appId.toString()); Apps.addToEnvironment(env, "SCRIPT_NAME", "hive_script.hql"); Apps.addToEnvironment(env, "CONTAINER_RESOURCES", containerResources.toString()); Apps.addToEnvironment(env, "CONTAINER_LAUNCH_COMMAND", command); Apps.addToEnvironment(env, "CONTAINER_ENV", env.toString()); Apps.addToEnvironment(env, "MAIN_CLASS", mainClass); Apps.addToEnvironment(env, "MAIN_ARGS", mainArgs.toString()); Apps.addToEnvironment(env, "LOCAL_RESOURCES", localResources.toString()); Apps.addToEnvironment(env, "APP_NAME", appName); // 创建应用主类 YarnClientApplication clientApplication = yarnClient.createApplication(); ApplicationSubmissionContext appContext = clientApplication.getApplicationSubmissionContext(); appContext.setApplicationName(appName); // 设置资源需求 ResourceRequest resourceRequest = ResourceRequest.newInstance( Priority.newInstance(0), ResourceRequest.ANY, Resource.newInstance(1024, 1), 1 ); appContext.setAMContainerResourceRequest(resourceRequest); // 设置应用主类信息 ContainerLaunchContext amContainer = ContainerLaunchContext.newInstance( localResources, env, Collections.singletonList(command), null, null, null ); appContext.setAMContainerSpec(amContainer); // 提交应用 yarnClient.submitApplication(appContext); } private static LocalResource createLocalResource(FileInputStream fileInputStream, Configuration configuration) throws IOException { // 创建本地资源 LocalResource localResource = Records.newRecord(LocalResource.class); // 设置本地资源类型 localResource.setType(LocalResourceType.FILE); // 设置本地资源可见性 localResource.setVisibility(LocalResourceVisibility.APPLICATION); // 设置本地资源大小 FileStatus fileStatus = FileSystem.get(configuration).getFileStatus(new Path("path/to/hive_script.hql")); localResource.setSize(fileStatus.getLen()); // 设置本地资源时间戳 localResource.setTimestamp(fileStatus.getModificationTime()); // 设置本地资源位置 Path path = new Path("path/to/hive_script.hql"); URL url = ConverterUtils.getYarnUrlFromURI(path.toUri()); localResource.setResource(url); return localResource; } } ``` 上述代码中,主要步骤如下: 1. 初始化YARN配置 2. 创建YARN客户端 3. 创建YARN应用 4. 设置应用名称 5. 创建容器启动命令 6. 设置本地资源 7. 设置容器环境变量 8. 设置容器资源 9. 设置容器启动命令 10. 设置应用主类 11. 设置应用主类参数 12. 提交应用 这个示例中,我们使用了`hive -f`命令来执行Hive SQL脚本,你需要将`my_table`替换为你的表名,将`path/to/hive_script.hql`替换为你的Hive SQL脚本所在的路径,将`path/to/your/jar`替换为你的Java程序的jar包路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值